代码部分及注释
#import "ViewController.h"
//当前操作状态
typedef enum{
kStatusNum,
kStatusOperation,
kStatusDone
}kStatus;
//运算符
typedef enum{
kOperationTypeAdd = 1,
kOperationTypeMinus,
kOperationTypeMultiple,
kOperationTypeDevide
}kOperationType;
typedef enum{
kComputeTypePrimary, //+ -
kComputeTypeSenior
}kComputeType;
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *resultLable;
@property (nonatomic,assign) kStatus status;
@property (nonatomic,strong) NSMutableArray *paramMutableArray;
@property (nonatomic,strong) NSMutableArray *operationMutableArray;
@property (nonatomic,strong) NSDictionary *operationEnumDic;
@property (nonatomic,assign) BOOL pointStatus;//判断是否输入的是“.”
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.paramMutableArray = [NSMutableArray array];
self.operationMutableArray = [NSMutableArray array];
self.status = kStatusDone;
self.operationEnumDic = @{@"+":@1, @"-":@2, @"*":@3, @"/":@4};
self.pointStatus = NO;
}
//数字键
- (IBAction)normalButtonDidClicked:(UIButton *)sender {
//获取按钮上的数字
NSString *num = sender.titleLabel.text;
NSString *showNum = NULL;
//判断是否为一个数字新的开始
if (self.status == kStatusNum) {
//若不是点,可以拼接显示,如果是一个点则不执行
if (self.pointStatus == NO) {
NSString *currentNum = self.resultLable.text;
showNum = [NSString stringWithFormat:@"%@"@"%@",currentNum,num];
}else{
//判断是否是一个点
if (![num isEqualToString:@"."]) {
NSString *currentNum = self.resultLable.text;
showNum = [NSString stringWithFormat:@"%@"@"%@",currentNum,num];
}else{
showNum = self.resultLable.text;
}
}
}else{
//判断之前是否有结果,如果有,我们将丢弃这个结果
if (self.status == kStatusDone) {
[self.paramMutableArray removeAllObjects];
}
showNum = num;
self.status = kStatusNum;
}
//判断当前数是否为点
if([num isEqualToString:@"."]){
self.pointStatus = YES;
}
self.resultLable.text = showNum;
}
//+/-键
- (IBAction)signChangeButtonDIdClicked:(UIButton *)sender{
double currentNumTemp = [self.resultLable.text doubleValue]*(-1);
self.resultLable.text = [NSString stringWithFormat:@"%g",currentNumTemp];
}
//运算符键
- (IBAction)operationButtonDidClicked:(UIButton *)sender {
//判断是不是重复按下了操作符
if(self.status != kStatusOperation){
//改变当前的状态,开始输入新的数字
self.status = kStatusOperation;
self.pointStatus = NO;
//说明当前的数字结束了
//将这个数字保存在数组中
[self.paramMutableArray addObject:self.resultLable.text];
//保存当前按钮上的title
[self.operationMutableArray addObject:sender.titleLabel.text];
}else{
//若已经有操作符,则将现有操作符替换原来的操作符
//获取当前操作符
self.pointStatus = NO;
NSString *newOperation = sender.titleLabel.text;
//获取最后一个索引值
NSInteger lastIndex = self.operationMutableArray.count - 1;
[self.operationMutableArray replaceObjectAtIndex:lastIndex withObject:newOperation];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//=号键
- (IBAction)equalButtonDidClicked:(UIButton *)sender {
//添加当前的最后一个数字
[self.paramMutableArray addObject:self.resultLable.text];
//进行分级运算
[self computeWithType:kComputeTypeSenior];
[self computeWithType:kComputeTypePrimary];
//将结果显示到界面上
self.resultLable.text = [self.paramMutableArray firstObject];
self.status = kStatusDone;
self.pointStatus = NO;
[self.paramMutableArray removeAllObjects];
}
-(void)computeWithType:(kComputeType)type{
NSString *firstOperation;
NSString *secondOperation;
if (type == kComputeTypePrimary) {
firstOperation = @"+";
secondOperation = @"-";
}else{
firstOperation = @"*";
secondOperation = @"/";
}
for (int i = 0; i <self.operationMutableArray.count; i++) {
//获取i对应的运算符
NSString *opr = [self.operationMutableArray objectAtIndex:i];
//判断是不是*或者/
if ([opr isEqualToString:firstOperation]|| [opr isEqualToString:secondOperation]) {
double firstNum = [[self.paramMutableArray objectAtIndex:i]doubleValue];
double secondNum = [[self.paramMutableArray objectAtIndex:i+1]doubleValue];
double result = [self calculate:firstNum second:secondNum operation:opr];
//覆盖相应i对应的值
[self.paramMutableArray replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%g",result]];
//删除后面的一个数和符号
[self.paramMutableArray removeObjectAtIndex:i+1];
[self.operationMutableArray removeObjectAtIndex:i];
//i--
i--;
}
}
}
//计算结果
-(double)calculate:(double)firstNum
second:(double)secondNum
operation:(NSString*) operation{
//从字典中获取这个字符串的运算符对应的枚举值
kOperationType type = [[self.operationEnumDic objectForKey:operation]intValue];
double result = 0;
switch (type) {
case kOperationTypeAdd:
result = firstNum + secondNum;
break;
case kOperationTypeMinus:
result = firstNum - secondNum;
break;
case kOperationTypeMultiple:
result = firstNum * secondNum;
break;
case kOperationTypeDevide:
result = firstNum / secondNum;
break;
default:
break;
}
return result;
}
//重置计算器
- (IBAction)resetButtonDidClicked:(UIButton *)sender {
self.resultLable.text = @"0";
[self.paramMutableArray removeAllObjects];
[self.operationMutableArray removeAllObjects];
self.status = kStatusDone;
self.pointStatus = NO;
}
@end