IOS基础开发二(iphone计算器)
来源:互联网 发布:我的世界java官方版 编辑:程序博客网 时间:2024/05/21 11:37
今天做了个iphone的小例子计算器:才用mvc设计模式
项目目录:
代码如下:
CalculatorViewController:(MVC的控制器)
// CalculatorViewController.h// Calculator//// Created by WildCat on 13-7-28.// Copyright (c) 2013年 wildcat. All rights reserved.//#import <UIKit/UIKit.h>@interface CalculatorViewController : UIViewController@property (weak, nonatomic) IBOutlet UILabel *display;@end
//// CalculatorViewController.m// Calculator//// Created by WildCat on 13-7-28.// Copyright (c) 2013年 wildcat. All rights reserved.//#import "CalculatorViewController.h"#import "CalculatorBrain.h"@interface CalculatorViewController()@property (nonatomic) BOOL userIsinTheMiddleOfEnteringANumber;@property(nonatomic,strong)CalculatorBrain *brain;@end@implementation CalculatorViewController@synthesize display=_display;@synthesize userIsinTheMiddleOfEnteringANumber=_userIsinTheMiddleOfEnteringANumber;@synthesize brain=_brain;-(CalculatorBrain *)brain{ if(!_brain) _brain=[[CalculatorBrain alloc] init]; return _brain;}- (IBAction)digitPressed:(UIButton *)sender { NSString *digit=[sender currentTitle]; //建一个局部变量 if (self.userIsinTheMiddleOfEnteringANumber) { self.display.text=[self.display.text stringByAppendingString:digit]; }else{ self.display.text=digit; self.userIsinTheMiddleOfEnteringANumber=YES; } }- (IBAction)operationPressed:(UIButton *)sender { if ([self userIsinTheMiddleOfEnteringANumber]) { [self enterPressed]; } double result=[self.brain performOperation:sender.currentTitle]; NSString *resultString=[NSString stringWithFormat:@"%g",result]; self.display.text=resultString; }- (IBAction)enterPressed { [self.brain pushOperand:[self.display.text doubleValue]]; self.userIsinTheMiddleOfEnteringANumber=NO; }- (IBAction)enterDEL:(id)sender { NSString *nowstr=self.display.text; int len=nowstr.length; int nowlength=len-1; if (nowlength>=1) { self.display.text=[nowstr substringWithRange:NSMakeRange(0, nowlength)]; }else{ self.display.text=@"0"; } }@endCalculatorBrain(计算器大脑mvc中的model)
// CalculatorBrain.h// Calculator//// Created by WildCat on 13-7-28.// Copyright (c) 2013年 wildcat. All rights reserved.//#import <Foundation/Foundation.h>@interface CalculatorBrain : NSObject-(void)pushOperand:(double)operand;-(double)performOperation:(NSString *)operation;@property (readonly)id program;+(double) runProgram:(id) program;+(NSString *)discriptionOfProgram:(id) program;@end//// CalculatorBrain.m// Calculator//// Created by WildCat on 13-7-28.// Copyright (c) 2013年 wildcat. All rights reserved.//#import "CalculatorBrain.h"//定义一个私有的栈@interface CalculatorBrain()@property (nonatomic,strong)NSMutableArray *programStack;@end@implementation CalculatorBrain@synthesize programStack=_programStack;//ProgramStack的setter and getter............-(NSMutableArray *)programStack{ if (_programStack==nil) { _programStack=[[NSMutableArray alloc] init]; } return _programStack;}-(void)setProgramStack:(NSMutableArray *)programStack{ _programStack=programStack;}-(double)performOperation:(NSString *)operation{ [self.programStack addObject:operation]; return [CalculatorBrain runProgram:self.program];}-(id)program{ return [self.programStack copy];}//入栈-(void)pushOperand:(double)operand{ [self.programStack addObject:[NSNumber numberWithDouble:operand]];}//出栈-(double)popOperand{ NSNumber *number=[self.programStack lastObject]; if (number) { [self.programStack removeLastObject]; } return [number doubleValue];}+(double)popOperandOffStack:(NSMutableArray *)stack{ double result=0; id topOfStack=[stack lastObject]; if (topOfStack) { [stack removeLastObject]; } if ([topOfStack isKindOfClass:[NSNumber class]]){ result=[topOfStack doubleValue]; }else if ([topOfStack isKindOfClass:[NSString class]]){ NSString*operation=topOfStack; if ([@"+" isEqualToString:operation]) { result=[self popOperandOffStack:stack]+[self popOperandOffStack:stack]; }else if ([@"*" isEqualToString:operation]){ result=[self popOperandOffStack:stack]*[self popOperandOffStack:stack]; }else if ([@"-" isEqualToString:operation]){ double firstnumber=[self popOperandOffStack:stack]; double secondnumber=[self popOperandOffStack:stack]; result=secondnumber-firstnumber; }else if([@"/" isEqualToString:operation]){ double firstnumber=[self popOperandOffStack:stack]; double secondnumber=[self popOperandOffStack:stack]; if(firstnumber)result=secondnumber/firstnumber; } } return result;}+(double) runProgram:(id) program{ NSMutableArray *stack; if ([program isKindOfClass:[NSArray class]]) { stack=[program mutableCopy]; } return [self popOperandOffStack:stack];}+(NSString *)discriptionOfProgram:(id) program{ return @"Implament this in Assignment2";}@end
效果图:
- IOS基础开发二(iphone计算器)
- IOS基础开发二(iphone计算器)
- IPhone开发之C基础(二)
- Iphone开发基础篇(二)-ObjectC之面向对象
- Objective-C ,ios,iphone开发基础:协议(protocol)
- iOS应用开发基础(二)基础知识
- 7.26 iOS 基础(加法计算器布局)
- ios开发简单计算器
- ios基础(二)
- ios开发/iphone开发 (收藏)
- iphone开发的基础二类的实现
- 仿微信/QQ 开发基于XMPP的iPhone/iOS聊天客户端(二)修改密码
- Objective-C,ios,iphone开发基础:分类(category,又称类别)
- Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明
- ios基础总结之iPhone开发内存管理
- Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
- Objective-C ,ios,iphone开发基础:自定义控件:Eg: UIButton
- Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary
- inputStream 数据分段传输问题
- 开发那点事1
- CoroutineScheduler
- 以公司实际应用讲解OpenStack到底是什么
- lua(1)
- IOS基础开发二(iphone计算器)
- 程序编译运行时头文件或动态链接库的查找
- 受限的玻尔兹曼机
- 关于Android XML文件的解析方式DOM,SAX,Pull的具体实现。
- 黑马程序员——String str1="abc";String str2=new String("abc");内部原理剖析
- experiment : test CreateProcessWithLogonW
- spoj 4491. Primes in GCD Table 莫比乌斯反演
- 大型企业为何要用腾讯通RTX?甚至很多政府机关也在用RTX
- 实现文本的 复制 粘贴 的创建