【问题标题】:Objective C calculator counting priorityObjective C 计算器计数优先级
【发布时间】:2015-02-02 12:52:34
【问题描述】:

我在 Xcode 中做了一个简单的 Objective C 计算器。它完美地工作,除了一件事。例如,当我数 4+5*2 时,计算器会显示答案“18”。它应该是“14”。它从 4+5 开始,然后乘以 2。我希望它将 5 乘以 2,然后加 4。

我一直在寻找我的问题的答案,但没有任何结果。

这是我的计算器的代码:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *display;
-(IBAction) zeroButton;
-(IBAction) oneButton;
-(IBAction) twoButton;
-(IBAction) threeButton;
-(IBAction) fourButton;
-(IBAction) fiveButton;
-(IBAction) sixButton;
-(IBAction) sevenButton;
-(IBAction) eightButton;
-(IBAction) nineButton;
-(IBAction) divideButton;
-(IBAction) multiButton;
-(IBAction) plusButton;
-(IBAction) minusButton;
-(IBAction) resultButton;
-(IBAction) resetButton;
-(IBAction) decimalButton;

@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

@synthesize display;
char myOperator;
NSString *operatorNumber;
bool numberClear = false;

-(void) clearNumber{
    if (numberClear) {
        display.text = @"";
        numberClear = false;
    }}

-(IBAction) zeroButton {
    [self clearNumber];
    if ([display.text isEqual:@"0"]){
     display.text=@"0";
    }
    else{
    display.text=[NSString stringWithFormat:@"%@0",display.text];}
}

-(IBAction) oneButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@1",display.text];}

-(IBAction) twoButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@2",display.text];}

-(IBAction) threeButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@3",display.text];}

-(IBAction) fourButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@4",display.text];}

-(IBAction) fiveButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@5",display.text];}

-(IBAction) sixButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@6",display.text];}

-(IBAction) sevenButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@7",display.text];}

-(IBAction) eightButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@8",display.text];}

-(IBAction) nineButton {
    [self clearNumber];
    display.text=[NSString stringWithFormat:@"%@9",display.text];}

-(IBAction) decimalButton {
    [self clearNumber];
NSRange decimal = [display.text rangeOfString:@"."];
if ( decimal.location == NSNotFound ) {
    display.text=[NSString stringWithFormat:@"%@.",display.text];}
}



-(IBAction) resetButton {
    display.text = @"";
    operatorNumber=@"";
    myOperator=0;
}

-(void)operatorCalculation{

    if (myOperator != 0) {
        [self calculateNewNumber];
        numberClear = true;
    } else {
        operatorNumber = display.text;
        display.text = @"";
    }
}

-(IBAction) plusButton {
    [self operatorCalculation];
    myOperator='+';
}


-(IBAction) minusButton {
    [self operatorCalculation];
    myOperator='-';
}

-(IBAction) multiButton {
    [self operatorCalculation];
    myOperator='*';
}

-(IBAction) divideButton {
    [self operatorCalculation];
    myOperator='/';
}

-(IBAction) resultButton {
    [self operatorCalculation];
    myOperator='=';
}


- (void)calculateNewNumber {
    switch (myOperator) {
        case '+': {
            NSString *secondNumber = display.text;
            float number1 = [operatorNumber floatValue];
            float number2 = [secondNumber floatValue];
            operatorNumber = [NSString stringWithFormat:@"%g", number1+number2];
            display.text = operatorNumber;
            break;
        }
        case '-': {
            NSString *secondNumber = display.text;
            float number1 = [operatorNumber floatValue];
            float number2 = [secondNumber floatValue];
            operatorNumber = [NSString stringWithFormat:@"%g", number1-number2];
            display.text = operatorNumber;
            break;
        }
        case '*': {
            NSString *secondNumber = display.text;
            float number1 = [operatorNumber floatValue];
            float number2 = [secondNumber floatValue];
            operatorNumber = [NSString stringWithFormat:@"%g", number1*number2];
            display.text = operatorNumber;
            break;
        }
        case '/': {
            NSString *secondNumber = display.text;
            float number1 = [operatorNumber floatValue];
            float number2 = [secondNumber floatValue];
            if (number2==0) {
                display.text=@"0";
            }
            else{
            operatorNumber = [NSString stringWithFormat:@"%g", number1/number2];
                display.text = operatorNumber;}
            break;
        }
        case '=': {
            operatorNumber = display.text;
            break;
        }
    }
}

- (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

【问题讨论】:

    标签: ios objective-c c xcode calculator


    【解决方案1】:

    当用户输入他们的方程式时,您正在计算您的答案。如果您想支持运算符优先级(例如:具有高优先级的加法和减法的乘法和除法),您将不得不存储结果并且在完成之前不显示。此外,您将必须实现自己的计算引擎来做出这些优先级决策。难度不大。

    【讨论】:

      【解决方案2】:

      我将回到我的记忆库,但据我记得,您需要等待操作完成,然后才能读取更多数据(数值)。这可以通过一个简单的标志来实现。显然,如果您扩展功能以包含括号之类的内容,则必须扩展逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-23
        • 2015-07-19
        • 2016-07-31
        相关资源
        最近更新 更多