博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
first ios app
阅读量:4565 次
发布时间:2019-06-08

本文共 3716 字,大约阅读时间需要 12 分钟。

calculatorViewController.m

1 #import "CalculatorViewController.h" 2 #import "CalculatorBrain.h" 3  4 @interface CalculatorViewController ()//create private varible 5 @property (nonatomic) BOOL useIsInTheMiddleOfEnteringANumber; 6  7 @property (nonatomic, strong) CalculatorBrain *brain; 8 @end 9 10 @implementation CalculatorViewController11 12 @synthesize display = _display;13 @synthesize useIsInTheMiddleOfEnteringANumber = _useIsInTheMiddleOfEnteringANumber;14 @synthesize brain = _brain;15 16 - (CalculatorBrain *)brain  //setter17 {18     if(!_brain) _brain = [[CalculatorBrain alloc] init];19     return _brain;20 }21 22 23 - (IBAction)digitPressed:(UIButton *)sender24 {25     NSString *digit = sender.currentTitle;26     if(self.useIsInTheMiddleOfEnteringANumber) {27         self.display.text = [self.display.text stringByAppendingString:digit]; 28     } else {29         self.display.text = digit;30         self.useIsInTheMiddleOfEnteringANumber = YES;31     }32     33     34 /*35     UILabel *myDisplay = self.display;  //[self display];  getter36     NSString *CurrentText = [myDisplay text];37     NSString *newText = [CurrentText stringByAppendingString:digit];38     myDisplay.text = newText;//[myDisplay setText:newText]; setter39     */40 41     42     43 }44 45 - (IBAction)doublePressed:(UIButton *)sender {46     47     double result = [self.brain performOperation:sender.currentTitle];48     NSString *resultString = [NSString stringWithFormat:@"%g", result];49     self.display.text = resultString;50 }51 52 - (IBAction)operationPressed:(UIButton *)sender 53 {54     55     if(self.useIsInTheMiddleOfEnteringANumber) [self enterPressed];56     double result = [self.brain performOperation:sender.currentTitle];57     NSString *resultString = [NSString stringWithFormat:@"%g", result];58     self.display.text = resultString;59     60 }61 62 - (IBAction)enterPressed63 {64     [self.brain pushOperand:[self.display.text doubleValue]];65     self.useIsInTheMiddleOfEnteringANumber = NO;66 }67 68 69 @end

viewController.h

 

1 #import 
2 3 @interface CalculatorViewController : UIViewController4 5 @property (weak, nonatomic) IBOutlet UILabel *display;6 7 @end

 

Brain.h

1 #import 
2 3 @interface CalculatorBrain : NSObject// model 4 5 6 - (void)pushOperand: (double)operand; 7 8 - (double) performOperation:(NSString *)operation; 9 10 @end

Brain.m

1 #import "CalculatorBrain.h" 2 @interface CalculatorBrain() 3 @property (nonatomic, strong) NSMutableArray *operandStack; 4 @end 5  6  7 @implementation CalculatorBrain 8  9 10 @synthesize operandStack = _operandStack;11 12 - (NSMutableArray *)operandStack//getter13 {14     if(_operandStack == nil) _operandStack = [[NSMutableArray alloc] init];15     return _operandStack;16 }17 18 19 - (void)pushOperand: (double)operand20 {21    22     [self.operandStack addObject:[NSNumber numberWithDouble:operand]];//must add double23     24 }25 26 - (double) popOperand27 {28     NSNumber *operandObject = [self.operandStack lastObject];29     if(operandObject != nil) [self.operandStack removeLastObject];30     return [operandObject doubleValue];31 }32 33 - (double) performOperation:(NSString *)operation34 {35     double result = 0;36     if([operation isEqualToString:@"+"]) {37         result = [self popOperand] + [self popOperand];38     } else if ([@"*" isEqualToString:operation]) {39         result = [self popOperand] * [self popOperand];40     } else if ([@"^" isEqualToString:operation]) {41         double d = [self popOperand];42         result =  d * d;43     }44     [self pushOperand:result];45     return result;46 }47 48 @end

 

 

screen 

转载于:https://www.cnblogs.com/Matrix54/archive/2012/09/15/2686625.html

你可能感兴趣的文章
字符串与整型的转换及判断
查看>>
Aapache 启动不了,报错信息:suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
查看>>
微信小程序 JS动态修改样式
查看>>
天纬思创网络综合信息管理系统 V1.0 后台
查看>>
OC-->NSMutableString常用方法
查看>>
hive 分区表
查看>>
观影计划:漫威电影宇宙「无限战争」系列
查看>>
用番茄工作法提升工作效率 (二)用番茄钟实现劳逸结合(简单到不可相信)...
查看>>
1.3 排序显示数据
查看>>
在form上设定了defaultbutton属性之后,切换提交按钮的解决办法
查看>>
HTMl5视频播放
查看>>
struts2标签之列求和
查看>>
《面向对象程序设计课程学习进度条》
查看>>
oracle 迁移数据文件( 转)
查看>>
mybatis association和collection标签怎么用
查看>>
一个最简单的Delphi2010的PNG异形窗口方法
查看>>
怎样关闭拼写错误标记
查看>>
常见COM问题解答
查看>>
用到的linux命令
查看>>
【词云】代码
查看>>