题解 | 简单计算器
简单计算器
https://www.nowcoder.com/practice/b8f770674ba7468bb0a0efcc2aa3a239
float 是单精度浮点数,只能精确表示约 6-7 位有效数字,计算过程中会有微小的舍入误差,比如 2051.6 被存成了 2051.59999...,用 %.4f 输出时就变成了 2051.5999。
#include <stdio.h>
int main() {
double a, b; char op;
scanf("%lf%c%lf", &a,&op,&b);
if(op == '+') printf("%.4f+%.4f=%.4f",a,b,a+b);
else if (op == '-') printf("%.4f-%.4f=%.4f",a,b,a-b);
else if (op == '*') printf("%.4f*%.4f=%.4f",a,b,a*b);
else if (op == '/') {
if(b == 0) printf("Wrong!Division by zero!");
else
printf("%.4f/%.4f=%.4f",a,b,a/b);
}else
printf("Invalid operation!");
return 0;
}

