iPhone计算器教程帮助,十进制样式..再次 [英] iPhone calculator tutorial help, decimal style..again

查看:127
本文介绍了iPhone计算器教程帮助,十进制样式..再次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究iPhone计算器教程,但在显示数字时遇到了问题.

I am working on the iPhone calculator tutorial, and I am running into a problem with the display of numbers.

计算器屏幕在第一个输入的操作数(数字)的第一个旁边打印",这导致一些错误的计算,因为currentNumber会返回该字符串.

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];
}

好的,这是一个完整输入的字符串1.11 + 2.22-注意将其全部放在一起.

OK, here is a fully entered string 1.11+2.22 - notice its placing it all together..

的屏幕截图

这里是完成的计算,这是错误的..id想让字符串显示运算符-我应该使用NSRange来解决十进制问题吗?

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?

推荐答案

您试图在按下相应按钮后立即执行每个算术运算.如果您考虑一下,您会发现这可能不正确:例如,如果用户尝试执行12 + 34,则所有计算器都知道何时按下"+"键需要添加某物到12.

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.

这是简单的版本.

仅记住一个当前显示的数字是不够的.假设您到目前为止已按下: 1 2 + 3 .计算器需要知道(1)您输入了数字12,(2)您要求它将其添加到某项中,以及(3)您正在输入另一个数字,到目前为止,您已经知道了3.(可能会变成345之类的东西.)

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.)

因此,计算器的状态由以下三件事组成.(1)按下操作键或"="时显示的最后一个完整值.(如果您最近按下清除按钮,则为 0.) (2) 当前通过按下数字按钮构造的值.(3)按下最后一个操作键.(或者,如果您最近按下"="或清除"按钮之一则为无操作".)

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.

如果您执行所有这些操作,则会发现在某些情况下,即使代码中没有错误,计算器也可能无法满足您的期望.假设您输入12 + 34 * 56.在算术书籍,计算机软件和更昂贵的计算器中,这意味着12+(34 * 56)而不是(12 + 34)* 56,因为乘法的优先级高于加法.以上述方式实现的计算器将以另一种方式实现并计算(12 + 34)* 56.如果那样打扰您,您可以使计算器正确"地执行操作-但这会更加复杂,并且至少在目前,我强烈建议您不要打扰.

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.

这篇关于iPhone计算器教程帮助,十进制样式..再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆