#include <bits/stdc++.h>
using namespace std;
int main() {
string ww;
while(getline(cin,ww)){
vector<char> s;
string str;
for(char c : ww){
if(c == '('){
s.push_back(c);
}else if(c == ')'){
while (s.size() && s.back() != '('){
str+=s.back(),s.pop_back();
}
s.pop_back();
}else if(c == '+' || c == '-'){
while(s.size() && s.back() != '('){
str+=s.back(),s.pop_back();
}
s.push_back(c);
}else if(c == '*' || c == '/' || c == '%'){
while(s.size() && s.back() != '(' && (s.back() == '*' || s.back() == '/' || s.back() == '%')){
str+=s.back(),s.pop_back();
}
s.push_back(c);
}else if(c >= '0' && c<= '9'){
str+=c;
}
}
while(s.size())
str+=s.back(),s.pop_back();
stack <int> st;
string qq;
for(int i=0;i<str.size();i++){
qq = str[i];
if(qq == "+" || qq == "-" || qq == "/" || qq == "*" || qq == "%") {
signed int sc = st.top(); st.pop();
signed int ft = st.top(); st.pop();
if(qq == "+") st.push(ft+sc);
if(qq == "-") st.push(ft-sc);
if(qq == "*") st.push(ft*sc);
if(qq == "/") st.push(ft/sc);
if(qq == "%") st.push(ft%sc);
} else {
st.push(stoi(qq));
}
}
cout<<st.top()<<endl;
}
return 0;
}
參考解題報告以及網路上的教學,不知道為何會有像主題這樣的錯誤,但是我自己試就能算出來,請問是哪裡出問題?