#include<stdio.h>int main(void){ char operator; double value1, value2; printf("Type in an expression: "); scanf("%lf%c%lf", &value1, &operator, &value2);switch(operator){ case '+': printf("=%.2f\n", value1+value2); break; case '-': printf("=%.2f\n", value1-value2); break; case '*': printf("=%.2f\n", value1*value2); break; case '/': if(value2==0) printf("Cannot divide into 0"); elseprintf("=%.2f\n", value1/value2); break; default: printf("Unknown operator\n"); break; } return 0;}哪里有问题
时间: 2024-01-20 15:03:08 浏览: 91
#include <stdio.h>
这段代码的逻辑没有问题,但是在除法运算中没有考虑除数为0的情况。如果除数为0,程序会输出"Cannot divide into 0",但是并没有结束程序,会继续执行后面的语句。建议加上`return`语句来结束程序的执行。修改后的代码如下:
```
#include<stdio.h>
int main(void){
char operator;
double value1, value2;
printf("Type in an expression: ");
scanf("%lf%c%lf", &value1, &operator, &value2);
switch(operator){
case '+':
printf("=%.2f\n", value1+value2);
break;
case '-':
printf("=%.2f\n", value1-value2);
break;
case '*':
printf("=%.2f\n", value1*value2);
break;
case '/':
if(value2==0)
printf("Cannot divide into 0");
else
printf("=%.2f\n", value1/value2);
return 0; // 结束程序
default:
printf("Unknown operator\n");
break;
}
return 0;
}
```
阅读全文