[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) 正则表达式是专门处理文本字符串的正式语言(这个是基础中的基础,就不再详细叙述,不了解的可以看这里). ^(在字符前): 负选择,匹配除括号以外 ...
随机推荐
- BZOJ3165 : [Heoi2013]Segment
建立线段树,每个节点维护该区间内的最优线段. 插入线段时,在线段树上分裂成$O(\log n)$棵子树,若与当前点的最优线段不相交,那么取较优的,否则暴力递归子树. 查询时在叶子到根路径上所有点的最优 ...
- FLTK 1.1.10 VS2010 Configuration 配置
Download FLTK 1.1.10 at here. Download VS2010 Download CMake 2.8.12 I assume you've already installe ...
- [转] - QBuffer类参考
QBuffer类参考 QBuffer类是一个操作QByteArray的输入/输出设备. 详情请见…… #include <qbuffer.h> 继承了QIODevice. 所有成员函数的列 ...
- 两个List,第二个List根据第一个List排序
/// <summary> /// 协同排序 /// </summary> /// <param name="sod"></param&g ...
- 转载:hdu 动态规划题集
1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包;第一次做的时候把概率当做背包(放大100000倍化为整数): ...
- HDU 2594 Simpsons’ Hidden Talents(KMP的Next数组应用)
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 【转】C#文件操作大全
文件与文件夹操作主要用到以下几个类: 1.File类: 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. msdn:http://msdn.microsof ...
- 2016.07.14,英语,《Vocabulary Builder》Unit 25
verb: comes from the Latin verbum, meaning 'word'. verbally: ['vɜːbəli] adv. 口头地,词句地, 逐字地 verbalize: ...
- Apache Spark源码走读之10 -- 在YARN上运行SparkPi
y欢迎转载,转载请注明出处,徽沪一郎. 概要 “spark已经比较头痛了,还要将其运行在yarn上,yarn是什么,我一点概念都没有哎,再怎么办啊.不要跟我讲什么原理了,能不能直接告诉我怎么将spar ...
- uitextfield输入字符限制
-(UITextField*)createField:(NSString*)placeholder andTag:(int)tag andFont:(double)font{ UITextField ...