问题
I am working on the iPhone calculator tutorial, and I am running into a problem with the display of numbers.
The calculator screen "prints" the 2nd entered operand (number) right next to the first one causing some erroneous calculations as- currentNumber feeds off this string.
How can i get the numbers and even the operator (+,-,*,/) to all show up and still perform the calculation correctly?
- (IBAction)pressedDecimal:(id)sender {
calculatorScreen.text = [calculatorScreen.text stringByAppendingString:@"."];
}
- (IBAction)pressedDigit:(id)sender {
calculatorScreen.text = [calculatorScreen.text stringByAppendingFormat:@"%d", [sender tag]];
currentNumber = [calculatorScreen.text floatValue];
}
- (IBAction) pressedOperation: (id) sender {
if (currentOperation == 0) result = currentNumber;
else {
switch (currentOperation) {
case 1:
result = result + currentNumber;
break;
case 2:
result = result - currentNumber;
break;
case 3:
result = result * currentNumber;
break;
case 4:
result = result / currentNumber;
break;
case 5:
currentOperation = 0;
break;
}
}
currentNumber = 0;
calculatorScreen.text = [NSString stringWithFormat:@"%g", result];
if ([sender tag] == 0) result = 0;
currentOperation = [sender tag];
}
OK, here is a fully entered string 1.11+2.22 - notice its placing it all together..

Here is the finished calculation, which is wrong..id like to have the string show the operator - is the decimal problem something i should be using NSRange for?

回答1:
You're trying to do each arithmetic operation as soon as the corresponding button is pressed. If you think about it, you'll see that this can't possibly be right: if the user tries to do 12+34, for instance, all the calculator knows when "+" is pressed is that it needs to add something to 12.
Now, if you're working from a build-a-calculator tutorial, then almost certainly whoever wrote it has a solution to this, and you should follow that because if you try to do it a different way then it'll be confusing. But here's what I'd do. There's a simple version and a much more complicated version.
Here's the simple version.
It's not enough just to remember one number, the one currently being displayed. Suppose you've so far pressed: 1 2 + 3
. The calculator needs to know (1) that you entered the number 12, (2) that you asked it to add that to something, and (3) that you're entering another number and so far have got as far as 3
. (It might turn into 345 or something.)
So, the state of the calculator consists of the following three things. (1) The last complete value it was showing when you pressed an operation key or "=". (Or 0 if you pressed the clear button more recently than those.) (2) The value currently being constructed by pressing number buttons. (3) The last operation key pressed. (Or "no operation" if you pressed "=" or the clear button more recently than one of those.)
The point of all this is that this is exactly the information the calculator needs in order to do what you've asked it to. I shan't go into details because it will be Good For Your Soul for you to work out just how that state changes when you press each possible key.
If you do all this, you'll find that there are situations where the calculator maybe doesn't do what you want, even if there aren't bugs in the code. Suppose you enter 12+34*56. In arithmetic books, and computer software, and more-expensive calculators, this means 12+(34*56) not (12+34)*56 because multiplication has "higher precedence" than addition. A calculator implemented the way I described above will do it the other way and compute (12+34)*56. If that bothers you, you can make the calculator do it "properly" -- but it will be much more complicated, and for the moment at least I strongly recommend that you not bother.
来源:https://stackoverflow.com/questions/10369173/iphone-calculator-tutorial-help-decimal-style-again