/* 14. Write a C program to create a class called EXPRESSION. Accept an arithmetic expression (assumed to be in valid INFIX form) and assign to EXPRESSION object. Convert the expression in the object to POSTFIX form by writing appropriate member functions. Display the results. */


#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
class EXPRESSION 
{
    char stack[30],infix[30],postfix[30];
    int top;

    public:
        EXPRESSION()
        {
            top = -1;
        }
        EXPRESSION(char *txt)
        {
            strcpy(infix,txt);
        }
        void ReadInfix()
        {
            cout<<"\nEnter an infix expression : ";
            cin>>infix;
        }
        int Operand(char x)
        {
            return (((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'))?1:0);
        }
        int ISP(char x)
        {
            int y;
            y = (x == '(')?0:(x == '/')?4:(x == '*')?4:(x == '+')?2:(x == '-')?2:-1;
            return y;
        }
        int ICP(char x)
        {
            int y;
            y = (x == '(')?4:(x == '/')?4:(x == '*')?4:(x == '+')?2:(x == '-')?2:-1;
            return y;
        }
        void InfixtoPostfix();
        void Push(char x);
        int Pop()
        {
            return (stack[top--]);
        }
        void Display()
        {        
            cout<<"\The postfix expression is : "<<postfix;
        }
};
void EXPRESSION::Push(char x)
{
    stack[++top] = x;
}
void EXPRESSION::InfixtoPostfix()
{
    char x,y;
    int j=0;
    stack[++top] = '\0';
    for (int i=0; (x = infix[i])!='\0'; i++)
        if (Operand(x))
            postfix[j++] = x;
        else if (x == ')')
        while ((y=Pop()) != '(')
            postfix[j++] = y;
        else
        {
            while (ISP(stack[top]) >= ICP(x))
                postfix[j++] = Pop();
            Push(x);
        }
        while (top >= 0)
            postfix[j++] = Pop();
}
void main()
{
    char *s;
    clrscr();
    EXPRESSION instr;
    instr.ReadInfix();
    instr.InfixtoPostfix();
    instr.Display();
    getch();
}