Advertisement
Guest User

Application using Stack

a guest
Nov 11th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1.  
  2. /* C++ implementation to convert infix expression to postfix*/
  3.  
  4. #include
  5. using namespace std;
  6.   
  7. //Function to return precedence of operators
  8.  
  9. int prec(char c)
  10. {
  11.     if(c == '^')
  12.     return 3;
  13.     else if(c == '*' || c == '/')
  14.     return 2;
  15.     else if(c == '+' || c == '-')
  16.     return 1;
  17.     else
  18.     return -1;
  19. }
  20. // The main function to convert infix expression
  21. //to postfix expression
  22.  
  23. void infixToPostfix(string st)
  24. {
  25.  stack s;
  26.  clear(s);
  27.  int length = st.length();
  28.  string ns;
  29.  for(int i = 0; i < length; i++)
  30.  {
  31.   //If the scanned character is an operand, add it to output string.
  32.   if((st[i] >= 'a' && st[i] <= 'z')||
  33.      (st[i] >= 'A' && st[i] <= 'Z'))
  34.         ns+=st[i];
  35.   
  36.    // If the scanned character is an ‘(‘, push it to the stack.
  37.         else if(st[i] == '(')
  38.           
  39.         push(s,'(');
  40.           
  41.    // If the scanned character is an ‘)’, pop and to output string from the stack
  42.         // until an ‘(‘ is encountered.
  43.         else if(st[i] == ')')
  44.         {
  45.             while(!empty(s) && stacktop(s) != '(')
  46.             {char c = pop(s);
  47.              ns += c;}
  48.             if(stacktop(s) == '(')
  49.               char c = pop(s);
  50.         }
  51.         //If an operator is scanned
  52.         else {
  53.                while(!empty(s) &&
  54.                      prec(st[i]) <= prec(stacktop(s)))
  55.                  {char c = pop(s);
  56.                   ns += c;}
  57.                push(s, st[i]);
  58.         }
  59.       }
  60.     //Pop all the remaining elements from the stack
  61.     while(!empty(s))
  62.       {char c = pop(s);
  63.         ns += c;}
  64.     cout << ns << endl;
  65. }
  66. //Driver program to test above functions
  67. int main()
  68. {
  69.     string exp = "a+b*(c^d-e)^(f+g*h)-i";
  70.     infixToPostfix(exp);
  71. }
  72.  
  73. Evaluation of Postfix Expression
  74.  
  75. // C++ program to evaluate value of a postfix expression  
  76. #include <iostream>  
  77. #include <string.h>  
  78.  using namespace std;
  79. // The main function that returns value of a given postfix expression  
  80. int evaluatePostfix(char* exp)  
  81. {  
  82.   int i;  
  83.   stack s;
  84.     // Scan all characters one by one  
  85.     for (i = 0; exp[i]; ++i)  
  86.     {  // If the scanned character is an operand (number here),  push it to the stack.  
  87.        if (isdigit(exp[i]))  
  88.             push(stack, exp[i] - '0');  
  89.        // If the scanned character is an operator, pop two  
  90.       // elements from stack apply the operator  
  91.         else
  92.         {  
  93.             int val1 = pop(stack);  
  94.             int val2 = pop(stack);  
  95.             switch (exp[i])  
  96.             {  
  97.             case '+': push(stack, val2 + val1); break;  
  98.             case '-': push(stack, val2 - val1); break;  
  99.             case '*': push(stack, val2 * val1); break;  
  100.             case '/': push(stack, val2/val1); break;  
  101.             }  
  102.         }  
  103.     }  
  104.     return pop(stack);  
  105. }  
  106.  
  107. // Driver program to test above functions  
  108. int main()  
  109. {   char exp[] = "231*+9-";  
  110.     cout<<"postfix evaluation: "<< evaluatePostfix(exp);  
  111. }  
  112. Output:  postfix evaluation: -4
  113.  
  114. There are following limitations of above implementation.
  115. 1) It supports only 4 binary operators ‘+’, ‘*’, ‘-‘ and ‘/’.
  116.    It can be extended for more operators by adding more switch cases.
  117. 2) The allowed operands are only single digit operands. The program
  118.    can be extended for multiple digits by adding a separator like
  119.    space between all elements (operators and operands) of given expression.
  120. // The main function that returns value  of a given postfix expression  
  121. int evaluatePostfix(char* exp)  
  122. {  for (i = 0; exp[i]; ++i)   // Scan all characters one by one  
  123.     {         //if the character is blank space then continue  
  124.         if(exp[i] == ' ') continue;  
  125.     // If the scanned character is an operand (number here),extract the full number  
  126.         // Push it to the stack.  
  127.         else if (isdigit(exp[i]))  
  128.         {  
  129.             int num=0;  
  130.             //extract full number  
  131.             while(isdigit(exp[i]))  
  132.             {   num = num * 10 + (int)(exp[i] - '0');  
  133.                 i++;  
  134.             }  
  135.             i--;    
  136.             //push the element in the stack  
  137.             push(stack,num);  
  138.         }        
  139.         // If the scanned character is an operator, pop two  
  140.         // elements from stack apply the operator  
  141.         else
  142.         {  
  143.             int val1 = pop(stack);  
  144.             int val2 = pop(stack);  
  145.             switch (exp[i])  
  146.             {          case '+': push(stack, val2 + val1); break;  
  147.                 case '-': push(stack, val2 - val1); break;  
  148.                 case '*': push(stack, val2 * val1); break;  
  149.                 case '/': push(stack, val2/val1); break;
  150.             }  
  151.         }  
  152.     }  
  153.     return pop(stack);  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement