Calculator v1.0 for iOS

我不要仅仅只做一个Manager, 我要努力成为一个Creator! Fighting!

This Is A Beginning To Be Big A Man!

//

//  ViewController.m

//  Calc

//

//  Created by ChenJackson on 7/1/16.

//  Copyright © 2016 ChenJackson. All rights reserved.

//


#import "ViewController.h"

#import "Calculator.h"


@interface ViewController ()


@end


@implementation ViewController

{

    SEL Op;

    bool isDigitEnd;

    int setPoint;

    long int currentDigit;

    int countOp;

    Calculator *myCalc;

    NSMutableString *displayString;


}

@synthesize display,display1;


-(void) processDigit: (long int) Digit

{

    if (setPoint) {

        myCalc.CurrentNum = myCalc.CurrentNum  + (float) Digit / (setPoint * 10);

        [displayString appendString: [NSMutableString stringWithFormat:@"%i", (int)Digit]];

        display1.text = displayString;

    }

    else {

        myCalc.CurrentNum = myCalc.CurrentNum * 10 + Digit;

        [displayString appendString: [NSMutableString stringWithFormat:@"%i", (int)Digit]];

        display1.text = displayString;


    }

}

-(void) storeValue

{

        myCalc.Num1 = (double) myCalc.CurrentNum;

        myCalc.CurrentNum = 0;

}

-(void) processOp: (char) theOp

{

    isDigitEnd = YES;

    NSString *opStr;

    switch (theOp) {

        case '+' :

            opStr = @" + ";

            Op = @selector(execPlus);

            break;

        case '-' :

            opStr = @" - ";

            Op = @selector(execMinus);

            break;

        case 'x' :

            opStr = @" x ";

            Op = @selector(execMultiply);

            break;

        case '/' :

            opStr = @" / ";

            Op = @selector(execDivided);

            break;

        case '%' :

            opStr = @" % ";

            Op = @selector(execMod);

            break;

        default:

            break;

    }


    [displayString appendString: opStr];

    display1.text = displayString;

}

-(void) showResult

{

    [myCalc performSelector: Op];

    if ( myCalc.Error ) {

        myCalc = [[Calculator alloc] init] ;

        display.text = @"Error";

        displayString = [NSMutableString stringWithString: @""];

        myCalc.Error -= 1;

    }

    else {

        display.text = [NSMutableString stringWithFormat:@"%g", [myCalc Accumulator]];

    }

    myCalc.Num1 = myCalc.Accumulator;

    myCalc.CurrentNum = 0;

}


-(IBAction) clickDigit: (UIButton *) sender

{

    isDigitEnd = NO;

    long int digit = sender.tag;

    [self processDigit: digit];

    currentDigit = digit;

}

-(IBAction) clickPoint

{

    setPoint = 1;

    [displayString appendString: @"."];

    display1.text = displayString;

}


-(IBAction) clickMinus

{

    if (countOp > 0) {

        [self showResult];

    }

    else {

    [self storeValue];

    }

    countOp++;

    setPoint = 0;

    [self processOp: '-'];

}

-(IBAction) clickPlus

{

    if (countOp) {

        [self showResult];

    }

    else {

        [self storeValue];

    }

    countOp++;

    setPoint = 0;

    [self processOp: '+'];

}

-(IBAction) clickMultiply

{

    if (countOp) {

        [self showResult];

    }

    else {

        [self storeValue];

    }

    countOp++;

    setPoint = 0;

    [self processOp: 'x'];

}

-(IBAction) clickDivided

{

    if (countOp) {

        [self showResult];

    }

    else {

        [self storeValue];

    }

    countOp++;

    setPoint = 0;

    [self processOp: '/'];

}

-(IBAction) clickMod

{

    if (countOp) {

        [self showResult];

    }

    else {

        [self storeValue];

    }

    countOp++;

    setPoint = 0;

    [self processOp: '%'];

}

-(IBAction) clickEqual

{


    if ( countOp == 0 ) {

        if (isDigitEnd == NO) {

            myCalc.Accumulator = myCalc.CurrentNum;

            display.text = [NSMutableString stringWithFormat:@"%g", myCalc.Accumulator];

            myCalc.CurrentNum = 0;

        }

        else

            display.text = @"0";

    }

    else

        [self showResult];


    isDigitEnd = YES;

    countOp = 0;

    setPoint = 0;

    Op = @selector(Num1);

    myCalc.CurrentNum = 0;

    myCalc.Num1 = myCalc.Accumulator;

    displayString = [NSMutableString stringWithString: @""];

    display1.text = @"=";

}

-(IBAction) clickAC

{

    isDigitEnd = YES;

    myCalc.CurrentNum = 0;

    countOp = 0;

    setPoint = 0;

    myCalc = [[Calculator alloc] init];

    myCalc.Num1 = 0;

    displayString = [NSMutableString stringWithString: @""];

    display.text = @"";

    display1.text = @"";


}

-(IBAction) clickDel

{

    if (isDigitEnd == NO) {

        myCalc.CurrentNum = (myCalc.CurrentNum - currentDigit) / 10;

    }

//    else


}


- (void)viewDidLoad {

    [super viewDidLoad];

    displayString = [NSMutableString stringWithCapacity:40];

    displayString = [NSMutableString stringWithString:@""];

    [self clickAC];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25521690/viewspace-2121545/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/25521690/viewspace-2121545/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值