Seal_of_approval

PrStack7

Jul 2nd, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <vector>
  5. #include <stack>
  6. #include <sstream>
  7. using namespace std;
  8.  
  9. string Subst(string& initial, char begin, char end)
  10. {
  11.     long idxBegin = initial.find(begin) + 1;
  12.     long idxEnd = initial.rfind(end);
  13.     return initial.substr(idxBegin, idxEnd - idxBegin);
  14. }
  15.  
  16. void evalPopOrTop(string& _current, vector<stack<long>>& _as, bool doPop)
  17. {
  18.     istringstream strUsedStack(Subst(_current, '(', ')'));
  19.     long idxUsedStack;
  20.     strUsedStack >> idxUsedStack;
  21.    
  22.     idxUsedStack--;
  23.  
  24.     cout << _as[idxUsedStack].top() << " ";
  25.     if (doPop) _as[idxUsedStack].pop();
  26. }
  27.  
  28. void evalPush(string& _current, vector<stack<long>>& _as)
  29. {
  30.     istringstream strUsedStack(Subst(_current, '(', ','));
  31.     istringstream strValue(Subst(_current, ',', ')')); 
  32.     long idxUsedStack, value;
  33.     strUsedStack >> idxUsedStack;
  34.     strValue >> value;
  35.  
  36.     idxUsedStack--;
  37.  
  38.     _as[idxUsedStack].push(value);
  39. }
  40.  
  41.  
  42. long main(void)
  43. {
  44.     freopen("input.txt", "r", stdin);
  45.     freopen("output.txt", "w", stdout);
  46.  
  47.     long stackCount, opCount;
  48.     cin >> stackCount >> opCount;
  49.     vector<stack<long> > allStacks(stackCount);
  50.  
  51.     string _current;
  52.     getline(cin, _current);
  53.     for (long i = 0; i < opCount; i++)
  54.     {
  55.         getline(cin, _current);
  56.         if (_current[0] == 'T') evalPopOrTop(_current, allStacks, false);
  57.         else if (_current[1] == 'O') evalPopOrTop(_current, allStacks, true);
  58.         else evalPush(_current, allStacks);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment