2015.12.29 iOS SimpleCalculator简单计算器
2015-12-29 21:20
453 查看
这是开始写的第一个iOS程序。
1.首先创建一个SingleViewApplication界面(iOS中),这个模式就是默认为我们提供了一个界面。
SimpleCalculator
请教一下各位大神,小数点的问题有什么更好地想法没有,我觉得自己的解决方案有些繁琐,并不是最简便的。谢谢。Orz~
1.首先创建一个SingleViewApplication界面(iOS中),这个模式就是默认为我们提供了一个界面。
#import "ViewController.h" typedef enum{ kStatusNum, kStatusOperation }kStatus; typedef enum{ kOperationTypeAdd = 1, kOperationTypeMinus, kOperationTypeMultiply, kOperationTypeDevide, kOperationTypeEqual, kOperationTypeNone, kOperationTypeSign = 11, kOperationTypePercent, kOperationTypePoint }kOperationType; @interface ViewController () @property (weak, nonatomic) IBOutlet UILabel *resultLabel; @property (assign, nonatomic) double firstParameter; @property (assign, nonatomic) kOperationType lastOperation, isOtherOperation; @property (assign, nonatomic) kStatus status; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //赋初值 self.lastOperation = kOperationTypeNone; self.status = kStatusNum; self.isOtherOperation = kOperationTypeNone; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //数字键 - (IBAction)numButtonDidClicked:(UIButton *)sender { NSMutableString *showNum = [NSMutableString stringWithCapacity:0]; //进行判断,是输入数字并且上一次操作不是正负 if (self.status == kStatusNum && self.isOtherOperation != kOperationTypeSign) { //进行显示的拼接 [showNum appendFormat:@"%@%@", self.resultLabel.text, sender.titleLabel.text]; //判断第一个字符是“0”切第二个字符不为“.”的情况 BOOL compareResultA, compareResultB; compareResultA = [self comparationIsEqual:[showNum substringToIndex:1] anotherNSString:@"0"]; compareResultB = [self comparationIsEqual:[showNum substringWithRange:NSMakeRange(1, 1)] anotherNSString:@"."]; //这种情况需要删除开头的“0” if (compareResultA == YES && compareResultB == NO) { [showNum deleteCharactersInRange:NSMakeRange(0, 1)]; } } else{ [showNum appendFormat:@"%@", sender.titleLabel.text]; self.status = kStatusNum; self.isOtherOperation = kOperationTypeNone; } //显示结果 self.resultLabel.text = showNum; } - (BOOL)comparationIsEqual:(NSString *)StringA anotherNSString:(NSString *)StringB{ if ([StringA isEqualToString:StringB]) { return YES; } else{ return NO; } } //+ - * / = 键 - (IBAction)operationButtonDidClicked:(UIButton *)sender { if (self.status != kStatusOperation) { self.status = kStatusOperation; //有两种情况 //1.第一次按操作,只需要保存这次操作 if (self.lastOperation != kOperationTypeNone) { //2.前面有操作需要计算,保存这次操作 [self calculate]; } else{ //第一个参数输入完毕 保存 self.firstParameter = [self.resultLabel.text doubleValue]; } } //保存这一次操作 if (sender.tag == kOperationTypeEqual) { self.lastOperation = kOperationTypeNone; } else{ self.lastOperation = (kOperationType)sender.tag; } } //清零 - (IBAction)clearButtonDidClicked:(UIButton *)sender { self.resultLabel.text = @"0"; self.firstParameter = 0; self.lastOperation = kOperationTypeNone; self.status = kStatusNum; self.isOtherOperation = kOperationTypeNone; } //计算结果 - (void)calculate{ //获取第二个参数 double secondParameter = [self.resultLabel.text doubleValue]; double result; switch (self.lastOperation) { case kOperationTypeAdd: result = self.firstParameter + secondParameter; break; case kOperationTypeMinus: result = self.firstParameter - secondParameter; break; case kOperationTypeMultiply: result = self.firstParameter * secondParameter; break; case kOperationTypeDevide: result = self.firstParameter / secondParameter; break; default: break; } //显示最终结果 self.resultLabel.text = [NSString stringWithFormat:@"%g", result]; //当前结果就是写一次的参数值 self.firstParameter = result; //更改操作符 self.lastOperation = kOperationTypeNone; } //± % 键 - (IBAction)otherOperationButtonDidClicked:(UIButton *)sender { double temp; //判断按键 switch (sender.tag) { case kOperationTypeSign: temp = [self.resultLabel.text doubleValue]; self.resultLabel.text = [NSString stringWithFormat:@"%g", temp * (-1)]; self.firstParameter = [self.resultLabel.text doubleValue]; self.isOtherOperation = kOperationTypeSign; break; case kOperationTypePercent: temp = [self.resultLabel.text doubleValue]; self.resultLabel.text = [NSString stringWithFormat:@"%g", temp / 100]; break; default: break; } } //. 键 - (IBAction)pointButtonDidClicked:(UIButton *)sender { if (self.isOtherOperation != kOperationTypePoint) { self.resultLabel.text = [NSString stringWithFormat:@"%@.", self.resultLabel.text]; self.isOtherOperation = kOperationTypePoint; } } @end
SimpleCalculator
请教一下各位大神,小数点的问题有什么更好地想法没有,我觉得自己的解决方案有些繁琐,并不是最简便的。谢谢。Orz~
相关文章推荐
- iOS的性能优化
- iOS中解决各种手势冲突问题
- AudioSource声音播放的静态方法
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别
- iOS7中的ViewController切换(一.present与dismiss方式)
- ios 并行开发
- iOS 搜索框
- iOS开发融云即时通讯集成详细步骤
- runtime模型与字典互转
- About iOS总结
- 百度告警-Nagios接入
- iOS -- warnings
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- ScrollFix.js:一个 iOS5 溢出滚动的(有限)修复方案
- IOS 消息分发
- IOS自定义弹出框
- Masonry教程--IOS自适配,丢掉Autolayout吧
- iOS 中各种引用关键字区别
- IOS 程序执行
- iOS解决Cell的分割线宽度不满屏的方法