我在Xcode中做了一个简单的Objective C计算器。除了一件事,它完美地运作。例如,当我计算4 + 5 * 2时,计算器显示答案" 18"。它应该是" 14"。它以4 + 5开始,然后乘以2.我希望它与5相乘,然后加上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
答案 0 :(得分:3)
当用户输入他们的等式时,您正在计算答案。如果要支持运算符优先级(例如:乘法和除法具有加法和减法的高优先级),则必须存储结果,直到完成后才显示。此外,您将不得不实施自己的计算引擎来进行这些优先级决策。这不是太难。
答案 1 :(得分:0)
我要回到我的记忆库中,但我记得你需要等待一个操作完成才能读取更多数据(数值)。这可以通过简单的标志来实现。显然,如果扩展功能以包括括号之类的东西,则必须扩展逻辑。