老式的计算机只能按照固定次序进行运算,华安大学就有这样一台老式计算机,计算模式为A@B#C,@和#为输入的运算符(可能是+、-或*,运算符优先级与C++一致),现给出A,B,C的数值以及@和#对应的运算符,请你编写程序,验证老式计算机的运行结果。输入说明:第一行是一个整数N(IN≤10000),表示有N组计算数据需要处理,接下来N行,每行是相应的数据,包括三个整数和两个运算符,分别对应A、@、B、#和C。输出说明:对每行输入的数据,输出计算结果。输入样例:
3
1 - 12 + 12
12 * 1 - 8
13 + 13 * 2
输出样例:
23
4
39
代码:
C++:
cpp
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b, c;
char d, e;
cin >> a >> d >> b >> e >> c;
if (d == '+' && e == '+') {
cout << a + b + c << endl;
}
else if (d == '+' && e == '-') {
cout << a + b - c << endl;
}
else if (d == '+' && e == '*') {
cout << a + b * c << endl;
}
else if (d == '-' && e == '-') {
cout << a - b - c << endl;
}
else if (d == '-' && e == '+') {
cout << a - b + c << endl;
}
else if (d == '-' && e == '*') {
cout << a - b * c << endl;
}
else if (d == '*' && e == '+') {
cout << a * b + c << endl;
}
else if (d == '*' && e == '-') {
cout << a * b - c << endl;
}
else if (d == '*' && e == '*') {
cout << a * b * c << endl;
}
}
return 0;
}
Python:
python
n = int(input())
for i in range(0, n, 1):
a, d, b, e, c = map(str, input().split())
if d == '+' and e == '+':
print(int(a) + int(b) + int(c))
elif d == '+' and e == '-':
print(int(a) + int(b) - int(c))
elif d == '+' and e == '*':
print(int(a) + int(b) * int(c))
elif d == '-' and e == '+':
print(int(a) - int(b) + int(c))
elif d == '-' and e == '-':
print(int(a) - int(b) - int(c))
elif d == '-' and e == '*':
print(int(a) - int(b) * int(c))
elif d == '*' and e == '+':
print(int(a) * int(b) + int(c))
elif d == '*' and e == '-':
print(int(a) * int(b) - int(c))
elif d == '*' and e == '*':
print(int(a) * int(b) * int(c))
Java:
java
package com.my.gududu;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
int a, b, c;
char d, e;
a = input.nextInt();
d = input.next().charAt(0);
b = input.nextInt();
e = input.next().charAt(0);
c = input.nextInt();
if (d == '+' && e == '+') {
System.out.println(a + b + c);
}
else if (d == '+' && e == '-') {
System.out.println(a + b - c);
}
else if (d == '+' && e == '*') {
System.out.println(a + b * c);
}
else if (d == '-' && e == '+') {
System.out.println(a - b + c);
}
else if (d == '-' && e == '-') {
System.out.println(a - b - c);
}
else if (d == '-' && e == '*') {
System.out.println(a - b * c);
}
else if (d == '*' && e == '+') {
System.out.println(a * b + c);
}
else if (d == '*' && e == '-') {
System.out.println(a * b - c);
}
else if (d == '*' && e == '*') {
System.out.println(a * b * c);
}
}
}
}