

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>

struct stack
{
	char c;
	stack *next;
};

void push(stack *&top,char);
char pop(stack *&top);

void main()
{
	char pos[80],inf[80],v;
	stack *top=NULL;
	int j;
   char ch;
	do{
      j=0;
		cout<<"Enter the equation you want conver it to postfix: ";
		gets(inf);
		for( int i=0;i<strlen(inf);i++)
		{
			if(inf[i]>='0'&&inf[i]<='9')
			{
				pos[j]=inf[i];
				j++;
			}


         else if(inf[i]=='+'||inf[i]=='-')
				{
					if(top!=NULL&&(top->c=='*'||top->c=='/'))
					{
						while(top!=NULL&&top->c!='(')
						{
							v=pop(top);
							pos[j]=v;
							j++;
						}
						push(top,inf[i]);
					}
					else
						push(top,inf[i]);
				}
				else if(inf[i]==')')
				{
					while(top->c!='(')
					{
						v=pop(top);
						pos[j]=v;
						j++;
					}
					stack *temp=top;
					top=top->next;
					delete(temp);
				}
         else if(inf[i]=='*'||inf[i]=='/'||inf[i]=='(')
					push(top,inf[i]);
		}//end for
		while(top!=NULL)
		{
			v=pop(top);
			pos[j]=v;
			j++;
		}

		pos[j]='\0';
		cout<<"The equation after convert it to postfix: ";
		cout<<pos<<endl;


		for(int j=0;j<strlen(pos);j++)
			{
			   if(pos[j]>='0'&&pos[j]<='9')
				   push(top,pos[j]);
			   else
			   {
               int a,b,c;
				   v=pop(top);
				   a=v-48;
				   v=pop(top);
				   b=v-48;
				   if(pos[j]=='+')
					   c=a+b;
				   else if(pos[j]=='-')
					   c=a-b;
				   else if(pos[j]=='*')
					   c=a*b;
				   else if(pos[j]=='/')
					   c=a/b;
				   v=c+48;
				   push(top,v);
			   }
            }
			   v=pop(top);
			   int result=v-48;

			   cout<<"\nThe result:"<<result;


		cout<<"\nAre you want continue?Y/N ";
		cin>>ch;
	}while(ch=='Y'||ch=='y');
   getch();
}//end main
//----------------------------

void push(stack *&top,char a)
{
	stack *temp;
	temp=new stack;
	temp->c=a;
	temp->next=top;
	top=temp;
}//end function;

char pop(stack *&top)
{
	stack *temp;
	char v;
      v=top->c;
      temp=top;
		top=top->next;
		delete(temp);
      return v;
}//end function;


