zoukankan      html  css  js  c++  java
  • calculator

    //  ViewController.h
    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @property (weak, nonatomic) IBOutlet UILabel *display;

    - (IBAction)digitpressed:(id)sender;

    @end

     //  ViewController.m


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

    @interface ViewController()
    @property (nonatomic)BOOL userIsInTheMiddleOfEnteringANumber;
    @property (nonatomic,strong)CalculatorBrain *brain;
    @end


    @implementation ViewController
    @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;
    }
    @end

    // CalculatorBrain.h

    #import <Foundation/Foundation.h>

    @interface CalculatorBrain : NSObject

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

    @end

    // CalculatorBrain.m

    #import "CalculatorBrain.h"

    @interface CalculatorBrain()

    @property(nonatomic ,strong)NSMutableArray *operandStack;


    @end


    @implementation CalculatorBrain
    @synthesize operandStack=_operandStack;
    -(NSMutableArray *)operandStack
    {
    if(_operandStack==nil)
    _operandStack=[[NSMutableArray alloc]init];
    return _operandStack;
    }

    -(double)popOperand
    {
    NSNumber *operandObject=[self.operandStack lastObject];
    if(operandObject)[self.operandStack removeLastObject];
    return [operandObject doubleValue];
    }
    -(void)pushOperand:(double)operand
    {

    [self.operandStack addObject:[NSNumber numberWithDouble:operand]];
    }
    -(double)performOperation:(NSString *)operation
    {
    double result=0;
    //calculate result
    if([operation isEqualToString:@"+"])
    {
    result=[self popOperand]+[self popOperand];
    }
    else if([@"*" isEqualToString:operation])
    {
    result = [self popOperand]*[self popOperand];

    }
    [self pushOperand:result];

    return result;
    }

    @end

  • 相关阅读:
    Prism之12345
    Struts2注解学习1
    模拟Spring依赖注入
    Spring的IOC注解学习
    Hibernate注解学习1
    Redis源码分析(二十六) slowLog和hyperloglog
    做优秀产品经理所需的7种素质
    程序员学习英语
    PL/SQL Developer记住密码设置
    oracle11g,安装及net Manager的配置
  • 原文地址:https://www.cnblogs.com/ganeveryday/p/4358355.html
Copyright © 2011-2022 走看看