iOS实现简易计算器的代码怎么写(ios,开发技术)

时间:2024-05-05 13:52:41 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉。

iOS实现简易计算器的代码怎么写

////ViewController.m//计算器////Createdbymacon15/8/25.//Copyright(c)2015年bjsxt.Allrightsreserved.//#import"ViewController.h"@interfaceViewController()@property(weak,nonatomic)IBOutletUITextField*ResultField;@property(nonatomic,assign)CGFloattemp;@property(nonatomic,assign)CGFloatnum1;@property(nonatomic,assign)CGFloatnum2;@property(nonatomic,strong)NSMutableString*string;@property(nonatomic,strong)NSArray*arr;@end@implementationViewController-(IBAction)buttonClear:(UIButton*)sender{[_stringsetString:@""];//重新开始计算,文本框置空self.ResultField.text=_string;}-(IBAction)button7:(UIButton*)sender{[_stringappendString:@"7"];self.ResultField.text=_string;}-(IBAction)button8:(UIButton*)sender{[_stringappendString:@"8"];self.ResultField.text=_string;}-(IBAction)button9:(UIButton*)sender{[_stringappendString:@"9"];self.ResultField.text=_string;}-(IBAction)button4:(UIButton*)sender{[_stringappendString:@"4"];self.ResultField.text=_string;}-(IBAction)button5:(UIButton*)sender{[_stringappendString:@"5"];self.ResultField.text=_string;}-(IBAction)button6:(UIButton*)sender{[_stringappendString:@"6"];self.ResultField.text=_string;}-(IBAction)button1:(UIButton*)sender{[_stringappendString:@"1"];self.ResultField.text=_string;}-(IBAction)button3:(UIButton*)sender{[_stringappendString:@"3"];self.ResultField.text=_string;}-(IBAction)button2:(UIButton*)sender{[_stringappendString:@"2"];self.ResultField.text=_string;}-(IBAction)button0:(UIButton*)sender{[_stringappendString:@"0"];self.ResultField.text=_string;}-(IBAction)buttonPoint:(UIButton*)sender{[_stringappendString:@"."];self.ResultField.text=_string;}//触发算数运算事件-(IBAction)buttonDiv:(UIButton*)sender{[_stringappendString:@"/"];self.ResultField.text=_string;}-(IBAction)buttonMul:(UIButton*)sender{[_stringappendString:@"*"];self.ResultField.text=_string;}-(IBAction)buttonSub:(UIButton*)sender{[_stringappendString:@"-"];self.ResultField.text=_string;}-(IBAction)buttonAdd:(UIButton*)sender{[_stringappendString:@"+"];self.ResultField.text=_string;}//做结果运算操作-(IBAction)buttonEqual:(UIButton*)sender{for(inti=0;i<[_stringlength];i++){self.arr=[[NSArrayalloc]init];//只输入一个数,不做运算if([_stringlength]==1){self.temp=[_stringdoubleValue];break;}//做加法运算if([_stringcharacterAtIndex:i]=='+'){self.arr=[_stringcomponentsSeparatedByString:@"+"];self.num1=[self.arr[0]doubleValue];self.num2=[self.arr[1]doubleValue];self.temp=self.num1+self.num2;break;}//做减法运算if([_stringcharacterAtIndex:(i+1)]=='-'){self.arr=[_stringcomponentsSeparatedByString:@"-"];if([self.arrcount]==2){self.num1=[self.arr[0]doubleValue];self.num2=[self.arr[1]doubleValue];self.temp=self.num1-self.num2;}else{self.num1=[self.arr[1]doubleValue];self.num2=[self.arr[2]doubleValue];self.temp=-(self.num1+self.num2);}break;}//做除法运算if([_stringcharacterAtIndex:i]=='/'){self.arr=[_stringcomponentsSeparatedByString:@"/"];self.num1=[self.arr[0]doubleValue];self.num2=[self.arr[1]doubleValue];self.temp=self.num1/self.num2;break;}//做乘法运算if([_stringcharacterAtIndex:i]=='*'){self.arr=[_stringcomponentsSeparatedByString:@"*"];self.num1=[self.arr[0]doubleValue];self.num2=[self.arr[1]doubleValue];self.temp=self.num1*self.num2;break;}}//输出结果[_stringsetString:[NSStringstringWithFormat:@"%.2f",self.temp]];self.ResultField.text=_string;}-(void)viewDidLoad{[superviewDidLoad];//创建一个可变的字符串_string=[NSMutableStringstringWithCapacity:20];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}@end
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:iOS实现简易计算器的代码怎么写的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Linux系统中如何同步服务器时间下一篇:

4 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18