【发布时间】: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