OLiSwEll

Sharing is My Style

Search This Blog

postfix

/*   Write a Program for to evaluate postfix expression    */
char postfix[100];
static i,top;
long int stack[20];
void main()
{
int temp,p;
clrscr();
printf("Enter Postfix Expression : ");
scanf("%s",postfix);

for(i=0;postfix[i]!='\0';i++)
{

if(postfix[i]=='$' || postfix[i]=='^')
{
temp = stack[top-1];
p = stack[top-2];
while(temp>1)
{
stack[top-2] *=p;
temp--;
}
top--;
}
else if(postfix[i] == '+')
{
stack[top-2] += stack[top-1];
top--;
}
else if(postfix[i] == '-')
{
stack[top-2] -= stack[top-1];
top--;
}
else if(postfix[i] == '*')
{
stack[top-2] *= stack[top-1];
top--;
}
else if(postfix[i] == '/')
{
stack[top-2] /= stack[top-1];
top--;
}
else if(postfix[i]!=',')
{
stack[top] = postfix[i]-48;
while(postfix[i+1]!=',')
{
stack[top] *= 10 + postfix[i+1]-48;
i++;
}
top++;
}
}
printf("\nValue of Postfix Expression : %ld",stack[top-1]);
getch();
}






0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Subscribe via email

Enter your email address:

Delivered by FeedBurner

Followers

Infolinks In Text Ads


Bloggers - Meet Millions of Bloggers

Traker

eXTReMe Tracker

online

PageRank Entertainment
Mobilt Bredband Guiden Submit Blog & RSS Feeds