evaluation implementation of postfix notation Data Structure developerIndian.com

Updated:13/01/2024 by Computer Hope

Go Back



A postfix expression is a group of operators and operands in which the operator is placed after the operands. That means, in a postfix expression the operator follows the operands.
consider the postfix expression 4 5 6 * +. As you scan the expression from left to right, you first encounter the operands 4 and 5. At this point, you are still unsure what to do with them until you see the next symbol. Placing each on the stack ensures that they are available if an operator comes next. Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned

#include<stdio.h>
#include<conio.h>                                            
#include<ctype.h>
# define MAX 50
float stack [MAX];int top = -1;

void  push (float  val)
  { if (top == MAX -1)
       return ;
     stack [++top]=val;
   }

float  pop( )
  { if(top==-1)
        return -0.0;
    return stack[top--];
  }



void main()
 {char post[50];
   int i=0;
   float A,B,C;
   clrscr();
   printf("\n Enter any expression in post notation(in Numeric Form):");
   scanf("%s",post);
   while(post[i] != '\0')
     { if( isdigit(post[i]) )
	push(post[i]-48);
     else if(post[i] != ' ')
       { A=pop();
          B=pop();
          if(post[i]=='+')
            C=B+A;
          else if(post[i]=='-')
             C=B-A;
          else if(post[i]=='*')
             C=B*A;
          else if(post[i]=='/')
             C=B/A;
          push(C);
        }
      i++;
     }//end of loop
    printf("\n Answer Of Postfix Expression=%f", pop() );
  }

Conclusion

In this example, the evaluate_postfix function takes a postfix expression as input and returns the result of the evaluation. It uses a stack to keep track of operands and performs operations based on the operators encountered in the postfix expression.
The example postfix expression is "23*5+", and the result is obtained using the evaluate_postfix function.