说明:

前面的RPN计算器是按照stanford课程做的,是后缀表达式的计算。现在这个计算器是自己做的。这个是一般的计算器,即中缀表达式的计算,而且把计算过程也显示在屏幕上,

设计方法:

在Model里用了两个栈,一个是数字栈,一个是操作符栈。如果压入数字的话,检查操作符栈的最顶端元素是不是乘号和除号,如果是乘号和除号则出栈计算乘除结果;如果操作符栈栈顶是加减号,直接把数字压入数字栈。最终,在按下“等号键”计算结果时,操作符栈中只有加号和减号,此时对数字栈和操作符栈依次出栈计算最终结果。

FinalView”

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)的更多相关文章

  1. (最长公共子序列+推导)Love Calculator (lightOJ 1013)

    http://www.lightoj.com/volume_showproblem.php?problem=1013   Yes, you are developing a 'Love calcula ...

  2. LeetCode OJ:Basic Calculator(基础计算器)

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  3. bzoj 2438 [中山市选2011]杀人游戏(SCC+概率)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2438 [题意] N个人中有一个杀手,每次询问一个人可能被杀或被告知其认识的人中谁是杀手 ...

  4. Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

    Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.htm ...

  5. Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)

    MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...

  6. (转载)Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)

    MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...

  7. 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 ...

  8. [BZOJ 2299][HAOI 2011]向量 题解(裴蜀定理)

    [BZOJ 2299][HAOI 2011]向量 Description 给你一对数a,b,你可以任意使用(a,b), (a,-b), (-a,b), (-a,-b), (b,a), (b,-a), ...

  9. Stanford NLP 学习笔记2:文本处理基础(text processing)

    I. 正则表达式(regular expression) 正则表达式是专门处理文本字符串的正式语言(这个是基础中的基础,就不再详细叙述,不了解的可以看这里). ^(在字符前): 负选择,匹配除括号以外 ...

随机推荐

  1. LightOJ1031 Easy Game(区间DP)

    我可能真想不到这题是区间DP,不过知道是区间DP想了下就AC了. dp[i][j]表示局面为ai...aj先手能获得与后手得分的最大差值 那么转移到当前状态就是枚举中间的位置,分成两边,其中一边先手全 ...

  2. Drainage Ditches

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. c++ auto_ptr 智能指针

    c++使用智能指针应该保证无论在何种情况下,只要自己被摧毁,就一定连带释放其所有资源,而由于智能型指针本身就是区域变量, 所以无论是正常退出,还是异常退出,只要函数退出,它就一定销毁 常数型auto_ ...

  4. css 框架——base.css,作用是重设浏览器默认样式和提供通用原子类。自己留存

    今天发下我自己的 css 框架——base.css,作用是重设浏览器默认样式和提供通用原子类. @charset "utf-8"; /*! * @名称:base.css * @功能 ...

  5. iOS5中UIViewController的新方法

    iOS5中UIViewController的新方法 前言 在苹果的 WWDC2011 大会视频的<Session 101 - What's New in Cocoa> 和<Sessi ...

  6. html标签,格式控制标签,内容容器标签,超链接标签,图片标签,表格

    打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background  背景壁纸.图片 text  文字颜色 topmargin  上边距 leftm ...

  7. [转]ASP.NET会话(Session)保存模式

    本文转自:http://blog.csdn.net/cityhunter172/article/details/727743 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦 ...

  8. 3种用组策略将域帐号加入本地管理员组的方法_jinifly_新浪博客

    次当前系统域帐号是怎么在第一次登录时,自动加入域客户端本地管理员组的?我猜不外乎就是脚本.计算机策略或虚拟机初始化的自动应答脚本,结果系统的前任同事找到了答案--GPO的用户策略(确切讲是用户首选项) ...

  9. Responsive布局技巧

    在Responsive布局中,可以毫无保留的丢弃: 第一, 尽量少用无关紧要的div: 第二,不要使用内联元素(inline): 第三,尽量少用JS或flash: 第四,丢弃没用的绝对定位和浮动样式: ...

  10. scapy 安装及简单测试

    关于scapy Scapy的是一个强大的交互式数据包处理程序(使用python编写).它能够伪造或者解码大量的网络协议数据包,能够发送.捕捉.匹配请求和回复包等等.它可以很容易地处理一些典型操作,比如 ...