#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param tokens string字符串vector * @return int整型 */ int calculatePostfix(vector<string>& tokens) { // write code here stack<int> st; for(auto str:tokens) { if(str!="+" && str!="-" && str!="*" && str!="/") st.push(stoi(str)); else if(str=="+") { int temp_1 = st.top(); st.pop(); int temp_2 = st.top(); st.pop(); st.push(temp_2+temp_1); } else if(str=="-") { int temp_1 = st.top(); st.pop(); int temp_2 = st.top(); st.pop(); st.push(temp_2-temp_1); } else if(str=="*") { int temp_1 = st.top(); st.pop(); int temp_2 = st.top(); st.pop(); st.push(temp_2*temp_1); } else if(str=="/") { int temp_1 = st.top(); st.pop(); int temp_2 = st.top(); st.pop(); st.push(temp_2/temp_1); } } return st.top(); } };