Write a program to evaluate postfix expression using stack.

Program 4.2

---------------------------------------------------------------------------------------------------------------------

Program name : stack.cpp

#include<iostream>
using namespace std;
template <class t>
class stack
{
public:
t data;
stack* prev;
};
template<class t>
class stacks
{
stack<t> *top;
public:
stacks()
{
top=NULL;
}
void push(t n)
{
stack<t> *temp;
temp=new stack<t>;
temp->data=n;
temp->prev=top;
top=temp;
}
t pop()
{
t a;
        if(top==NULL)
        {
         cout<<"UNDERFLOW\n";
        }
        else
        {
         a=top->data;
         top=top->prev;
         return a;
        }
        }
}

--------------------------------------------------

Program name : postfix.cpp

#include<iostream>
#include<string.h>
#include<ctype.h>
#include"stack.cpp"
using namespace std;
int main()
{
stacks<int> s1;
int t1,t2,cho;
char ch;
string l;
do
{
cout<<"Enter the postfix notation(enter 0 to terminate):\n";
getline(cin,l);
cho=l.lenght();
do
{
for(int i=0;i<cho;i++)
switch(l[i])
{
case '+': t1=s1.pop();
 t2=s1.pop();
 s1.push(t1+t2);
 break;
case '*': t1=s1.pop();
 t2=s1.pop();
 s1.push(t1*t2);
 break;
case '/': t1=s1.pop();
 t2=s1.pop();
 s1.push(t1/t2);
 break;
case '-': t1=s1.pop();
 t2=s1.pop();
 s1.push(t1-t2);
 break;
case '%': t1=s1.pop();
 t2=s1.pop();
 s1.push(t1%t2);
 break;
default:
 s1.push(l[i]);
}
cout<<"The result after postfix notation solving:\n";
s1.display();
cout<<"\n\n\nWant to enter more:\n";
cin>>ch;
}while(ch=='y');
}

---------------------------------------------------------------------------------------------------------------------

Program Finish.........