Search This Blog

Monday, March 17, 2014

POSTFIX Evaluation


POSTFIX Evaluation


#include<stdio.h>
#include<conio.h>
#include <ctype.h>
int s[20];
int top=-1;       
push(int elem)
 top++;                    
 s[top]=elem;
}
int pop()
{                      
 return(s[top--]);
}
void main()
{                         
 char pofx[50],ch;
 int i=0,op1,op2;
 printf("\n\nRead the Postfix Expression ? ");
 scanf("%s",pofx);
 while( (ch=pofx[i++]) != '\0')
 {
  if(isdigit(ch)) 
   push(ch-'0'); 
  else
  {        
   op2=pop();
   op1=pop();
   switch(ch)
   {
   case '+':
   push(op1+op2);
    break;
   case '-':
   push(op1-op2); 
   break;
   case '*':
   push(op1*op2);
   break;
   case '/':
   push(op1/op2); 
   break;
   }
  }
 }
 printf("\n Given Postfix Expn: %s\n",pofx);
 printf("\n Result after Evaluation: %d\n",s[top]);
getch();
}

No comments:

Post a Comment