整个项目下载:https://github.com/junxianhu/Calculator,觉得有帮助的可以点击Star啊,谢谢啦。


界面不太好看  ==!

斯坦福大学iOS应用开发教程学习笔记(第二课) 计算器实现(mvc实战)_#import


主要的文件目录如下:

斯坦福大学iOS应用开发教程学习笔记(第二课) 计算器实现(mvc实战)_sed_02


贴几个关键的文件,其实注视都很详细,可以下载下来仔细看:


ViewController.h


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

//为什么使用weak,因为UIlabel已经有个strong指针了,在父窗口view已经有strong指向它了,只需要它在view里面,所以weak //线程非安全
@property (weak, nonatomic) IBOutlet UILabel *display;

@end


ViewController.m

#import "ViewController.h"
#import "CalculatorBrain.h"

//private
@interface ViewController()
@property (nonatomic) BOOL userIsInTheMIdddlOfEnteringANumber;
//用来指向model
@property(nonatomic,strong) CalculatorBrain *brain;
@end


@implementation ViewController

@synthesize display= _display;
@synthesize userIsInTheMIdddlOfEnteringANumber = _userIsInTheMIdddlOfEnteringANumber;
@synthesize brain=_brain;

//延迟实例化
-(CalculatorBrain *)brain{
    if (!_brain) {
        _brain = [[CalculatorBrain alloc]init];
    }
    return _brain;
}


//IBAction:void 标记为action id:可以指向任何类型的指针
//更改id为UIButton类型即可
- (IBAction)digitPressed:(UIButton *)sender {
    
    NSString *digit =  [sender currentTitle];
    //NSLog(@"digit pressed = %@",digit);
    
//    UILabel *myDisplay = self.display;//[self display];
//    NSString *currentText = [myDisplay text];
//    NSString *newText = [currentText stringByAppendingString:digit];
//    myDisplay.text = newText;
//    上面4行精简一下代码为
//    self.display.text = [self.display.text stringByAppendingString:digit
    
    if (self.userIsInTheMIdddlOfEnteringANumber) {
        self.display.text = [self.display.text stringByAppendingString:digit];
    } else {
        self.display.text = digit;
        self.userIsInTheMIdddlOfEnteringANumber =  YES;
    }
}

//先按2个数字 然后按操作符
//所以这里按下操作符就是要计算结果了
- (IBAction)operationPressed:(UIButton *)sender {
    
    if (self.userIsInTheMIdddlOfEnteringANumber) {
        [self enterPressed];
    }
    //利用model计算结果,然后输出
    double result = [self.brain performOperation:sender.currentTitle];
    NSString *resultString = [NSString stringWithFormat:@"%g",result];
    self.display.text = resultString;
}

//按下enter键,就是入栈一个数字
- (IBAction)enterPressed {
    
    [self.brain pushOperation:[self.display.text doubleValue]];
    self.userIsInTheMIdddlOfEnteringANumber = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


CalculatorBrain.h文件

#import <Foundation/Foundation.h>

@interface CalculatorBrain : NSObject

-(void)pushOperation:(double) operand;
-(double)performOperation:(NSString *)operation;

@end


CalculatorBrain.m 文件


#import "CalculatorBrain.h"

//private
@interface CalculatorBrain()

//初始化为nil或0
@property(nonatomic,strong) NSMutableArray *operandStack;

@end



@implementation CalculatorBrain
@synthesize operandStack = _operandStack;
//只有一个变量指针 不分配内存空间

-(NSMutableArray *)operandStack{
    
    //延迟实例化
    if(_operandStack == nil){
        _operandStack = [[NSMutableArray alloc] init];
    }
    return _operandStack;
}

-(void)setOperandStack:(NSMutableArray *)operandStack{
    _operandStack = operandStack;
}

//入操作数
-(void)pushOperation:(double) operand{
    //add只能加入对象
    [self.operandStack addObject:[NSNumber numberWithDouble:operand]];
}

//出栈操作数
-(double)popOperand{
    
    NSNumber *operandObject = [self.operandStack lastObject];
    //数组为空 删除会崩溃
    if (operandObject != nil) {
        [self.operandStack removeLastObject];
    }
    return [operandObject doubleValue];
}

-(double)performOperation:(NSString *)operation{
    
    double result = 0;
    
    if ([operation isEqualToString:@"+"]) {
        result =[self popOperand] + [self popOperand];
    }else if ([operation isEqualToString:@"/"]){
        
        double tmp2 = [self popOperand];
        double tmp1 = [self popOperand];
        if (tmp2 != 0) {
            result = tmp1 / tmp2;
        }
    }else if([operation isEqualToString:@"*"]){
        result = [self popOperand] * [self popOperand];
    }else if([operation isEqualToString:@"-"]){
        double tmp2 = [self popOperand];
        double tmp1 = [self popOperand];
        result = tmp1 - tmp2;
    }
    
    //结果入栈
    [self pushOperation:result];
    return result;
}


@end