Evaluation of PostFix Expression Program in C Plus Plus
Hello Friends!
In this article I am going to explore the C++ program of the evaluation of postfix expression for you.
We will develop such a program in C++ which on execution will ask the user to enter the postfix expression and then output the value calculated.
I have follow the steps of algorithm of evaluation of postfix expression.
The algorithm of postfix expression is given below.
Evaluation of PostFix Expression Algorithm
This algorithm finds the value of the arithmetic expression P written in postfix notation.
STEPS
- Add a right parenthesis "(" at the end of P. [It will act as sentinel.]
- Scan P from left to right and repeat steps 3 and 4 for each element of P until the sentinel ")" is encountered.
- If an operand is encountered, put it on STACK.
- If an operator is encountered, then;
- Remove the two top elements of STACK, where A is the top element and B is the next to top element.
- Evaluate B and A.
- Place the result of B and A back on STACK.
- [End of If Structure].
- Set VALUE equal to the top element on STACK.
- EXIT.
Evaluation of PostFix Expression Program
// softoset.blogspot.com
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 100
class Evaluation{
private:
char Exp[MAX];
int Stack[MAX];
int Top,item;
int stop;
char sent,ch;
int a,b,c;
public:
void in()
{
cout<<"Enter the Postfix Expression "<<endl;
gets(Exp);
}
void sentinel()
{
cout<<"\nEnter Sentinel : ) Bracket"<<endl;
cin>>sent;
Top=strlen(Exp);
if(Top>=MAX)
{
cout<<"Overflow "<<endl;
exit(1);
}
else
{
Exp[Top]=sent;
Top=Top+1;
}
cout<<"Now ";
}
void scan()
{
int i=0;
while(Exp[i]!=')')
{
ch=Exp[i];
if(isdigit(ch))
{
stack_push(ch-'0');
}
if(Exp[i]=='+'||Exp[i]=='-'||Exp[i]=='*'||Exp[i]=='/'||Exp[i]=='^')
{
a=stack_pop();
b=stack_pop();
switch(Exp[i])
{
case '^':
c=pow(b,a);
break;
case '/':
c=b/a;
break;
case '*':
c=b*a;
break;
case '+':
c=b+a;
break;
case '-':
c=b-a;
break;
default:
cout<<"incorrect operaand "<<endl;
}
stack_push(c);
}
i++;
}
cout<<"\n\n Value = "<<stack_pop()<<endl;
}
int stack_pop()
{
if(stop<=0)
{
cout<<"Underflow "<<endl;
exit(1);
}
else
{
item=Stack[stop];
stop=stop-1;
return item;
}
}
void stack_push(int item)
{
if(stop>=MAX)
{
cout<<"Overflow "<<endl;
exit(1);
}
else
{
stop=stop+1;
Stack[stop]=item;
}
}
void out()
{
cout<<"Expression is "<<endl;
Top=strlen(Exp);
for(int i=0;i<Top;i++)
cout<<Exp[i]<<" ";
}
};
int main()
{
Evaluation obj;
obj.in();
obj.out();
obj.sentinel();
obj.out();
obj.scan();
return 0;
}
Do's and Don't's
Before we show you the output produced from this code some of things to be cleared.
- First don't use characters such as alphabets and special symbols.
- Only use numbers and operators.
- Don't use the more than one figure numbers such as 34 or 89,454 etc.
- Only use single figure numbers.
Evaluation of PostFix Expression Output
And here is the output of above program on DEV C++ IDE.
So, you can see that program ask the user to enter the postfix expression.
User enter the postfix expression.
Program separate each character and ask the user to enter
sentinel ie.
")"
User enter sentinel.
Program now display the expression with sentinel.
And output the result on the screen as variable Value.
You can evaluate any postfix expression using this program.
If troubling in any type of situation.
Comment us below.
We will take the measures.
Thank You
Post a Comment
Feel Free to Comment Us