#include "iostream.h"
#include "conio.h"
#include "math.h"
int compare(char );
//_____________________________________________________
//******************** Class stack ********************
class stack{
private:
char s1[100];
int top;
public:
stack();
void push(char);
char pop();
int IsEmpty();
int IsFull();
};
stack::stack(){
top=-1;
}
void stack::push(char x)
{
top++;
if(IsFull())
cout<<"stack is full";
else
s1[top]=x;
}
int stack::IsFull()
{
if(top==100)
return 1;
else
return 0;
}
char stack::pop()
{
if(IsEmpty())
{
cout<<"stack is empty";
return -1;
}
else{
int x = s1[top];
top--;
return x;
}
}
int stack::IsEmpty()
{
if(top==-1)
return 1;
return 0;
}
//***************** End of class stack *****************
//______________________________________________________
int main()
{
stack s1;
char end[50],ch;
int k=0,j=0,v;
cout<<"\n\n\t\t\t********************************";
cout<<"\n\n\t\t\t\tInfix to Postfix";
cout<<"\n\n\t\t\t********************************";
cout<<"\n\n\n\aEnter infix expression: \n\n";
while((ch=cin.get())!='\n')
{char h;
if(('A'<=ch && ch<='Z') || ('a'<=ch && ch<='z'))
{
end[j]=ch;
j++;
}
else
{
if(s1.IsEmpty())
s1.push(ch);
else
{
h=s1.pop();
if(compare(ch)>compare(h))
{
s1.push(h);
s1.push(ch);
}
else
{
while(compare(ch)<=compare(h))
{
end[j]=h;
j++;
if(s1.IsEmpty())
{
s1.push(ch);
break;
}
else
h=s1.pop();
}
if(compare(ch)>compare(h))
{
s1.push(h);
s1.push(ch);
}
}
}
}
}
while(true)
{
if(s1.IsEmpty())
break;
end[j]=s1.pop();
j++;
}
cout<<"\n\n\a\aPostfix notation :\n\n";
for(k=0;k<j;k++)
cout<<end[k];
getch();
return 0;
}
//_____________________________________________________________
//************************* Function *************************
int compare(char e)
{
int c;
switch(e)
{
case '^': c=3; break;
case '*': c=2; break;
case '/': c=2; break;
case '%': c=2; break;
case '+': c=1; break;
case '-': c=1; break;
default: c=-1; break;
}
return c;
}
//********************** End of function **********************
//______________________________________________________________