5-41 Simple Calculator (20 points)
Simulates the work of a simple arithmetic device. Assuming that the calculator can only perform subtraction operations, the operands and results are integers, and the four operators have the same precedence, calculated in left-to-right order. Input Format:
The input gives a arithmetic calculation in a row, with no spaces and at least one operand. An equal sign "=" indicates the end of the input. output Format:
Outputs the result of a calculation in a row, or if the division denominator is 0 or has an illegal operator, the error message "error" is output. Input Sample:
1+2*10-10/2=
Sample output:
10
//
/* Subtraction operation of the analog operator, however, here, the
arithmetic rule no longer applies, calculated in the order of number and operation symbols,
such as, 1+2*10-10/2=10
*/
#include "stdio.h"
int main ()
{
int value1,value2;
int sum=0;
char ch;
scanf ("%d", &value1);
while ((Ch=getchar ()) = ' = ')
{
scanf ("%d", &value2);
if (ch== '/' &&value2==0) {
printf ("error\n");
return 0;
}
Switch (CH)
{case
' + ': sum=value1+value2;break;
Case '-': sum=value1-value2;break;
Case ' * ': sum=value1*value2;break;
Case '/': Sum=value1/value2;break;
default:printf ("error\n"); return 0;
}
value1=sum;
}
printf ("%d\n", value1);
return 0;
}