$$$ POSTFIX EVALUATION $$$
PROGRAM:
#include<iostream>
#include<string.h>
int
stack[20],top=-1;
int
len,a,b,c,i,x;
char expr[20];
class postfix
{
public:
void evaluate()
{
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
{
b=pop();
a=pop();
switch(expr[i])
{
case '+':c=a+b;
push(c);
break;
case '-':c=a-b;
push(c);
break;
case '*':c=a*b;
push(c);
break;
case '/':c=a/b;
push(c);
break;
}
}
else
{
cout<<"Enter
the value of"<<expr[i]<<":";
cin>>x;
push(x);
}
}
}
void push(int item)
{
stack[++top]=item;
}
int pop()
{
int a;
a=stack[top];
top=top-1;
return a;
}
};
void main()
{
postfix p;
cout<<"$$$ POSTFIX EVALUATION
$$$";
cout<<"\nEnter the postfix
expression:";
cin>>expr;
p.evaluate();
cout<<"The resultant value is
:"<<stack[top];
}
OUTPUT:
$$$ POSTFIX EVALUATION $$$
Enter the postfix expression: abc+*
Enter the value of a: 10
Enter the value of b: 20
Enter the value of c: 30
The resultant value is : 500
$$$ POSTFIX EVALUATION $$$
Enter the postfix expression: abc+*
Enter the value of a: 10
Enter the value of b: 20
Enter the value of c: 30
The resultant value is : 500
No comments:
Post a Comment