【发布时间】:2017-01-12 22:06:26
【问题描述】:
我想用 c 编写基本的计算器: 我有累加器的问题(使用“+”和“-”运算符)
int main(void)
{
float num1,num2,res;
char operator;
printf ("Type in your expression.\n");
scanf ("%f %c %f", &num1, &operator, &num2);
if(operator == 'S'){
printf(" = %.2f\n",num1);
res = num1;
while(1){
printf ("Type in your expression.\n");
scanf ("%c %f", &operator, &num2);
if(operator == '+'){
res += num2;
printf("%.2f\n", res);
}
else if(operator == '-'){
res -=num2;
printf("%.2f\n",res);
}
else if(operator == '*'){
res *=num2;
printf("%.2f\n",res);
}
else if(operator == '/'){
if(num2 == 0)
printf("Division by zero\n");
else
res /=num2;
printf("%.2f\n",res);
}
else if(operator == 'E'){
printf("End of the calculation:");
printf("%.2f",res);
break;
}
}
}
在这部分代码中,除法和乘法工作正常: 但是当我在累加器期间像“+3”或“-2”这样的小费时,什么也没有发生。我不知道问题出在哪里。
【问题讨论】:
-
见How to Ask。调试器说什么?
标签: c if-statement calculator