[Stanford 2011] Ordinary Calculator(By myself)
说明:
前面的RPN计算器是按照stanford课程做的,是后缀表达式的计算。现在这个计算器是自己做的。这个是一般的计算器,即中缀表达式的计算,而且把计算过程也显示在屏幕上,
设计方法:
在Model里用了两个栈,一个是数字栈,一个是操作符栈。如果压入数字的话,检查操作符栈的最顶端元素是不是乘号和除号,如果是乘号和除号则出栈计算乘除结果;如果操作符栈栈顶是加减号,直接把数字压入数字栈。最终,在按下“等号键”计算结果时,操作符栈中只有加号和减号,此时对数字栈和操作符栈依次出栈计算最终结果。
Final “View” :
the codes of the “Model”:
// OrdinaryCalculatorBrain.h
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import <UIKit/UIKit.h> @interface OrdinaryCalculatorBrain : NSObject
-(void)pushOperation:(NSString *)operation; // + - * /
-(void)pushOperand:(double)operand;
-(double)equal;
-(void)clearAll;
@end // OrdinaryCalculatorBrain.m
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import "OrdinaryCalculatorBrain.h"
@interface OrdinaryCalculatorBrain()
@property (nonatomic,strong)NSMutableArray *numberStack;
@property(nonatomic,strong)NSMutableArray *operationStack;
@end @implementation OrdinaryCalculatorBrain
@synthesize numberStack = _numberStack;
@synthesize operationStack = _operationStack; -(NSMutableArray*)numberStack{
if (_numberStack==) {
_numberStack = [[NSMutableArray alloc]init];
}
return _numberStack;
}
-(NSMutableArray*)operationStack{
if(_operationStack==){
_operationStack = [[NSMutableArray alloc]init];
}
return _operationStack;
}
-(void)pushOperation:(NSString *)operation{ // + - * /
[self.operationStack addObject:operation]; }
-(void)pushOperand:(double)operand{
NSString *lastOperation = [self.operationStack lastObject];
if (lastOperation==nil || [lastOperation isEqualToString:@"+" ]||[lastOperation isEqualToString:@"-"]){
[self.numberStack addObject:[NSNumber numberWithDouble:operand]];
}
else if([lastOperation isEqualToString:@"*" ]||[lastOperation isEqualToString:@"/"]){
[self.operationStack removeLastObject];
double num2 = operand;
NSNumber *lastNumber = [self.numberStack lastObject];
if(lastNumber)[self.numberStack removeLastObject];
double num1 = [lastNumber doubleValue];
if ([lastOperation isEqualToString:@"*" ]) {
[self.numberStack addObject:[NSNumber numberWithDouble:num1*num2]];
}else if([lastOperation isEqualToString:@"/" ]){
[self.numberStack addObject:[NSNumber numberWithDouble:num1/num2]];
}
}
}
-(double)equal{
double result = ;
while ([self.operationStack lastObject]!=nil) {
NSString *lastOperation = [self.operationStack lastObject];
if (lastOperation) [self.operationStack removeLastObject];
double num1 = result;
NSNumber *lastNumber = [self.numberStack lastObject];
if(lastNumber)[self.numberStack removeLastObject];
double num2 = [lastNumber doubleValue];
if ([lastOperation isEqualToString:@"+" ]) {
result = num1+num2;
}else if([lastOperation isEqualToString:@"-" ]){
result = num1-num2;
}
}
NSNumber *lastNumber = [self.numberStack lastObject];
if(lastNumber)[self.numberStack removeLastObject];
double num2 = [lastNumber doubleValue];
result += num2;
return result; }
-(void)clearAll{
if (self.numberStack) {
[self.numberStack removeAllObjects];
}
if(self.operationStack){
[self.operationStack removeAllObjects];
}
}
@end
the codes of the “controller”:
// CalculatorViewController.h
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import <UIKit/UIKit.h> @interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display; @end // CalculatorViewController.m
// OrdinaryCalculator
//
// Created by Lvxy on 10/4/14.
// Copyright (c) 2014 Lvxy. All rights reserved.
// #import "CalculatorViewController.h"
#import "OrdinaryCalculatorBrain.h"
@interface CalculatorViewController ()
@property (nonatomic,strong)OrdinaryCalculatorBrain *brain;
@property(nonatomic)BOOL userIsInTheMiddleOfEnteringANumber;
@property(nonatomic)NSString *currentDigit;
@property(nonatomic)BOOL IsNotFirstPressed;
@end @implementation CalculatorViewController
@synthesize display = _display;
@synthesize brain = _brain;
@synthesize userIsInTheMiddleOfEnteringANumber=_userIsInTheMiddleOfEnteringANumber;
@synthesize IsNotFirstPressed=_IsNotFirstPressed;
-(OrdinaryCalculatorBrain*)brain{
if(_brain==nil)
_brain = [[OrdinaryCalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitPressed:(UIButton *)sender {
if(self.IsNotFirstPressed==){
self.display.text = @"";
self.IsNotFirstPressed = ;
} NSString *digit = sender.currentTitle;
self.display.text = [self.display.text stringByAppendingString:digit];
if (self.userIsInTheMiddleOfEnteringANumber) {
self.currentDigit = [self.currentDigit stringByAppendingString:digit];
}else{
self.currentDigit = digit;
self.userIsInTheMiddleOfEnteringANumber = ;
} } - (IBAction)operationPressed:(UIButton *)sender {
[self.brain pushOperand:[self.currentDigit doubleValue]];
self.currentDigit = @"";
self.display.text = [self.display.text stringByAppendingString:sender.currentTitle];
[self.brain pushOperation:sender.currentTitle];
self.userIsInTheMiddleOfEnteringANumber = ; }
- (IBAction)ClearPressed {
[self.brain clearAll];
self.userIsInTheMiddleOfEnteringANumber = ;
self.IsNotFirstPressed = ;
self.display.text = @"";
} - (IBAction)equalPressed:(UIButton *)sender {
[self.brain pushOperand:[self.currentDigit doubleValue]];
self.currentDigit = @"";
double result = [self.brain equal];
self.display.text = [[self.display.text stringByAppendingString:@"="]stringByAppendingString:[NSString stringWithFormat:@"%g",result]];
self.userIsInTheMiddleOfEnteringANumber = ;
} @end
[Stanford 2011] Ordinary Calculator(By myself)的更多相关文章
- (最长公共子序列+推导)Love Calculator (lightOJ 1013)
http://www.lightoj.com/volume_showproblem.php?problem=1013 Yes, you are developing a 'Love calcula ...
- LeetCode OJ:Basic Calculator(基础计算器)
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- bzoj 2438 [中山市选2011]杀人游戏(SCC+概率)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2438 [题意] N个人中有一个杀手,每次询问一个人可能被杀或被告知其认识的人中谁是杀手 ...
- Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结
Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.htm ...
- Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...
- (转载)Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...
- android 1.6 launcher研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )
2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup2011.06.03(2)——— android 1.6 launcher研究之自定义ViewG ...
- [BZOJ 2299][HAOI 2011]向量 题解(裴蜀定理)
[BZOJ 2299][HAOI 2011]向量 Description 给你一对数a,b,你可以任意使用(a,b), (a,-b), (-a,b), (-a,-b), (b,a), (b,-a), ...
- Stanford NLP 学习笔记2:文本处理基础(text processing)
I. 正则表达式(regular expression) 正则表达式是专门处理文本字符串的正式语言(这个是基础中的基础,就不再详细叙述,不了解的可以看这里). ^(在字符前): 负选择,匹配除括号以外 ...
随机推荐
- Time.deltaTime 增量时间
static var deltaTime : float Description描述 The time in seconds it took to complete the last frame (R ...
- C# 如何判断数据是否为 NaN
double a = 0 / 0d; if (double.IsNaN(a)){ //do } 在浮点数计算中, 0除以0将得到NaN ,正数除以0将得到PositiveInfinity ,负数除以0 ...
- TYVJ P1091 等差数列 Label:dp
背景 广东汕头聿怀初中 Train#3 Problem 3 描述 等差数列的定义是一个数列S,它满足了(S[i]-S[i-1]) = d (i>1).显然的一个单独的数字或者两个数字也可以形成一 ...
- 【BZOJ】3809: Gty的二逼妹子序列
http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题意:n个元素(1<=n<=100000)每个元素有一权值<=n.q个询问, ...
- 字符串分割与存入List集合
List<string> namelist = new List<string>(); string[] namejh = null; string name= "张 ...
- for循环计算游戏通关分数
一个游戏1到20关是成绩是自身关卡数的成绩,21-30每关10分,31-40每关20分,41-49每关30分,50关100分.输入一个关数求成绩 代码如下: <!DOCTYPE html PUB ...
- 网易前端JavaScript编码规范【转】
1. 变量命名规范 变量名包括全局变量,局部变量,类变量,函数参数等等,他们都属于这一类. 基本规范 变量命名都以类型前缀+有意义的单词组成,单词首字母都需要大写.例如:sUserName,nCoun ...
- 文件上传(springMVC+ckeditor)
1.首先添加springMVC文件上传的jar commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar (maven项目可以使用 <dependenc ...
- 嵌入式 如何定位死循环或高CPU使用率(linux) 及性能优化
嵌入式 如何定位死循环或高CPU使用率(linux) ln -s /mnt/nfs/_install/usr/bin/sort /usr/bin/sort awk '{print $1,$2,$14, ...
- Ubuntu 开启 Crontab 计划任务日志
Ubuntu 下的 Crontab 日志功能不是自动开启的.开启日志功能的步骤是: ① 修改 rsyslog sudo vim /etc/rsyslog.d/-default.conf 找到: #cr ...