Sunday, July 4, 2010

PROGRAM TO EVALUATE POSTFIX EXPRESSION(DS)

Buzz It
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char a[10];
int b[10],i,n,q,c=0,r;
clrscr();
cout<<"enter the postfix expression\n";
gets(a);
n=strlen(a);
for(i=0;i<n-1;i++)
{
if(a[i]!='+'&&a[i]!='-'&&a[i]!='*'&&a[i]!='/')
{
c++;
cout<<"enter the value for\t"<<a[i]<<"\n";
cin>>b[i];
}}
r=n-c;
q=c-r;
if(q!=1)
{c
out<<"the expression is invalid\n";
getch();
}
else
{
c=c-1;
for(i=n-1;i>=0;i--)
{
switch (a[i])
{
case '+':b[c-1]=b[c-1]+b[c];
c=c-1;
break;
case '-':b[c-1]=b[c-1]-b[c];
c=c-1;
break;
case '*':b[c-1]=b[c-1]*b[c];
c=c-1;
break;
case '/':b[c-1]=b[c-1]/b[c];
c=c-1;
break;
}}
cout<<"the result is\n"<<b[c];
}
getch();
}


OUTPUT:
enter the postfix expression
abc+-
enter the value for a
10
enter the value for b
20
enter the value for c
10
the result is
20

0 comments:

Post a Comment