iOS实现简易的计算器
作者:XYQ全哥
这篇文章主要为大家详细介绍了iOS实现简易的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了iOS实现简易的计算器的具体代码,供大家参考,具体内容如下
初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。