Exercise 6-7 simple calculator (20 points)

Analog simple arithmetic operation. Suppose only calculator arithmetic operations, operands and results are integers, the same four kinds of operator precedence is calculated from left to right.

Input formats:

In a given input row four arithmetic expression, there is no space, and at least one operand. In case of equal sign "=" Enter the end of the description.

Output formats:

Expression in a row output operation result, or if the denominator of the division is 0 or illegal operator, an error message is output "ERROR".

Sample input:

1+2*10-10/2=

 

Sample output:

10

answer:

#include "stdio.h"
int main(){
    int x, y,re=0;
    char op='0';
    scanf("%d",&x);
    while(op!='='){
        scanf("%c",&op);
        if(op=='=')
            break;
        scanf("%d",&y);
        switch (op){
            case '+':x=x+y;break;
            case '-':x=x-y;break;
            case '*':x=x*y;break;
            case '/':if(y==0)
                re=1;
            else x=x/y;break;
            default :re=1;break;
        }
        if(re==1)break;
    }
    if(re==0)printf("%d",x);
    else printf("ERROR");
    return 0;
}

 

Published 98 original articles · won praise 2 · Views 3725

Guess you like

Origin blog.csdn.net/qq_30377869/article/details/104766379