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