Rakibul_Ahasan

Infix_to_Postfix_and_Postfix_evaluation.cpp

Aug 22nd, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. /*
  2.     Infix notation to Postfix notation conversion.
  3.     Postfix notation evaluation to get result of that expression.
  4.     Using stack data structure in C++ language.
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. string convertInfixToPostfix(string infix);
  12. int operatorPrecedence(char ch);
  13. int evaluatePostfixExpression(string postfix);
  14. int calculate(int a, int b, char operatorSign);
  15.  
  16. int main()
  17. {
  18.     string infixExpression, postfixExpression;
  19.     int result;
  20.  
  21.     infixExpression = "(1+(2^3)*4)/3"; //"(A+(B^C)*D)/C" 123^4*+3/
  22.  
  23.     postfixExpression = convertInfixToPostfix(infixExpression);
  24.  
  25.     cout<<"Infix Expression: "<<infixExpression<<endl;
  26.     cout<<"Postfix Expression: "<<postfixExpression<<endl;
  27.  
  28.     result = evaluatePostfixExpression(postfixExpression);
  29.  
  30.     cout<<"\nResult is: "<<result<<endl;
  31.  
  32.     return 0;
  33. }
  34.  
  35. //INFIX to POSTFIX conversion
  36. string convertInfixToPostfix(string infix)
  37. {
  38.     string postfix = "";
  39.     stack <char> myStack;
  40.     char ch;
  41.  
  42.     for(int i=0; infix[i]; i++)
  43.     {
  44.         ch = infix[i];
  45.  
  46.         if(ch=='(') ///if found opening bracket
  47.             myStack.push(ch);
  48.         else if(ch==')') ///if found closing bracket
  49.         {
  50.             while(!myStack.empty() && myStack.top()!='(')
  51.             {
  52.                 postfix = postfix + myStack.top();
  53.                 myStack.pop();
  54.             }
  55.  
  56.             if(!myStack.empty() && myStack.top()=='(')
  57.                myStack.pop();
  58.  
  59.         }
  60.         else ///found operator or operand
  61.         {
  62.             int priority = operatorPrecedence(ch); ///^ is highest, then * and /. then + and -
  63.  
  64.             if(priority==0) ///found operand
  65.                 postfix = postfix + ch;
  66.             else ///found operator
  67.             {
  68.                 if(myStack.empty())
  69.                     myStack.push(ch);
  70.                 else
  71.                 {
  72.                     while(!myStack.empty()
  73.                           && myStack.top()!='('
  74.                             && priority<=operatorPrecedence(myStack.top()))
  75.                     {
  76.                         postfix = postfix + myStack.top();
  77.                         myStack.pop();
  78.                     }
  79.                     myStack.push(ch);
  80.                 }
  81.  
  82.             }
  83.         }
  84.     }
  85.  
  86.     while(!myStack.empty())
  87.     {
  88.         postfix += myStack.top();
  89.         myStack.pop();
  90.     }
  91.  
  92.     return postfix;
  93. }
  94.  
  95.  
  96. /// POSTFIX evaluation to get result
  97. int evaluatePostfixExpression(string postfix)
  98. {
  99.     stack <int> myStack;
  100.     char ch;
  101.     ///1 2 3 ^ 4 * + 3 /
  102.     for(int i=0; postfix[i]; i++)
  103.     {
  104.         ch = postfix[i];
  105.         if(ch>='0' && ch<='9') ///found operand
  106.             myStack.push(ch-'0'); ///char to int conversion
  107.         else ///found operator
  108.         {
  109.             int a,b;
  110.             b = myStack.top();
  111.             myStack.pop();
  112.  
  113.             a = myStack.top();
  114.             myStack.pop();
  115.  
  116.             myStack.push(calculate(a, b, ch));
  117.         }
  118.     }
  119.  
  120.     return myStack.top();
  121. }
  122.  
  123.  
  124. /// Calculation, according to arithmetic operator sign
  125. int calculate(int a, int b, char operatorSign)
  126. {
  127.     if(operatorSign=='+')
  128.         return a+b;
  129.     else if(operatorSign=='-')
  130.         return a-b;
  131.    else if(operatorSign=='*')
  132.         return a*b;
  133.    else if(operatorSign=='/')
  134.         return a/b;
  135.      else if(operatorSign=='^')
  136.         return pow(a,b);
  137.  
  138. }
  139.  
  140.  
  141. /// Operator precedence. You can add more operator like % (MOD)
  142. int operatorPrecedence(char ch)
  143. {
  144.     if(ch=='^')
  145.         return 3; ///highest priority for exponential operator
  146.     else if(ch=='/' || ch=='*')
  147.         return 2; ///high priority than + or - operator
  148.     else if(ch=='+' || ch=='-')
  149.         return 1; ///lowest priority among operators
  150.     else
  151.         return 0; ///works for operands
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment