m2skills

in2pre cpp

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