简体   繁体   English

努力编写一个简单的计算器

[英]Struggling to program a simple calculator

I am trying to program a simple calculator. 我正在尝试编写一个简单的计算器。
Here is my code first: 这是我的代码优先:

#include <stdio.h>
#include <stdlib.h>

int main(void) { 

char operator = 0;
float num1 = 0.0;
float num2 = 0.0;
float sol = 0.0;


while (operator != 'q') {

    printf("Operator: ");
    scanf("%c", &operator);
    printf("First Number: ");
    scanf("%f", &num1);
    printf("Second Number: ");
    scanf("%f", &num2);

    switch (operator)
    {
    case '+': sol = num1 + num2; break;
    case '-': sol = num1 - num2; break;
    case '*': sol = num1 * num2; break;
    case '/': sol = num1 / num2; break;
    case 'q': printf("Finished!"); exit(0);
    default: printf("Error!"); exit(0);
    }

    printf("The solution is: %.2f\n\n", sol); 
}

return 0;
}

So for me the code is fine. 所以对我来说,代码很好。 As you can see I did this with a while loop that lets you calculate until you type in 'q' as operator. 如您所见,我使用while循环进行了此操作,该循环使您可以进行计算,直到您输入'q'作为运算符为止。 The first run of the loop works fine but then it gets creepy (my console): 循环的第一次运行正常,但随后变得令人毛骨悚然(我的控制台):

Operator: +
First Number: 5
Second Number: 4
The solution is: 9.00

Operator: First Number:

Why does the program not let me enter an operator in the second loop run? 为什么程序不让我在第二个循环运行中输入运算符?

Most format specifiers with scanf will skip leading whitespace. 带有scanf的大多数格式说明符将跳过前导空格。 %c does not. %c没有。
scanf("%f", &num2); at the end of the first iteration leaves a newline in the input buffer. 在第一次迭代结束时,在输入缓冲区中留下了换行符。
scanf("%c", &operator); at the start of the second iteration, reads the newline and proceeds. 在第二次迭代的开始,读取换行符并继续。
using a space before %c in scanf(" %c", &operator); 之前使用空间%cscanf(" %c", &operator); will allow %c to skip the leading whitespace and capture the operator. 将允许%c跳过前导空格并捕获运算符。

You should check scanf for errors: 您应该检查scanf是否存在错误:

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    char operator = 0;
    float num1 = 0.0;
    float num2 = 0.0;
    float sol = 0.0;

    while (operator != 'q') {

        printf("Operator: ");
        if((scanf(" %c", &operator)) != 1){
            printf("Error, Fix it!\n");
            exit(1);
        }

        printf("First Number: ");
        if((scanf("%f", &num1)) != 1){
            printf("Error, Fix it!\n");
            exit(1);
        }

        printf("Second Number: ");
        if((scanf("%f", &num2)) != 1){
            printf("Error, Fix it!\n");
            exit(1);
        }

        switch (operator){
            case '+': sol = num1 + num2; break;
            case '-': sol = num1 - num2; break;
            case '*': sol = num1 * num2; break;
            case '/': sol = num1 / num2; break;
            case 'q': printf("Finished!"); exit(0);
            default: printf("Error!"); exit(0);
        }

        printf("The solution is: %.2f\n\n", sol);
    }

    return 0;
}

and as you can see I changed scanf("%c", &operator); 如您所见,我更改了scanf("%c", &operator); to this scanf(" %c", &operator); 到这个scanf(" %c", &operator); to make scanf to ignore (skip) Whitespace . 使scanf忽略(跳过) Whitespace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM