题解 | 简单计算器
记得使用双精度,否则会有一组报错。
#include <stdio.h>
int main() {
char ch;
double a,b;
scanf("%lf%c%lf",&a,&ch,&b);
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
switch (ch) {
case '+':printf("%.4lf+%.4lf=%.4lf\n",a,b,a+b);break;
case '-':printf("%.4lf-%.4lf=%.4lf\n",a,b,a-b);break;
case '*':printf("%.4lf*%.4lf=%.4lf\n",a,b,a*b);break;
case '/':
if(b == 0.0){
printf("Wrong!Division by zero!\n");break;
}
else{
printf("%.4lf/%.4lf=%.4lf\n",a,b,a/b);break;
}
}
}
else{
printf("Invalid operation!");
}
return 0;
}

查看15道真题和解析