Time Limit: 2 Seconds Memory Limit: 65536 KB
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 “number1 operatora number2 operatorb number3”. 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.
题目链接:ZOJ-3782
题目大意:简单四则运算
题目思路:模拟
以下是代码:
#include <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof (a))
#define FOR(i,n) for (int i = 0; i < n; i++)
#define INF 1e9
#define eps 1e-10
using namespace std;
typedef long long ll;
int get(char op)
{
if (op == '+' || op == '-') return 0;
else return 1;
}
int solve(int a,char op,int b)
{
if (op == '-') return a - b;
if (op == '+') return a + b;
if (op == '*') return a * b;
if (op == '/') return a / b;
if (op == '%') return a % b;
}
int main(){
int t;
cin >> t;
while(t--)
{
int a,b,c,ans = 0;
char op1,op2;
cin >> a >> op1 >> b >> op2 >> c;
if (get(op1) >= get(op2))
{
ans = solve(a,op1,b);
ans = solve(ans,op2,c);
}
else
{
ans = solve(b,op2,c);
ans = solve(a,op1,ans);
}
cout << ans << endl;
}
return 0;
}