zoukankan      html  css  js  c++  java
  • IOS基础开发二(iphone计算器)

    今天做了个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";
        }
            
    }
    
    @end
    


    
    

     CalculatorBrain(计算器大脑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
    

    效果图:






  • 相关阅读:
    [node.js学习]为node.js写的一个操作mysql的类
    极光IM简单接入步骤
    windows自带的netsh 端口转发
    nodejs 做的带管理后台的东东,主要学习到 ....我忘了学到什么了
    利用来JS控制页面控件显示和隐藏有两种方法
    phpstudy 出现You don't have permission to access / on this server.
    禁用input自动填充
    一般充值的流程
    jq传输json字符串
    ECSHOP更改后台顶部图片
  • 原文地址:https://www.cnblogs.com/lixingle/p/3312966.html
Copyright © 2011-2022 走看看