m2skills

in2post cpp

Mar 29th, 2017
3,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.26 KB | None | 0 0
  1. /* Program to perform all types of stack conversions */
  2. #include<iostream>
  3. #include<string>
  4. #define MAX 20
  5. using namespace std;
  6.  
  7.  
  8. void push(char);
  9. char pop();
  10. string in2postfix(string);
  11. int priority ( char );
  12. char stk[20];
  13. int top=-1;
  14.  
  15.  
  16. int main()
  17. {
  18.     int cont;
  19.     string infix, postfix;
  20.     cout<<"\n enter the infix expression : ";
  21.     //cin>>infix;
  22.     postfix = in2postfix("a+b*c/(d-e)+f");
  23.     return 0;
  24. }
  25.  
  26. // method that pushes the elements onto the character stack
  27. void push(char oper)
  28. {
  29.     if(top==MAX-1)
  30.     {
  31.         cout<<"stackfull!!!!";
  32.     }
  33.    
  34.     else
  35.     {
  36.         top++;
  37.         stk[top]=oper;
  38.     }
  39. }
  40.  
  41. // method that removes character from stack and returns them
  42. char pop()
  43. {
  44.     char ch;
  45.     if(top==-1)
  46.     {
  47.         cout<<"stackempty!!!!";
  48.     }
  49.     else
  50.     {
  51.         ch=stk[top];
  52.         stk[top]='\0';
  53.         top--;
  54.         return(ch);
  55.     }
  56.     return 0;
  57. }
  58.  
  59. // method that converts String from infix to postfix
  60. // all the strings are assumed to be valid expressions
  61. string in2postfix(string infix)
  62. {
  63.     int i=0;
  64.     string postfix = "";
  65.  
  66.     // iterating while end of string is not found
  67.     while(infix[i]!='\0')
  68.     {
  69.         // if an alphabet is found then copy it to the output string
  70.         if(infix[i]>='a' && infix[i]<='z')          
  71.         {
  72.             postfix.insert(postfix.end(),infix[i]);
  73.             i++;
  74.         }
  75.  
  76.         // if an opening bracket is found then put it in stack
  77.         else if(infix[i]=='(' || infix[i]=='{'  || infix[i]=='[')
  78.         {
  79.             push(infix[i]);
  80.             i++;
  81.         }
  82.  
  83.         // if an closing bracket is found then
  84.         // keep removing the operators from the stack and add them to postfix string until you find the corresponding opening bracket
  85.         else if(infix[i]==')' || infix[i]=='}'  || infix[i]==']')
  86.         {
  87.             if(infix[i]==')')
  88.             {
  89.                 while(stk[top]!='(')
  90.                 {
  91.                     postfix.insert(postfix.end(),pop());
  92.                 }
  93.                 pop();
  94.                 i++;
  95.             }
  96.  
  97.             if(infix[i]==']')
  98.             {
  99.                 while(stk[top]!='[')
  100.                 {
  101.                     postfix.insert(postfix.end(),pop());
  102.                 }
  103.                 pop();
  104.                 i++;
  105.             }
  106.  
  107.             if(infix[i]=='}')
  108.             {
  109.                 while(stk[top]!='{')
  110.                 {
  111.                     postfix.insert(postfix.end(),pop());
  112.                 }
  113.                 pop();
  114.                 i++;
  115.             }
  116.         }
  117.  
  118.         // if none of the above cases are satisfied then we surely have an operator
  119.         else            
  120.         {
  121.  
  122.             // if the stack if empty then we simply put the operator in stack
  123.             if(top==-1)
  124.             {
  125.                 push(infix[i]);
  126.                 i++;
  127.             }
  128.  
  129.             // if the priority of current operator is less than or equal to the stack top then
  130.             // pop the stack top and add it to the postfix string
  131.             else if( priority(infix[i]) <= priority(stk[top])) {
  132.                 postfix.insert(postfix.end(),pop());
  133.                
  134.                 // now if you have an operator that has equal priority as of current operator then pop
  135.                 while(priority(stk[top]) == priority(infix[i])){
  136.                     postfix.insert(postfix.end(),pop());
  137.                     if(top < 0) {
  138.                         break;
  139.                     }
  140.                 }
  141.                 push(infix[i]);
  142.                 i++;
  143.             }
  144.            
  145.             // if the priority of current operator if high then push it onto the stack
  146.             else if(priority(infix[i]) > priority(stk[top])) {
  147.                 push(infix[i]);
  148.                 i++;
  149.             }
  150.         }
  151.     }
  152.  
  153.     // at the end remove all the operators from the stack
  154.     while(top!=-1)
  155.     {
  156.         postfix.insert(postfix.end(),pop());
  157.     }
  158.     cout<<"The converted postfix string is : "<<postfix;
  159.     return postfix;
  160. }
  161.  
  162. // method that returns priority for operators according to their precedence
  163. int priority ( char alpha )
  164. {
  165.     if(alpha == '+' || alpha =='-')
  166.     {
  167.         return(1);
  168.     }
  169.  
  170.     if(alpha == '*' || alpha =='/')
  171.     {
  172.         return(2);
  173.     }
  174.  
  175.     if(alpha == '$')
  176.     {
  177.         return(3);
  178.     }
  179.  
  180.     return 0;
  181. }
Add Comment
Please, Sign In to add comment