已声明但未设置的变量计算结果为 true?

发布于 2024-12-06 09:32:23 字数 1517 浏览 3 评论 0原文

我正在用下面的代码做一个简单的计算器。现在它执行得很完美。然而,当我试图改变现状时,却行不通。我使用 BOOL 程序 来检查是否继续询问该人的输入或完成程序。

如果我将 while 语句的表达式更改为 (program) 并更改 YES/NO 中的 >program 语句,为什么代码无法执行 while 内的操作?

// A simple printing calculator
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]
    Calculator *deskCalc = [[Calculator alloc] init];
    double value1;
    char operator        
    BOOL program;

    [deskCalc setAccumulator: 0];

    while (!program) {
    NSLog (@"Please type in your expression");
    scanf (" %lf %c", &value1, &operator);
    program = NO;

        if (operator == '+') {
            [deskCalc add: value1];
        }
        else if (operator == '-') {
            [deskCalc subtract: value1];
        }
        else if (operator == '*' || operator == 'x') {
            [deskCalc multiply: value1];
        }
        else if (operator == '/') {
            if (value1 == 0)
                NSLog (@"Division by zero!");
            else
                [deskCalc divide: value1];
        }
        else if (operator == 'S') {
            [deskCalc set: value1];
        }
        else if (operator == 'E') {
            [deskCalc accumulator];
            program = YES;
        }
        else {
            NSLog (@"Unknown operator");
        }
    }

    NSLog (@"The result is %f", [deskCalc accumulator]);

    [deskCalc release];

    [pool drain];
    return 0;
}

I was doing a simple calculator with the following code. Right now it executes perfectly. When I tried to change things around, however, it doesn't work. I used BOOL program to check whether to continue asking for input from the person or finish the program.

If I change the expression of while statement to just (program) and change YES/NO in the program statements, why does the code fail to do what is inside the while?

// A simple printing calculator
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]
    Calculator *deskCalc = [[Calculator alloc] init];
    double value1;
    char operator        
    BOOL program;

    [deskCalc setAccumulator: 0];

    while (!program) {
    NSLog (@"Please type in your expression");
    scanf (" %lf %c", &value1, &operator);
    program = NO;

        if (operator == '+') {
            [deskCalc add: value1];
        }
        else if (operator == '-') {
            [deskCalc subtract: value1];
        }
        else if (operator == '*' || operator == 'x') {
            [deskCalc multiply: value1];
        }
        else if (operator == '/') {
            if (value1 == 0)
                NSLog (@"Division by zero!");
            else
                [deskCalc divide: value1];
        }
        else if (operator == 'S') {
            [deskCalc set: value1];
        }
        else if (operator == 'E') {
            [deskCalc accumulator];
            program = YES;
        }
        else {
            NSLog (@"Unknown operator");
        }
    }

    NSLog (@"The result is %f", [deskCalc accumulator]);

    [deskCalc release];

    [pool drain];
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-12-13 09:32:23

您尚未设置 program 的初始值,因此它默认为一个垃圾值,而该值恰好为非零。

在声明时设置 program 的初始值:

BOOL program = NO; // or YES, whichever is appropriate

You haven't set the initial value of program, so it defaults to a garbage value which just happens to be non-zero.

Set the initial value of program when you declare it:

BOOL program = NO; // or YES, whichever is appropriate
浅紫色的梦幻 2024-12-13 09:32:23

在声明所有变量时初始化它们始终是一个好习惯。

另外,使用 scanf 进行输入可能有点过分,如果我是你,我会使用 fgets,然后使用 strtok 从字符串中提取信息。这样,即使用户将肘部放在键盘上,您也不必担心。或者,如果您喜欢 scanf,请在该字符串上使用 sscanf 而不是 strtok。

It is always a good practice to initialize all your variables when you declare them.

Also using scanf for input may be overdoing it, if I were you I would use fgets and then extract the information from the string using strtok. That way even if the user puts his elbow on the keyboard you will not have to worry. Alternatively if you are fond of scanf use sscanf on that string instead of strtok.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文