• 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

  • 相关阅读:
    修改代码的艺术阅读笔记-01
    周总结
    代码整洁之道阅读笔记-03
    周总结
    mybatis三种执行器性能比较
    Tomcat长连接是如何实现的
    Zookeeper在Windows下搭建集群教程
    Zookeeper单机模式下RequestProcessor流程与源码理解
    JDK NIO基础概念与原理
    zookeeper客户端访问服务端时,基于NIO的线程池绑定
  • 原文地址:https://www.cnblogs.com/damonWq/p/5083533.html
Copyright © 2020-2023  润新知