初音未来 • 3个月前
#include <bits/stdc++.h>
using namespace std;
stack<int>num;
stack<char>op;
int compare(char x) {
int q;
if (x == '+')
q = 1;
else if (x == '*')
q = 2;
return q;
}
void deal() {
int a, b;
b = num.top();
num.pop();
a = num.top();
num.pop();
char p = op.top();
op.pop();
if (p == '+')
num.push(a + b);
if (p == '*')
num.push(a * b % 10000);
}
void ceal() {
string str;
getline(cin, str);
for (int i = 0; i < str.size(); i++) {
int d = 0;
if (str[i] >= '0' && str[i] <= '9') {
while (str[i] >= '0' && str[i] <= '9') {
int a = str[i] - '0';
d = d * 10 + a;
i++;
}
i--;
num.push(d);
} else {
while (op.size() > 0 && compare(op.top()) >= compare(str[i]))
deal();
op.push(str[i]);
}
}
while (!op.empty())
deal();
cout << num.top() % 10000;
}
int main() {
ceal();
return 0;
}
评论:
请先登录,才能进行评论