Simple Calculator Design Code

Source: Internet
Author: User

#import "AppDelegate.h"

#define Kscreenwidth Cgrectgetwidth ([[[UIScreen mainscreen] bounds])

#define Kscreenheight Cgrectgetheight ([[[UIScreen mainscreen] bounds])

#define KBUTTONWIDTH ((kScreenWidth-5)/4)

#define Kbuttonheight Kbuttonwidth

#define KBUTTONOFFSETX 1

#define KBUTTONOFFSETY 1

#define Kbuttonoriginy (Kscreenheight-(kbuttonheight + kbuttonoffsety) * 5)

@interface Appdelegate ()

{

BOOL _ishave;

BOOL _isresult;

}

@property (retain,nonatomic) NSString *string;

@end

@implementation Appdelegate

-(void) dealloc{

[_window release];

[Super Dealloc];

}

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Self.window = [[[UIWindow alloc] Initwithframe:[[uiscreen Mainscreen] bounds]]autorelease];

Override point for customization after application launch.

Self.window.backgroundColor = [Uicolor blackcolor];

[Self.window makekeyandvisible];

/*

AC +-%/

7 8 9 *

4 5 6-

1 2 3 +

0. =

*/

Nsarray *titles = @[@[@ "AC", @ "+-", @ "Baifenhao", @ "Chuhao"], @[@ "7", @ "8", @ "9", @ "Chenghao"], @[@ "4", @ "5", @ "6", @ "Jian Hao "], @[@" 1 ", @" 2 ", @" 3 ", @" Jiahao "], @[@" 0 ", @" ", @" Dian ", @" Dengyu "];

for (int i = 0; i < Titles.count; i++) {

Nsarray *subtitles = titles[i];

for (int j = 0; J < Subtitles.count; J + +) {

NSString *title = Subtitles[j];

if ([title isequaltostring:@ ""]) {

Continue

}

UIButton *abutton = [UIButton buttonwithtype:uibuttontypecustom];

abutton.tag=100+ (10*i) +j;

CGRect frame = CGRectMake (Kbuttonoffsetx + (kbuttonwidth + kbuttonoffsetx) * J, Kbuttonoriginy + (Kbuttonheight + KButtonO ffsety) * I, kbuttonwidth, kbuttonheight);

if ([title isequaltostring:@ "0"]) {

Frame.size.width = frame.size.width * 2 + kbuttonoffsetx;

}

Abutton.frame = frame;

NSString *normalimagename = [Subtitles[j] stringbyappendingstring:@ "_btn_nor"];

NSString *highlightedimagename = [Subtitles[j] stringbyappendingstring:@ "_btn_highlighted"];

[Abutton setimage:[uiimage Imagenamed:normalimagename] forstate:uicontrolstatenormal];

[Abutton setimage:[uiimage Imagenamed:highlightedimagename] forstate:uicontrolstatehighlighted];

[Abutton Settitle:title Forstate:uicontrolstatenormal];

[Abutton addtarget:self Action: @selector (handlebuttonaction:) forcontrolevents:uicontroleventtouchupinside];

[Self.window Addsubview:abutton];

}

}

Uitextfield *alabel=[[[uitextfield alloc]initwithframe:cgrectmake (0, 0, Kscreenwidth, kScreenHeight-5* ( kbuttonheight+kbuttonoffsety))]autorelease];

alabel.tag=1000;

Alabel.backgroundcolor=[uicolor Blackcolor];

Alabel.clearsonbeginediting=yes;

Alabel.enabled=no;

[email protected] "0";

Alabel.textcolor=[uicolor Whitecolor];

Alabel.font=[uifont fontwithname:@ "Times New Roman" size:70];

Alabel.textalignment=nstextalignmentright;

[_window Addsubview:alabel];

return YES;

}

-(void) Handlebuttonaction: (UIButton *) abutton{

Uitextfield *label= (Uitextfield *) [_window viewwithtag:1000];

Switch (Abutton.tag) {

Case 100:{

[email protected] "0";

Label.clearsonbeginediting=yes;

Break

}case 101:{

float Num=[label.text Floatvalue];

NSNumber *number=[nsnumber Numberwithfloat:-num];

Label.text=[nsstring stringwithformat:@ "%@", number];

Break

}case 102:{

float Num=[label.text Floatvalue];

NSNumber *number=[nsnumber numberwithfloat: (num/100)];

Label.text=[nsstring stringwithformat:@ "%@", number];

Break

}case 110:

Case 111:

Case 112:

Case 120:

Case 121:

Case 122:

Case 130:

Case 131:

Case 132:

Case 140:

Case 142:{

if (_ishave| | [Label.text isequaltostring:@ ""]| | _isresult) {

[Email protected] "";

Label.text=[label.text StringByAppendingString:abutton.currentTitle];

}else{label.text=[label.text StringByAppendingString:abutton.currentTitle];

}

_ishave=no;

_isresult=no;

Break

}case 103:

Case 113:

Case 123:

Case 133:{

Self.string=[label.text StringByAppendingString:abutton.currentTitle];

_ishave=yes;

Break

}case 143:{

if ([self.string hassuffix:@ "Jiahao"]) {

float value1=[self.string Floatvalue];

float Value2=[label.text Floatvalue];

NSNumber *number=[nsnumber numberwithfloat: (value1+value2)];

Label.text=[nsstring stringwithformat:@ "%@", number];

[Email protected] "";

_isresult=yes;

}else if ([self.string hassuffix:@ "Jianhao"]) {

float value1=[self.string Floatvalue];

float Value2=[label.text Floatvalue];

NSNumber *number=[nsnumber numberwithfloat: (value1-value2)];

Label.text=[nsstring stringwithformat:@ "%@", number];

[Email protected] "";

_isresult=yes;

}else if ([_string hassuffix:@ "Chenghao"]) {

float value1=[self.string Floatvalue];

float Value2=[label.text Floatvalue];

NSNumber *number=[nsnumber numberwithfloat: (value1*value2)];

Label.text=[nsstring stringwithformat:@ "%@", number];

[Email protected] "";

_isresult=yes;

}else if ([self.string hassuffix:@ "Chuhao"]) {

float value1=[self.string Floatvalue];

float Value2=[label.text Floatvalue];

NSNumber *number=[nsnumber numberwithfloat: (value1/value2)];

Label.text=[nsstring stringwithformat:@ "%@", number];

[Email protected] "";

_isresult=yes;

}

Break

}

Default

Break

}

}

Simple Calculator Design Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.