Haval1

infix to postfix

Feb 23rd, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. /* This program is for evaluation of postfix expression
  2. * This program assume that there are only four operators
  3. * (*, /, +, -) in an expression and operand is single digit only
  4. * Further this program does not do any error handling e.g.
  5. * it does not check that entered postfix expression is valid
  6. * or not.
  7. * */
  8.  
  9. #include "stdafx.h"
  10. #include "iostream"
  11. #include "cctype"
  12. using namespace std;
  13.  
  14.  
  15. # define MAXSTACK 100        /* for max size of stack */
  16. # define POSTFIXSIZE 100     /* define max number of charcters in postfix expression */
  17.  
  18. /* declare stack and its top pointer to be used during postfix expression
  19. evaluation*/
  20. int stack[MAXSTACK];
  21. int top = -1;             /* because array index in C begins at 0 */
  22.                           /* can be do this initialization somewhere else */
  23.  
  24.                           /* define push operation */
  25. void push(int item)
  26. {
  27.  
  28.     if (top >= MAXSTACK - 1)
  29.     {
  30.         cout<<"stack over flow\n";
  31.         return;
  32.     }
  33.     else
  34.     {
  35.         top = top + 1;
  36.         stack[top] = item;
  37.     }
  38. }
  39.  
  40. /* define pop operation */
  41. int pop()
  42. {
  43.     int item;
  44.     if (top <0)
  45.     {
  46.         cout<<"stack under flow\n";
  47.     }
  48.     else
  49.     {
  50.         item = stack[top];
  51.         top = top - 1;
  52.         return item;
  53.     }
  54. }
  55.  
  56. /* define function that is used to input postfix expression and to evaluate it */
  57. void EvalPostfix(char postfix[])
  58. {
  59.  
  60.     int i;
  61.     char ch;
  62.     int val;
  63.     int A, B;
  64.  
  65.  
  66.     /* evaluate postfix expression */
  67.     for (i = 0; postfix[i] != ')'; i++)
  68.     {
  69.         ch = postfix[i];
  70.         if (isdigit(ch))
  71.         {
  72.             /* we saw an operand,push the digit onto stack
  73.             ch - '0' is used for getting digit rather than ASCII code of digit */
  74.             push(ch - '0');
  75.         }
  76.         else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
  77.         {
  78.             /* we saw an operator
  79.             * pop top element A and next-to-top elemnet B
  80.             * from stack and compute B operator A
  81.             */
  82.             A = pop();
  83.             B = pop();
  84.  
  85.             switch (ch) /* ch is an operator */
  86.             {
  87.             case '*':
  88.                 val = B * A;
  89.                 break;
  90.  
  91.             case '/':
  92.                 val = B / A;
  93.                 break;
  94.  
  95.             case '+':
  96.                 val = B + A;
  97.                 break;
  98.  
  99.             case '-':
  100.                 val = B - A;
  101.                 break;
  102.             }
  103.  
  104.             /* push the value obtained above onto the stack */
  105.             push(val);
  106.         }
  107.     }
  108.     cout<<" \nResult of expression evaluation : "<< pop()<<endl;
  109. }
  110.  
  111. int main()
  112. {
  113.  
  114.     int i;
  115.  
  116.     /* declare character array to store postfix expression */
  117.     char postfix[POSTFIXSIZE];
  118.     cout<<"ASSUMPTION: There are only four operators(*, /, +, -) in an expression and operand is single digit only.\n";
  119.     cout<<" \nEnter postfix expression,\npress right parenthesis ')' for end expression : ";
  120.  
  121.     /* take input of postfix expression from user */
  122.  
  123.     for (i = 0; i <= POSTFIXSIZE - 1; i++)
  124.     {
  125.         cin>>postfix[i];
  126.  
  127.         if (postfix[i] == ')') /* is there any way to eliminate this if */
  128.         {
  129.             break;
  130.         } /* and break statement */
  131.     }
  132.  
  133.     /* call function to evaluate postfix expression */
  134.  
  135.     EvalPostfix(postfix);
  136.  
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment