我正在从 Stephen Kochan 的“C 语言编程”中学习 C。我正在做第 6 章的第 4 练习,编写一个充当简单累加器计算器的代码。代码如下。只要以正确的方式顺序提供输入,它就可以正常工作。
#include<stdio.h>
int main(void)
{
float num, accum = 0;
char operator;
while(1)
{
printf("Enter the number and the operator ");
scanf("%f %c",&num, &operator);
if (operator == 'E') break;
switch (operator){
case 'S':
accum = num;
printf("= %g\n",accum);
break;
case '+':
accum = accum + num;
printf("= %g\n",accum);
break;
case '-':
accum = accum - num;
printf("= %g\n",accum);
break;
case '*':
accum = accum * num;
printf("= %g\n",accum);
break;
case '/':
accum = accum / num;
printf("= %g\n",accum);
break;
}
}
printf("= %g\n",accum);
printf("End of Calculation\n");
return 0;
}
但它挂在错误的输入上。可以做些什么来检查这种行为?