While doing tutorial work, it took me forever to implement a decimal point, and now i have one, that works in the formula, but the issue is it reverses the input - e.g.. enter 1.4, the calculator screen shows it as 4.1 - i know its something to do with my housekeeping of the variables.. planning to use BOOL's instead of a float..but..any takers?
在做教程工作时,我花了很长时间来实现一个小数点,现在我有一个,在公式中工作,但问题是它反转输入 - 例如。输入1.4,计算器屏幕显示为4.1 - i知道它与我对变量的管理有关。计划使用BOOL而不是浮动......但是......任何接受者?
The code:
- (IBAction) pressedDecimal: (id) sender
{
pressedDec = 1;
}
- (IBAction) pressedDigit: (id) sender
{
if (pressedDec == 1)
{currentNumber = .1 * currentNumber + (double)[sender tag];
calculatorScreen.text = [NSString stringWithFormat:@"%g",currentNumber];
}
else
{
currentNumber = currentNumber * 10 + (double)[sender tag];
calculatorScreen.text = [NSString stringWithFormat:@"%g",currentNumber];
pressedDec = 0;
}
}
- (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];
}
2 个解决方案
#1
2
Consider more carefully what this extract from your code does:
更仔细地考虑代码中的这个摘录:
currentNumber = .1 * currentNumber + (double)[sender tag];
(I'm not being very explicit because if I've understood the question right you're doing this in order to learn, and the more you have to think the better you'll learn :-).)
(我不是很明确,因为如果我理解了这个问题,你就是为了学习而做的,而且你必须越多地认为你学得越好:-)。)
#2
-1
Lesson number 2 here: Stanford iTunes U tutorial covers building a simple calculator. Might be helpful. Either way it's a great tutorial series.
第2课:Stanford iTunes U教程包括构建一个简单的计算器。可能会有所帮助。无论哪种方式,它都是一个很棒的教程系列。
#1
2
Consider more carefully what this extract from your code does:
更仔细地考虑代码中的这个摘录:
currentNumber = .1 * currentNumber + (double)[sender tag];
(I'm not being very explicit because if I've understood the question right you're doing this in order to learn, and the more you have to think the better you'll learn :-).)
(我不是很明确,因为如果我理解了这个问题,你就是为了学习而做的,而且你必须越多地认为你学得越好:-)。)
#2
-1
Lesson number 2 here: Stanford iTunes U tutorial covers building a simple calculator. Might be helpful. Either way it's a great tutorial series.
第2课:Stanford iTunes U教程包括构建一个简单的计算器。可能会有所帮助。无论哪种方式,它都是一个很棒的教程系列。