大家好我在C做了一个小程序,提到了一本书练习。 它的目的是做一些简单的数学运算,询问用户数学运算符和数字,并将它们与累加器一起使用。 这是代码:
/* Printer program, take a number and various operators */
#include <stdio.h>
int main(void)
{
char var;
_Bool end = 0;
float value, accum;
accum = 0.0;
printf("Welcome to the printer, please select your operation
writing the right character\n");
printf("1. + For addiction\n");
printf("2. - For subtraction\n");
printf("3. * For moltiplication\n");
printf("4. / For division\n");
printf("5. S To accumulate a number\n");
printf("6. E To end the program\n");
while( end == 0)
{
printf("Enter the sign and the value: ");
scanf("%c %f", &var, &value);
switch(var)
{
case '+':
printf("Add by %.2f = %.2f\n", value, accum + value);
accum += value;
break;
case '-':
printf("Subtract by %.2f = %.2f\n", value, accum - value);
accum -= value;
break;
case '*':
printf("Multiply by %.2f = %.2f\n", value, accum * value);
accum *= value;
break;
case '/':
printf("Divide by %.2f = %.2f\n", value, accum / value);
accum /= value;
break;
case 'S':
printf("Set accumulator to %.2f\n", value);
accum = value;
break;
case 'E':
printf("Goodbye!\n");
end = 1;
break;
default:
printf("No valid character, retry\n");
break;
}
}
return 0;
}
现在,第一次执行while循环似乎一切正常,但在第一次执行后它开始变得奇怪。该程序无法识别所有操作员,并继续复制显示的printfs。 这是我制作的一个屏幕: http://imgur.com/BQJxJjE
可能是什么问题? 我很喜欢编程,对不起我的英语。