逆波兰表达式(栈操作)

#include
#include
#include
#include
using namespace std;
class Solution {
public:
int elaLRPN(vector& tokens)
{
if (tokens.empty())
{
return 0;
}
stackst;
for (int i = 0;i < size(tokens);i++)
{
if (tokens[i] == “+” || tokens[i] == “-” || tokens[i] == “" || tokens[i] == “/”)
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
if (tokens[i] == “+”)
st.push(b+a);
else if (tokens[i] == “-”)
st.push(b-a);
else if (tokens[i] == "
”)
st.push(b*a);
else if(tokens[i]=="/")
st.push(b/a);
}
else {
st.push(stoi(tokens[i]));
}
}
return st.top();
}
};
int main()
{
vector str(5);
cout << "please input the string: ";
for (string& a : str)
cin >> a;
Solution s;
cout << s.elaLRPN(str);
return 0;
}

你可能感兴趣的:(逆波兰表达式(栈操作))