Preliminary contact view, made a simple calculator, basically simple calculation is no problem, not very perfect, there may be some bugs, make unremitting efforts.
1//2//VIEWCONTROLLER.M 3//Calculator 4//5//Created by Ma C on 15/8/25. 6//Copyright (c) 2015 BJSXT. All rights reserved. 7//8 9 #import "ViewController.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet UITEXTF Ield *resultfield; @property (nonatomic,assign) CGFloat temp; @property (nonatomic,assign) cgfloat num1; @property (nonatomic,assign) cgfloat num2; @property (Nonatomic,strong) nsmutablestring *string; @property (Nonatomic,strong) Nsarray *arr; @end @implementation Viewcontroller-(ibaction) Buttonclear: (UIButton *) sender [_string setstring:@] ]; Start the calculation again and the text box is empty to self. Resultfield.text = _string; (ibaction) Button7: (UIButton *) sender [_string appendstring:@ "7"]; Resultfield.text = _string; (ibaction) Button8: (UIButton *) Sender {[_string appendstring:@ "8"]; Resultfield.text = _string; -(Ibaction) Button9: (UIButton *) SendeR [_string appendstring:@ "9"]; Resultfield.text = _string; (ibaction) Button4: (UIButton *) sender, [_string appendstring:@ "4"]; Resultfield.text = _string; (ibaction) Button5: (UIButton *) sender, [_string appendstring:@ "5"]; Resultfield.text = _string; (ibaction) Button6: (UIButton *) Sender {_string appendstring:@ "6"]; Resultfield.text = _string; (ibaction) Button1: (UIButton *) sender, [_string appendstring:@ "1"]; Resultfield.text = _string; (ibaction) Button3: (UIButton *) sender of [appendstring:@] [_string] 3 "]; Resultfield.text = _string; +-(ibaction) Button2: (UIButton *) sender [appendstring:@] [_string] 2 "]; Resultfield.text = _string; (ibaction) Button0: (UIButton *) sender, [_string appendstring:@ "0"]; Resultfield.text = _string; (IbactiON) Buttonpoint: (UIButton *) sender [_string appendstring:@ "] Self. Resultfield.text = _string; 80} 81 82//Trigger Arithmetic Operation Event-(ibaction) Buttondiv: (UIButton *) sender, [_string appendstring:@ "/"]; Resultfield.text = _string; (ibaction) Buttonmul: (UIButton *) sender, [_string appendstring:@ "*"]; Resultfield.text = _string; 94-(Ibaction) Buttonsub: (UIButton *) sender, [_string appendstring:@ "-"]; 98 self. Resultfield.text = _string; }100 101-(Ibaction) Buttonadd: (UIButton *) sender102 {103 [_string appendstring:@ "+"];104 self. Resultfield.text = _string;105}106 107//Do result operation 108-(ibaction) Buttonequal: (UIButton *) sender109 {111 for (int i= 0; I<[_string length]; i++) 113 Self.arr = [[Nsarray alloc]init];115 116//Enter only one number, do not operate 117 if ( [_string Length] = = 1) 118 {119 Self.temp = [_string doublevalue];120 break;121}122 123 124//Do addition operation if ([_string characteratindex:i] = = ' + ') 1 $ {127 Self.arr = [_string componentsseparatedbystring:@ "+"];128 129 self.num 1 = [Self.arr[0] doublevalue];130 self.num2 = [Self.arr[1] doublevalue];131 self.temp = self.num1 + self.num2;132 break;133}134 135 136//subtraction Operation 137 if ([_string characteratindex :(i+1)] = = '-') 138 {139 Self.arr = [_string componentsseparatedbystring:@ "-"];140 141 if ([self.arr count] = = 2) 142 {143 self.num1 = [Self.arr[0] doublevalue];144 SELF.NUM2 = [Self.arr[1] doublevalue];145 self.temp = self.num1-self.num2;146}147 else148 {149 SELF.NUM1 = [Self.arr[1] doublevalue];150 self.num2 = [SE LF.ARR[2] Doublevalue];1Wuyi self.temp =-(self.num1 + self.num2);}153 break;154}155 15 6 157//Do Division operation 158 if ([_string characteratindex:i] = = '/') 159 {Self.arr = [_ String componentsseparatedbystring:@ "/"];161 self.num1 = [Self.arr[0] doublevalue];162 self.num2 = [Self.arr[1] doublevalue];163 self.temp = self.num1/self.num2;164 break;165}166 167//Do multiplication 168 if ([_string characteratindex:i] = = ' * ') 169 {Self.arr = [_string C omponentsseparatedbystring:@ "*"];171 SELF.NUM1 = [Self.arr[0] doublevalue];172 self.num2 = [SELF.A RR[1] doublevalue];173 self.temp = self.num1 * self.num2;174 break;175}176 177 }178 179//output result [_string setstring:[nsstring stringwithformat:@ "%.2f", self.temp]];181 self. Resultfield.text = _string;182}183 184-(void) Viewdidload {185 [Super viewdidload];186 187//Create a variable string 188 _string = [nsmutablestring stringwi thcapacity:20];189}190 191-(void) didreceivememorywarning {192 [Super didreceivememorywarning];193//Dispose of Any resources this can be recreated.194}195 196 @end
IOS: Make a simple calculator