Advertisement
Riposati

shunting yard

Nov 4th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     vector<char>output;
  8.     list<char>stack;
  9.     list<char>::iterator it = stack.begin();
  10.  
  11.     string expr("2+2*3+3+5");
  12.  
  13.     map<char,int> op_precedence;
  14.  
  15.     op_precedence['+'] = 10;
  16.     op_precedence['-'] = 10;
  17.     op_precedence['*'] = 20;
  18.     op_precedence['/'] = 20;
  19.  
  20.     for(char &c : expr){
  21.  
  22.         if(isdigit(c)){
  23.             output.push_back(c);
  24.         }else{
  25.  
  26.             if(stack.size() > 0){
  27.  
  28.                 if(op_precedence[stack.front()] >= op_precedence[c]){
  29.                     output.push_back(stack.front());
  30.                     stack.pop_front();
  31.                 }
  32.             }
  33.             stack.push_back(c);
  34.         }
  35.     }
  36.  
  37.     for(it = stack.begin(); it!= stack.end(); it++){
  38.         output.push_back(*it);
  39.     }
  40.  
  41.     for(auto &i : output){
  42.         cout<<i<<' ';
  43.     }
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement