开始用的float型声明操作数,但是提交后结果如下:
提示有一组测试用例没通过,看了下是因为四舍五入精度问题,于是将操作数声明为double型变量,顺利通过:
代码:
#include<stdio.h>
int main(){
char op;
double op1,op2;
while(~scanf("%lf%c%lf",&op1,&op,&op2)){
if(op!='+'&&op!='-'&&op!='*'&&op!='/')
printf("Invalid operation!\n");
else if(op=='/'){
if(op2==0.0) printf("Wrong!Division by zero!\n");
else printf("%.4f/%.4f=%.4f\n",op1,op2,op1/op2);
}
else{
if(op=='+') printf("%.4f+%.4f=%.4f\n",op1,op2,op1+op2);
if(op=='-') printf("%.4f-%.4f=%.4f\n",op1,op2,op1-op2);
if(op=='*') printf("%.4f*%.4f=%.4f\n",op1,op2,op1*op2);
}
}
}
京公网安备 11010502036488号