#include <cstdio>
#include <stack>
#include <cstring>
#define int long long
using namespace std;
const int MAXN = 30000;
char str[MAXN];
stack<int> numSTA;
stack<char> opSTA;
char ops[8] = {'+', '-', '*', '/', '(', ')', '^', '@'};
int matrix[8][8] = {
{-1, -1, -1, -1, 1, -1, -1, 1},
{-1, -1, -1, -1, 1, -1, -1, 1},
{ 1, 1, -1, -1, 1, -1, -1, 1},
{ 1, 1, -1, -1, 1, -1, -1, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1},
{-1, -1, -1, -1, 0, -1, -1, 1},
{ 1, 1, 1, 1, 1, -1, -1, 1},
{-1, -1, -1, -1, -1, -1, -1, 0}
};
int char_to_int(char ch) {
for (int i = 0; i < 8; i++) {
if (ops[i] == ch) return i;
}
return -1;
}
int calc(int a, char ch, int b) {
if (ch == '+') return a + b;
if (ch == '-') return a - b;
if (ch == '*') return a * b;
if (ch == '/') return a / b;
if (ch == '^') {
int ans = 1;
while (b) {
if (b & 1) {
ans *= a;
}
a *= a;
b >>= 1;
}
return ans;
}
}
signed main() {
scanf("%s", str + 1);
int n = strlen(str + 1);
str[++n] = '@';
int num = 0;
bool have_num = 0;
opSTA.push('@');
int ans = 0;
for (int i = 1; i <= n;) {
if (str[i] >= '0' && str[i] <= '9') {
//nums
have_num = 1;
num = num * 10 + str[i] - '0';
}
else {
//operator
if (have_num) {
numSTA.push(num);
have_num = 0;
num = 0;
}
char op = opSTA.top();
int ret = matrix[char_to_int(str[i])][char_to_int(op)];
if (ret == 1) {
opSTA.push(str[i]);
}
else if (ret == -1) {
opSTA.pop();
int num2 = numSTA.top();
numSTA.pop();
int num1 = numSTA.top();
numSTA.pop();
numSTA.push(ans = calc(num1, op, num2));
// printf("%d\n", ans);
continue;
}
else if (ret == 0) {
//break
opSTA.pop();
}
}
i++;
}
printf("%lld", ans);
return 0;
}
支持