Untitled
unknown
plain_text
3 years ago
834 B
7
Indexable
int postfixExpression(string s) { stack<int> st; string str=""; for(int i=0;i<s.length();i++) { if(s[i]>='0' and s[i]<='9'){ str=str+s[i]; } else if(s[i]=='+' or s[i]=='-' or s[i]=='*'){ char c = s[i]; int x = st.top(); st.pop(); int y = st.top(); st.pop(); if (c == '+') { st.push(y + x); } else if (c == '-') { st.push(y - x); } else if (c == '*') { st.push(y * x); } } else if(str.size() and (s[i]==' ' or (i==s.length()-1))){ st.push(stoi(str)); str=""; } } return st.empty() ? stoi(str) : st.top(); }
Editor is loading...