zoukankan      html  css  js  c++  java
  • 混合运算

    #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

  • 相关阅读:
    Java设计模式系列之策略模式
    设计模式系列之热身
    算术表达式系列之后缀表达式求值
    算术表达式系列之中缀表达式转后缀表达式
    Maven下使用Junit对Spring进行单元测试
    Windows命令行使用总结(持续更新)
    Eclipse中web项目部署至Tomcat步骤
    MyBatis保存完整日期的解决方法
    Redis(一)源码安装
    【集成学习】sklearn中xgboost模块中plot_importance函数(绘图--特征重要性)
  • 原文地址:https://www.cnblogs.com/liuzhicen/p/5090612.html
Copyright © 2011-2022 走看看