代碼
#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
typedef long long LL;
const int SIZE = 1010;
char str[SIZE];
stack<char> OPERATOR;
stack<LL> OPERAND;
inline int get_priority(char ch) {
switch (ch) {
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case ')':
return 3;
}
}
inline LL calc() {
LL b = OPERAND.top(); OPERAND.pop();
LL a = OPERAND.top(); OPERAND.pop();
char ch = OPERATOR.top(); OPERATOR.pop();
switch (ch) {
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
}
}
int main() {
cin >> str;
int priority_top, priority_cur;
LL number;
int i = 0, len = strlen(str)-1;
while (i < len) {
if (isdigit(str[i])) {
number = 0;
while (i < len && isdigit(str[i])) {
number = number * 10 + (str[i] - '0');
i++;
}
OPERAND.push(number);
} else {
if (OPERATOR.empty()) {
OPERATOR.push(str[i]);
i++;
continue;
}
priority_top = get_priority(OPERATOR.top());
priority_cur = get_priority(str[i]);
if (priority_cur == 0) {
OPERATOR.push(str[i]);
i++;
} else if (priority_cur == 3) {
while (OPERATOR.top() != '(') {
number=calc();
OPERAND.push(number);
}
OPERATOR.pop();
i++;
} else {
while (!OPERATOR.empty() && priority_cur <= priority_top) {
number=calc();
OPERAND.push(number);
if (!OPERATOR.empty()) priority_top = get_priority(OPERATOR.top());
}
OPERATOR.push(str[i]);
i++;
}
}
}
while (!OPERATOR.empty()) {
number = calc();
OPERAND.push(number);
}
cout << OPERAND.top() << endl;
return 0;
}
測試用例
編號 |
輸入 |
輸出 |
1 |
1= |
1 |
2 |
1+2-3= |
0 |
3 |
(1+2)*3= |
9 |
4 |
1-(2+3)*4/5= |
-3 |
5 |
(9+8-7*6/5+(3-2))= |
10 |
6 |
(100)= |
100 |
7 |
(((0+31)-4)*50)/6= |
225 |
8 |
(((100000000+31)-4)*50)/6000= |
833333 |
9 |
(99999999-20)/300+365+24*12+(32767*5)= |
497821 |
10 |
(((9*9)*9)*9)/10/10+(100*100*100*100-100)/900+1000000= |
1111176 |