zoukankan      html  css  js  c++  java
  • 1228.1——计算器(未使用MVC设计模式)

    #import "ViewController.h"
    typedef enum{
        kStausNum,
        kStausOperation
    }kStaus;

    typedef enum{
        kOperationTypeAdd = 1,
        kOperationTypeMinus,
        kOperationTypeMultiply,
        kOperationTypeDevide,
        kOperationTypeEqual,
        kOperationTypeNone
    }kOperationType;

    @interface ViewController ()
    @property (weak, nonatomic) IBOutlet UILabel *result_label;
    @property (nonatomic,assign) long long firstParam; //记录第一个参数
    @property (nonatomic,assign) kOperationType lastOperation;//保存上一次的操作数
    @property (nonatomic,assign) BOOL status;//记录当前的状态

    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        //赋初值
        self.firstParam = 0;
        self.lastOperation = kOperationTypeNone;
        self.status = kStausNum;
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    - (IBAction)numButtonDidClicked:(UIButton *)sender {
        //获取点击的按钮上的数字
        int num = [sender.titleLabel.text intValue];
        //判断继续拼接还是单独显示
        long long showNum;
        if (self.status == kStausNum) {
            //获取之前显示的结果
        long long orgNum = [self.result_label.text longLongValue];
        
        //计算最终显示结果
        showNum = orgNum * 10 + num;
        }else{
            showNum = num;
            self.status = kStausNum;
        }
            
        //显示结果
        self.result_label.text = [NSString stringWithFormat:@"%lld",showNum];
        
        }

    //操作键+-*÷
    - (IBAction)operationButtonDidClicked:(UIButton *)sender {
        if (self.status != kStausOperation) {
            self.status = kStausOperation;
        }
        self.status = kStausOperation;
        //有两种情况
        //1.第一次操作,只需保存只一次操作
        //2.前面有操作需要计算,并保存这一次的操作
        //3.判断是不是第一次操作
        if (self.lastOperation != kOperationTypeNone) {
            //2.前面有操作需要计算,保存这次操作
            [self calculate];
        }else{
            //第一个参数输入完毕 保存
            self.firstParam = [self.result_label.text longLongValue];
        }
        //保存这一次操作
        if (sender.tag == kOperationTypeEqual) {
            self.lastOperation = kOperationTypeNone;
        }else{
            self.lastOperation = (kOperationType)sender.tag;//自定义tag的值
        }
        
    }

    //计算结果
    -(void)calculate{
        int secondParam = [self.result_label.text intValue];
        long long result;
        
        switch (self.lastOperation) {
            case kOperationTypeAdd:
                result = self.firstParam + secondParam;
                break;
            case kOperationTypeMinus:
                result = self.firstParam - secondParam;
                break;
            case kOperationTypeMultiply:
                result = self.firstParam * secondParam;
                break;
            case kOperationTypeDevide:
                result = self.firstParam / secondParam;
                break;
            default:
                break;
        }
        //显示最终结果
        self.result_label.text = [NSString stringWithFormat:@"%lld",result];
        
        //当前的结果就是下一次的第一个参数的值
        self.firstParam = result;
        
        //更改操作符
        self.lastOperation = kOperationTypeNone;
    }

    - (IBAction)resetButtonDidClicked:(UIButton *)sender {
        self.result_label.text = @"0";
        self.lastOperation = kOperationTypeNone;
        self.status = kStausNum;
    }

    @end

  • 相关阅读:
    SQL解发器与SQL游标实例
    动态调用JS
    HDU_5729_rmq+二分
    struts2 在MyEclipse中 的配置
    Struts 1.2 中如何测试Action
    OGNL使用小结【转】
    JUnit中assertEquals和assertSame方法的不同
    struts2 ActionContext
    ser文件与Java对象序列化
    测试Action组件代码(StrutsTestCase)
  • 原文地址:https://www.cnblogs.com/damonWq/p/5083533.html
Copyright © 2011-2022 走看看