this is a code to convert infix string to postfix......
but i am having some problems please help me:(

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

int presidence( );
void infixToPostfix(char *);

template<class T>
class Stack
{
    private:
    T *data;
      int capacity;
      int top;

   public:
    Stack();
      Stack(int );
      T pop();
      void push(T);
      int isEmpty();
      int isFull();
      int getTop();
      ~Stack();
      void display()
      {
        for(int i = 0 ; i < top ; i++)
            cout<<"  " <<data[i];
      }
};


void main()
{
   clrscr();
   char arr[] = "A+B*(C+D-E/F*G)";
   infixToPostfix(arr);
   getch();
}

template<class T>
Stack<T> ::Stack()
{
    capacity=0;
    top=0;
}
template<class T>
Stack<T>::Stack(int s=5)
{
    capacity=s;
    data=new T[capacity];
    top=0;
}
template<class T>
int Stack<T>:: isEmpty()
{
    return (top==0);
}
template<class T>
int Stack <T>:: isFull()
{
    return (top==capacity);
}
template<class T>
void Stack<T>:: push(T p)
{
    if(isFull())
    {
        return;
    }
    else
    {
        data[top]=p;
        top++;
    }
}
template<class T>
T Stack<T>:: pop()
{
  /*    if(isEmpty())
        exit(0);
    else
    {*/
        top--;
        return data[top];
  // }
}
template<class T>
Stack<T>::~Stack()
{
    if(data!=NULL)
    {
        delete[] data;
        data=0;
    }
}
template<class T>
int Stack<T>::getTop()
{
    return top;
}
int presidence(char A,char B)
{
   if(A=='+' && B=='-' || A=='*' && B=='/')
   {
    return 1;
   }

   if(A=='-' && B=='+' || A=='/' && B=='*')
    return 1;

   if(A=='+' && B=='*' || B=='-'||A=='/' && B=='+' ||A=='/' && B=='-')
   {
        return 1;
   }
   if(A=='*' && B=='+' || A=='*' && B=='-')
    return 0;
   return 0;
}


void infixToPostfix(char *infix)
{
    int len=strlen(infix);
    char postfix[20];
    Stack <char> P(len+1);
   int pi = 0;

   for(int i=0;infix[i]!='\0';i++)
   {
        if(infix[i]>='A' && infix[i]<='Z')
      {
            postfix[pi]=infix[i];
            pi++;
      }
      if(infix[i]=='+' ||infix[i]=='-'||infix[i]=='*'||infix[i]=='/')
      {
            if(P.isEmpty() || infix[i-1]=='(')
            {
                P.push(infix[i]);
            }
            else if(!P.isEmpty() || infix[i-1]!='(')
            {
                int no=presidence(infix[i],P.pop());

                while(!P.isEmpty() && no==1)
                {
                    postfix[pi]=P.pop();
               pi++;
                }
                P.push(infix[i]);
                if(infix[i]!=')')
                {
                    P.push(infix[i]);
                }
                else if(infix[i]==')')
                {
                    while(P.pop()!='(')
                    {
                        postfix[pi]=P.pop();
                        pi++;
                }

                }
            }

      }
   }
   for(int i=pi;!P.isEmpty() && i<=len;i++)
   {
        postfix[i]=P.pop();
   }

   for(int j=0;j<=pi;j++)
        cout<<" "<<postfix[j];
}

thanx in advance:)

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.