Sunday, 30 October 2011

EVALUATION OF POSTFIX EXPRESSION


#include<stdio.h>
#include<ctype.h>
#include<string.h>
main()
{
char infix[20];
float eval(char *);
clrscr();
printf("\nenter a valid infix expression:-\n");
gets(infix);
strrev(infix);
printf("\nresult of evaluation=%f\n",eval(infix));
getch();
}
float eval(char *infix)
{
float stack[20],x1,x2;
int i=0,top=-1;
char ch;
while((ch=infix[i++])!='\0')
{
if(isdigit(ch))
stack[++top]=ch-'0';
else
{
x1=stack[top--];
x2=stack[top--];
switch(ch)
{
case '+':stack[++top]=x1+x2;
break;
case '-':stack[++top]=x1-x2;
break;
case '*':stack[++top]=x1*x2;
break;
case '/':stack[++top]=x1/x2;
break;
}
     }
   }
  return(stack[top]);
 }

No comments:

Post a Comment