zoj-3782-Ternary Calculation

Description

Complete the ternary calculation.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is a string in the form of "number1operatoranumber2operatorbnumber3". Each operator will be one of {'+', '-' , '*', '/', '%'}, and each number will be an integer in [1, 1000].

Output

For each test case, output the answer.

Sample Input

5
1 + 2 * 3
1 - 8 / 3
1 + 2 - 3
7 * 8 / 5
5 - 8 % 3

Sample Output

7
-1
0
11
3

Note

The calculation "A % B" means taking the remainder of A divided by B, and "A / B" means taking the quotient.


总共只有那么几种情况,也就两个操作符三个数,实在不行枚举也行

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()  
{  
    int i,j,t,a,b,c,ans;  
    char o1,o2;  
    scanf("%d",&t);  
    while(t--)  
    {  
        scanf("%d%*c%c%*c%d%*c%c%*c%d",&a,&o1,&b,&o2,&c);  
        if((o1=='+'||o1=='-')&&(o2=='*'||o2=='/'||o2=='%'))  
        {  
            switch(o2)  
            {  
                case'*':ans=b*c;break;  
                case'/':ans=b/c;break;  
                case'%':ans=b%c;  
            }  
            switch(o1)  
            {  
                case'+':ans+=a;break;  
                case'-':ans=a-ans;  
            }  
        }  
        else  
        {  
            switch(o1)  
            {  
                case'+':ans=a+b;break;  
                case'-':ans=a-b;break;  
                case'*':ans=a*b;break;  
                case'/':ans=a/b;break;  
                case'%':ans=a%b;  
            }  
            switch(o2)  
            {  
                case'+':ans+=c;break;  
                case'-':ans-=c;break;  
                case'*':ans*=c;break;  
                case'/':ans/=c;break;  
                case'%':ans%=c;  
            }  
        }  
        printf("%d\n",ans);  
    }  
    return 0;  
}  



你可能感兴趣的:(zoj-3782-Ternary Calculation)