Advertisement
Guest User

Untitled

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