混合运算

#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 *resultLabel;

@property (nonatomic, strong) NSMutableArray *paramMutableArray;//存放每个数字

@property (nonatomic, strong) NSMutableArray *operationMutableArray;//存放运算符

@property (nonatomic, assign) kStatus status;

@property (nonatomic, strong) NSDictionary *opertationEnumDic;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //初始化数组

    self.paramMutableArray = [NSMutableArray array];

    self.operationMutableArray = [NSMutableArray array];

    self.status = kStatusNum;

    

    self.opertationEnumDic = @{@"+":@1, @"-":@2, @"*":@3, @"/":@4};

}

//数字按钮按下了

- (IBAction)normalButtonDidCliked:(UIButton *)sender {

    //获取按钮上面的数字

    int num = [sender.titleLabel.text intValue];

    long long showNum;

    

    //判断是否是一个新的数字的开始

    if (self.status == kStatusNum){

        //获取label上显示的之前的数字

        long long orgNum = [self.resultLabel.text longLongValue];

        

        //显示计算的结果

        showNum = orgNum * 10 + num;

    } else{

        //判断之前是否有结果 如果有结果,我们将丢弃这个结果

        if (self.status == kStatusDone) {

            [self.paramMutableArray removeAllObjects];

        }

        //这是一个新的数字

        showNum = num;

        self.status = kStatusNum;

    }

    self.resultLabel.text = [NSString stringWithFormat:@"%lld", showNum];

}

- (IBAction)operationButtonDidClicked:(id)sender {

    //将id类型的对象转化为本身的类型

    UIButton *btn = (UIButton *)sender;

    

    //判断是不是重复按下操作符了

    if (self.status != kStatusOperation){

        //改变当前的状态 通知上面的方法,开始输入新德数字了

        self.status = kStatusOperation;

        

        //这个数字结束了

        //将这个数字保存到数组里面

        [self.paramMutableArray addObject:self.resultLabel.text];

    

        //保存当前点击按钮上面的title

        [self.operationMutableArray addObject:btn.titleLabel.text];

    } else{

        //已经重复按下了 12 + -

        //用新的运算符替换原来的那个 == 替换最后一个

        //1.获取当前的这个操作符

        NSString *newOperation = btn.titleLabel.text;

        

        //2. 获取最后一个的索引值

        NSInteger lastIndex = self.operationMutableArray.count - 1;

        

        [self.operationMutableArray replaceObjectAtIndex:lastIndex withObject:newOperation];

    }

}

//=号

- (IBAction)calculate:(UIButton *)sender {

    //添加当前的最后一个数字

    [self.paramMutableArray addObject:self.resultLabel.text];

    NSLog(@"%@", self.paramMutableArray);

    NSLog(@"%@", self.operationMutableArray);

    

    [self computeWithType:kComputeTypeSenior];

    [self computeWithType:kComputeTypePrimary];

    

    //将结果显示到界面上

    self.resultLabel.text = [self.paramMutableArray firstObject];

    

    self.status = kStatusDone;

    

    [self.paramMutableArray removeAllObjects];

    

    NSLog(@"%@", self.paramMutableArray);

    NSLog(@"%@", self.operationMutableArray);

}

- (void)computeWithType:(kComputeType)type{

    NSString *firstOperation;

    NSString *seconOpertion;

    if (type == kComputeTypePrimary) {

        firstOperation = @"+";

        seconOpertion = @"-";

    } else {

        firstOperation = @"*";

        seconOpertion = @"/";

    }

    

    //12 + 2 * 3 - 4 / 2 + 89 / 3

    //开始计算

    //将运算符数组里面的* /运算

    for (int i = 0; i < self.operationMutableArray.count; i++){

        //获取i对应的运算符

        NSString *opr = [self.operationMutableArray objectAtIndex:i];

        

        //判断是不是* 或者 /

        if ([opr isEqualToString:firstOperation] || [opr isEqualToString:seconOpertion]) {

            //获取即将进行运算的两个数

            NSString *firstString = [self.paramMutableArray objectAtIndex:i];

            NSString *secondString = [self.paramMutableArray objectAtIndex:i+1];

            

            int firstNum = [firstString intValue];

            int secondNum = [secondString intValue];

            

            int result = [self compute:firstNum second:secondNum operation:opr];

            

            //覆盖i对应的值

            [self.paramMutableArray replaceObjectAtIndex:i withObject:[NSString stringWithFormat:@"%d", result]];

            

            //删除后面的一个数

            [self.paramMutableArray removeObjectAtIndex:i+1];

            

            //删除i对应的那个运算符

            [self.operationMutableArray removeObjectAtIndex:i];

            

            //将i--

            i--;

        }

    }

}

//计算结果

- (int)compute:(int)firstNum second:(int)secondNum operation:(NSString *)operation{

    //从字典里面获取这个字符串的运算符对应的枚举值

    kOperationType type = [[self.opertationEnumDic objectForKey:operation] intValue];

    

    int reslut = 0;

    switch (type) {

        case kOperationTypeAdd:

            reslut = firstNum + secondNum;

            break;

        case kOperationTypeMinus:

            reslut = firstNum - secondNum;

            break;

        case kOperationTypeMultiple:

            reslut = firstNum * secondNum;

            break;

        case kOperationTypeDevide:

            reslut = firstNum / secondNum;

            break;

        default:

            break;

    }

    return reslut;

}

//清空

- (IBAction)clearButtonDidClicked:(UIButton *)sender {

    self.resultLabel.text = @"0";

    [self.paramMutableArray removeAllObjects];

    [self.operationMutableArray removeAllObjects];

    self.status = kStatusNum;

}

@end

原文地址:https://www.cnblogs.com/liuzhicen/p/5090612.html