iOS计算器
XYQ全哥 人气:0初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉。
// // ViewController.m // 计算器 // // Created by ma c on 15/8/25. // Copyright (c) 2015年 bjsxt. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *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:@""]; //重新开始计算,文本框置空 self.ResultField.text = _string; } - (IBAction)button7:(UIButton *)sender { [_string appendString:@"7"]; self.ResultField.text = _string; } - (IBAction)button8:(UIButton *)sender { [_string appendString:@"8"]; self.ResultField.text = _string; } - (IBAction)button9:(UIButton *)sender { [_string appendString:@"9"]; self.ResultField.text = _string; } - (IBAction)button4:(UIButton *)sender { [_string appendString:@"4"]; self.ResultField.text = _string; } - (IBAction)button5:(UIButton *)sender { [_string appendString:@"5"]; self.ResultField.text = _string; } - (IBAction)button6:(UIButton *)sender { [_string appendString:@"6"]; self.ResultField.text = _string; } - (IBAction)button1:(UIButton *)sender { [_string appendString:@"1"]; self.ResultField.text = _string; } - (IBAction)button3:(UIButton *)sender { [_string appendString:@"3"]; self.ResultField.text = _string; } - (IBAction)button2:(UIButton *)sender { [_string appendString:@"2"]; self.ResultField.text = _string; } - (IBAction)button0:(UIButton *)sender { [_string appendString:@"0"]; self.ResultField.text = _string; } - (IBAction)buttonPoint:(UIButton *)sender { [_string appendString:@"."]; self.ResultField.text = _string; } //触发算数运算事件 - (IBAction)buttonDiv:(UIButton *)sender { [_string appendString:@"/"]; self.ResultField.text = _string; } - (IBAction)buttonMul:(UIButton *)sender { [_string appendString:@"*"]; self.ResultField.text = _string; } - (IBAction)buttonSub:(UIButton *)sender { [_string appendString:@"-"]; self.ResultField.text = _string; } - (IBAction)buttonAdd:(UIButton *)sender { [_string appendString:@"+"]; self.ResultField.text = _string; } //做结果运算操作 - (IBAction)buttonEqual:(UIButton *)sender { for(int i=0; i<[_string length]; i++) { self.arr = [[NSArray alloc]init]; //只输入一个数,不做运算 if([_string length] == 1) { self.temp = [_string doubleValue]; break; } //做加法运算 if([_string characterAtIndex:i] == '+') { self.arr = [_string componentsSeparatedByString:@"+"]; self.num1 = [self.arr[0] doubleValue]; self.num2 = [self.arr[1] doubleValue]; self.temp = self.num1 + self.num2; break; } //做减法运算 if([_string characterAtIndex:(i+1)] == '-') { self.arr = [_string componentsSeparatedByString:@"-"]; if([self.arr count] == 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([_string characterAtIndex:i] == '/') { self.arr = [_string componentsSeparatedByString:@"/"]; self.num1 = [self.arr[0] doubleValue]; self.num2 = [self.arr[1] doubleValue]; self.temp = self.num1 / self.num2; break; } //做乘法运算 if([_string characterAtIndex:i] == '*') { self.arr = [_string componentsSeparatedByString:@"*"]; self.num1 = [self.arr[0] doubleValue]; self.num2 = [self.arr[1] doubleValue]; self.temp = self.num1 * self.num2; break; } } //输出结果 [_string setString:[NSString stringWithFormat:@"%.2f",self.temp]]; self.ResultField.text = _string; } - (void)viewDidLoad { [super viewDidLoad]; //创建一个可变的字符串 _string = [NSMutableString stringWithCapacity:20]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
加载全部内容