Advertisement
Guest User

Infix to Postfix

a guest
Jan 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. bool is_operator(char x);
  2.  
  3. int main()
  4. {
  5.    
  6.     string expression;
  7.     cout<<"\tEnter Infix Expression: \n\t-->";
  8.     getline(cin,expression);
  9.    
  10.  
  11.     string postfix = Infix_Postfix(expression);
  12.    
  13.     cout<<"\n\tCorresponding Postfix Expression: \n\t-->"<<postfix;
  14.    
  15.    
  16.    
  17.  
  18.     return 0;
  19. }
  20.  
  21. bool is_operator(char x)
  22. {
  23.     if (x == '*' || x == '/' || x == '+' || x == '-')
  24.       {
  25.         return true;
  26.       }
  27.     else
  28.       {
  29.         return false;
  30.       }
  31. }
  32.  
  33.  
  34. string Infix_Postfix(string X)
  35. {
  36.     STACK<char> s = STACK<char>();
  37.     s.push('[');
  38.     X.append("]");
  39.    
  40.     string Y;
  41.     int index = 0;
  42.    
  43.     while(!s.is_empty())
  44.     {
  45.         for(int i = 0; i < X.length(); i++)
  46.             {
  47.                
  48.                 if(X[i] == '(')
  49.                     {
  50.                         s.push(X[i]);
  51.                     }
  52.                 else if (X[i] == ')')
  53.                         {
  54.                             while(!s.is_empty() && s.get_top() != '(')
  55.                                 {
  56.                                     Y = Y + s.get_top();
  57.                                     s.pop();
  58.                                 }
  59.                             s.pop();
  60.                         }  
  61.                 else if (is_operator(X[i]))
  62.                     {
  63.                         if(X[i] == '+' || X[i] == '-')
  64.                             {
  65.                                 while(s.get_top() == '*' || s.get_top() == '/' || s.get_top() == '+' || s.get_top() == '-')
  66.                                     {
  67.                                         Y = Y + s.get_top();
  68.                                         s.pop();
  69.                                     }
  70.                                 s.push(X[i]);  
  71.                             }
  72.                         else if (X[i] == '*' || X[i] == '/')
  73.                             {
  74.                                 while(s.get_top() == '*' || s.get_top() == '/' )
  75.                                     {
  76.                                         Y = Y + s.get_top();
  77.                                         s.pop();
  78.                                     }
  79.                                 s.push(X[i]);  
  80.                             }
  81.                                
  82.                     }
  83.                 else if (X[i] == ']')
  84.                     {
  85.                         while(s.get_top() != '[')
  86.                             {
  87.                                 Y = Y + s.get_top();
  88.                                 s.pop();
  89.                             }
  90.                         s.pop();   
  91.                     }
  92.                 else
  93.                     Y = Y + X[i];          
  94.             }
  95.         return Y;  
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement