[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) 正则表达式是专门处理文本字符串的正式语言(这个是基础中的基础,就不再详细叙述,不了解的可以看这里). ^(在字符前): 负选择,匹配除括号以外 ...
随机推荐
- iphone6来了,我该做点什么(兼容iphone6的方法)
北京时间2014年9月10日凌晨1点,苹果公司正式发布其新一代产品 iPhone6,相信做webapp开发的同学对它是充满了好奇和等待,也担心它带来各种坑爹,高清的分辨率,升级的retina显示屏,我 ...
- 【BZOJ】1049: [HAOI2006]数字序列(lis+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1049 题意:给一个长度为n的整数序列.把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希 ...
- Graph database_neo4j 底层存储结构分析(5)
3.5 Property 的存储 下面是neo4j graph db 中,Property数据存储对应的文件: neostore.propertystore.db neostore.propertys ...
- python 面向对象的三大特征之 继承
#继承 #object 基类,是python定义的所有类的父类 #经典类:不继承object的类称作经典类 #新式类:继承object的类称作新式类 #python 3.x统一为新式类 #经典类是类对 ...
- 在openGL中绘制图形
点的绘制.: glVertex*():星号表示函数要有后缀 该函数 需要放在glBegin函数和glEnd函数之间,glBegin函数的向量指定绘制图元的类型,而glEnd函数没有参数,例如: glB ...
- php isset() empty() 区别, 判断 变量存在与否神器
先看PHP手册: bool empty ( mixed $var ) 判断一个变量是否被认为是空的.当一个变量并不存在,或者它的值等同于FALSE,那么它会被认为不存在.如果变量不存在的话,empty ...
- 每天php函数 - list()给一组变量赋值
array list ( mixed $varname [, mixed $... ] ) 像 array() 一样,这不是真正的函数,而是语言结构.list()用一步操作给一组变量进行赋值. var ...
- MySQL 日期和时间戳互相转换
① 时间戳转换成日期 FROM_UNIXTIME 例如: 数据表中 invest_time 存储的是时间戳,如 1429063399 使用 FROM_UNIXTIME 可以把时间戳转换为日期: sel ...
- PHP调用java的class
PHP调用java的class 转:http://hi.baidu.com/lei0827/blog/item/28439a4e923234ced1c86a18.html PHP调用java的cl ...
- MVC 扩展方法特点
.NET MVC 3中扩展方法特点: (1)扩展类的名称以Extensions结尾: (2)扩展类的类型是static: (3)扩展方法至少有一个参数,第一个参数必须指定该方法作用于哪个类型,并且该参 ...