Calculating an expression
Given an mathematic expression, you soppose to print out the value of this espression.
Input Specification:
Given an mathematic expression, include 2 integer operands and 1 operator (+,-,*,/ ). No extra space allowed.
Output Specification:
Output the answer. If the operator is "/", keep 2 decimal digits behind point; else, output in integer.
Sample Input 1:
10+15
Sample Output 1:
25
Sample Input 2:
5/3
Sample Output 2:
1.67
解答:
#include <stdio.h>
int main()
{
double a, b, c;
char op;
scanf("%lf %c %lf",&a, &op, &b);
switch(op){
case '+':
c = a + b;
printf("%.0f\n", c);
break;
case '-':
c = a - b;
printf("%.0f\n", c);
break;
case '*':
c = a * b;
printf("%.0f\n", c);
break;
case '/':
c = a / b;
printf("%.2f\n", c);
break;
}
return 0;
}