题解 | #简单计算器#
简单计算器
http://www.nowcoder.com/practice/b8f770674ba7468bb0a0efcc2aa3a239
#include <stdio.h>
int main()
{
double x = 0;
double y = 0;
char ch = '0';
while (scanf("%lf%c%lf",&x,&ch,&y) != EOF)
{
if (ch == '+' ||ch == '-' || ch == '*' || ch == '/')
{
if (ch == '+')
{
printf("%.4lf+%.4lf=%.4lf\n",x,y,x+y);
}
else if (ch == '-')
{
printf("%.4lf-%.4lf=%.4lf\n",x,y,x-y);
}
else if (ch == '*')
{
printf("%.4lf*%.4lf=%.4lf\n",x,y,x*y);
}
else
{
if (y == 0.0)
{
printf("Wrong!Division by zero!\n");
}
else
{
printf("%.4lf/%.4lf=%.4lf\n",x,y,x/y);
}
}
}
else
{
printf("Invalid operation!\n");
}
}
return 0;
}