about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.
Evaluation of PostFix Expression Program in C Plus Plus


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

  1. Add a right parenthesis "(" at the end of P.  [It will act as sentinel.]
  2. Scan P from left to right and repeat steps 3 and 4 for each element of P until the sentinel ")" is encountered.
  3. If an operand is encountered, put it on STACK.
  4. 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].
  5. Set VALUE equal to the top element on STACK.
  6. EXIT.
So, this is the algorithm and now will transform it to code or program so here is the conversion of this program in C++ language.


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.

  1. First don't use characters such as alphabets and special symbols.
  2. Only use numbers and operators.
  3. Don't use the more than one figure numbers such as 34 or 89,454 etc.
  4. Only use single figure numbers.
Evaluation of PostFix Expression Output

And here is the output of above program on DEV C++ IDE.



Evaluation of PostFix Expression Output


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

Previous Post Next Post

Recents