Advertisement
VladSmirN

Индивидуальная работа аип2

Mar 28th, 2021
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <stack>
  5. using namespace std;
  6. string rev(string s)
  7. {
  8.     string ans;
  9.     for(int i=s.size()-1; i>=0; --i)
  10.     {
  11.         ans.push_back(s[i]);
  12.     }
  13.     return ans;
  14. }
  15. stack<int> st1;
  16. stack<char> st2;
  17.  
  18. int main()
  19. {
  20.     string s = "M(m(M(18,8),111),m(17,23))";
  21.     for(int i=0; i<s.size(); ++i)
  22.     {
  23.         if(s[i]=='(' || s[i]==')' || s[i]==',')
  24.         {
  25.             s[i]=' ';
  26.         }
  27.     }
  28.     string str;
  29.     bool flag = false;
  30.     for(int i=s.size()-1; i>=0; --i)
  31.     {
  32.  
  33.         if(s[i]-'0' >=0 && s[i]-'0' <10 )
  34.         {
  35.             flag =true;
  36.             str.push_back(s[i]);
  37.             continue;
  38.         }
  39.         else if(flag)
  40.         {
  41.             flag = false;
  42.             st1.push(atoi( rev(str).c_str() ));
  43.             str.clear();
  44.         }
  45.  
  46.         if(s[i]=='m' || s[i]=='M') st2.push(s[i]);
  47.  
  48.         if(st2.size()>0 && st1.size()>1 )
  49.         {
  50.  
  51.             int a = st1.top();
  52.             st1.pop();
  53.             int b = st1.top();
  54.             st1.pop();
  55.             char c = st2.top();
  56.             st2.pop();
  57.             if(c == 'M')
  58.                 st1.push(max(a,b));
  59.             else
  60.                 st1.push(min(a,b));
  61.  
  62.         }
  63.     }
  64.  
  65.     cout<<st1.top();
  66.  
  67.  
  68.     return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement