努力编写一个简单的计算器
Struggling to program a simple calculator
我正在尝试编写一个简单的计算器。
首先是我的代码:
#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;
}
所以对我来说代码很好。如您所见,我使用一个 while 循环来执行此操作,该循环允许您计算直到您输入 'q' 作为运算符。循环的第一个 运行 工作正常但随后变得令人毛骨悚然(我的控制台):
Operator: +
First Number: 5
Second Number: 4
The solution is: 9.00
Operator: First Number:
为什么程序不让我在第二个循环中输入运算符 运行?
大多数带有 scanf 的格式说明符将跳过前导白色space。 %c
没有。
scanf("%f", &num2);
在第一次迭代结束时在输入缓冲区中留下一个换行符。
scanf("%c", &operator);
在第二次迭代开始时,读取换行符并继续。
在 scanf(" %c", &operator);
中的 %c
之前使用 space 将允许 %c
跳过前导白色 space 并捕获运算符。
您应该检查 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;
}
如您所见,我将 scanf("%c", &operator);
更改为此 scanf(" %c", &operator);
以使 scanf
忽略(跳过)Whitespace
。
我正在尝试编写一个简单的计算器。
首先是我的代码:
#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;
}
所以对我来说代码很好。如您所见,我使用一个 while 循环来执行此操作,该循环允许您计算直到您输入 'q' 作为运算符。循环的第一个 运行 工作正常但随后变得令人毛骨悚然(我的控制台):
Operator: +
First Number: 5
Second Number: 4
The solution is: 9.00
Operator: First Number:
为什么程序不让我在第二个循环中输入运算符 运行?
大多数带有 scanf 的格式说明符将跳过前导白色space。 %c
没有。
scanf("%f", &num2);
在第一次迭代结束时在输入缓冲区中留下一个换行符。
scanf("%c", &operator);
在第二次迭代开始时,读取换行符并继续。
在 scanf(" %c", &operator);
中的 %c
之前使用 space 将允许 %c
跳过前导白色 space 并捕获运算符。
您应该检查 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;
}
如您所见,我将 scanf("%c", &operator);
更改为此 scanf(" %c", &operator);
以使 scanf
忽略(跳过)Whitespace
。