1230.1——可连续计算的计算器

代码部分及注释

#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

时间: 2024-10-05 20:22:43

1230.1——可连续计算的计算器的相关文章

用python40行代码编写的计算器

偶尔用脚本写点东西也是不错的 效果图 代码 from tkinter import * reset=True def buttonCallBack(event): global label global reset num=event.widget['text'] if num=='C': label['text']="0" return if num in "=": label['text']=str(eval(label['text'])) reset=True

HDUOj 简单计算器

 /*简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14933    Accepted Submission(s): 5087 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过20

hdu1237 简单计算器 (模拟+栈)

简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15075    Accepted Submission(s): 5132 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,

C++.NET的简易计算器的制作

计算器的制作需要实现一下几个功能:加减乘除,连续计算,重复计算. 加减乘除就是简单的二元运算,连续计算就是不使用等号连续进行几次二元运算,重复计算就是进行一次二元运算之后再次单击等号可以将之前的运算再次进行一次. 由于是C++的窗体程序,所以先设计出窗体的界面.界面如下. 需要注意的是,上面的两个显示框用的是TextBox,其余的均是Button. 窗体的FormBorderStyle属性应改成FixedSingle或其他,不能用None,这个样子计算器窗体框的大小就是固定不可变的. Maxim

自己做的javascript简易计算器

html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>javascript任务2-简易计算器</title> <link rel="stylesheet" href="style/demo.css"> </head> <body&g

将Python当作计算器

在交互模式中,最近一个表达式的值赋给变量 _.这样我们就可以把它当作一个桌面计算器,很方便的用于连续计算.例如: >>> price = 1.25 #声明变量price >>> price * 10 #最近的表达式 12.5 >>> price +_ #等价于1.25+12.5 13.75 >>> round(_,1) #等价于round(13.75,1) 13.8

【自动化__GUI自动化】__java__Windows应用程序识别__计算器

一.代码如下 package www.woniu.gui.one; import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.aw

bzoj2242: [SDOI2011]计算器.

2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 4353  Solved: 1679[Submit][Status][Discuss] Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数. In

j2ee-JSP之简单计算器

来源韩顺平.j2ee视频实战教程jsp第1讲(下集) -------------------------------------------------------------------------------------------------------- 简单计算器,可以控制输入的数(仅第一个数)不能为空且不能为字符串 myCal.jsp代码 1 <!--这是计算器的界面 --> 2 <!-- 可以控制输入的数不能为空且不能为字符串 --> 3 <%@ page co