55)Write a C program to read a postfix expression,evaluate it and display the result.


#include<stdio.h>
#include<conio.h>
#include<math.h>
char st[100];
int top=-1;
void push(char c)
{
top++;
st[top]=c;
}
char pop()
{
char c;
c=st[top];
top–;
return c;
}
int main()
{
int i,j,p,a,b,temp;
char s1[30];
clrscr();
printf(“\nEnter the Postfix Expr”);
gets(s1);
j=0;
for(i=0;s1[i]!=”;i++)
{
if(s1[i]<=’9′ && s1[i]>=’0′)
push(s1[i]-48);
else
{
a=pop();
b=pop();
switch(s1[i])
{
case ‘+’:
temp=b+a;
break;
case ‘-‘:
temp=b-a;
break;
case ‘/’: temp=b/a;
break;
case ‘*’:
temp=b*a;
break;
case ‘^’:
temp=pow(b,a);
break;
case ‘%’:
temp=b%a;
break;
}
push(temp);
}
}
j=pop();
printf(“Result is %d “,j);
getch();
return;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s