题解 | #简单计算器#
简单计算器
https://www.nowcoder.com/practice/b8f770674ba7468bb0a0efcc2aa3a239
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
double a, b;
char c;
while (scanf("%lf%c%lf",&a,&c,&b)==3) { // 注意 while 处理多个 case
if(c=='+')
{
printf("%.4lf+%.4lf=%.4lf",a,b,a+b);
}
else if(c=='-')
{
printf("%.4lf-%.4lf=%.4lf",a,b,a-b);
}
else if(c=='*')
{
printf("%.4lf*%.4lf=%.4lf",a,b,a*b);
}
else if(c=='/')
{
if (b==0)
{
cout<<"Wrong!Division by zero!"<<endl;
}
else
{
printf("%.4lf/%.4lf=%.4lf",a,b,a/b);
}
}
else
{
cout<<"Invalid operation!"<<endl;
}
}
}
// 64 位输出请用 printf("%lld")
92.0000*22.3000=2051.5999 C++中输入float的类型的92.0000*22.3000,输出是2051.5999。但是答案是2051.6000,应该怎么处理?
遇到这种问题就去提高精度!float->double!