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 113
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int main(){ int a,b,c,T,ans; char e,f; cin >> T; while(T--){ cin >> a >> e >> b >> f >> c; if((e=='+' || e=='-')&&(f=='*' || f=='/'|| f=='%')){ switch(f){ case '*':ans=b*c;break; case '/':ans=b/c;break; case '%':ans=b%c;break; } switch(e){ case '+':ans=a+ans;break; case '-':ans=a-ans;break; } } else{ switch(e){ case '*':ans=a*b;break; case '/':ans=a/b;break; case '%':ans=a%b;break; case '+':ans=a+b;break; case '-':ans=a-b;break; } switch(f){ case '*':ans=ans*c;break; case '/':ans=ans/c;break; case '%':ans=ans%c;break; case '+':ans=ans+c;break; case '-':ans=ans-c;break; } } cout << ans << endl; } return 0; }