chillurbrain

12. Постфиксная запись

May 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. using namespace std;
  5. stack<int> st;
  6. int main()
  7. {
  8.     string s;
  9.     int i,j,b,c;
  10.     getline(cin,s);
  11.     for(i=0;i<s.size();++i)
  12.     {
  13.         if(s[i]>='0' && s[i]<='9')
  14.             st.push(s[i]-'0');
  15.         else
  16.         {
  17.             if(s[i]=='+')
  18.             {
  19.                 b=st.top();
  20.                 st.pop();
  21.                 c=st.top();
  22.                 st.pop();
  23.                 st.push(b+c);
  24.             }
  25.             if(s[i]=='-')
  26.             {
  27.                 b=st.top();
  28.                 st.pop();
  29.                 c=st.top();
  30.                 st.pop();
  31.                 st.push(c-b);
  32.             }
  33.                 if(s[i]=='*')
  34.             {
  35.                 b=st.top();
  36.                 st.pop();
  37.                 c=st.top();
  38.                 st.pop();
  39.                 st.push(b*c);
  40.             }
  41.  
  42.         }
  43.     }
  44.     cout<<st.top();
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment