[PTA] 7-20 simple calculator (20 minutes)

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 format:
input is given in a four row arithmetic expression, there is no space, and at least one operand. In case of equal sign "=" Enter the end of the description.

Output format:
outputting a calculation result of expression in a row, or if the denominator of the division is 0 or illegal operator outputs an error message "ERROR".

Sample input:
1 + 2 * 10-10 / 2 =

Sample output:
10

#include<stdio.h>
int main()
{
    int a,b;
    char c;
    scanf("%d",&a);
    while(scanf("%c",&c))
    {
    switch(c)
    {
        case('+'):scanf("%d",&b);a+=b;break;
        case('-'):scanf("%d",&b);a-=b;break;
        case('*'):scanf("%d",&b);a*=b;break;
        case('/'):scanf("%d",&b);if(b==0)
        {
            printf("ERROR");return 0;
        }else
        {
            a/=b;break;
        }
        case('='):printf("%d",a);return 0;
        default:printf("ERROR");return 0;
    }
    }
    return 0;
}

Knowledge Point: switch-case statement can be used characters

Published 48 original articles · won praise 0 · Views 323

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/105368700