C++ Programming Blog

 
 
 

Image

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

 int top=-1;

 char Stack[100]={NULL};

 void push(const char);
 const char pop( );

 void infix_to_postfix(const char *);

 int main( )
    {
       clrscr( );

       fstream File(\"CP-15.txt\",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<\"\\n Unable to open the input file.\"<<endl;
         cout<<\"\\n Press any key to exit.\";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[10]={NULL};
       char Infix_expression[100]={NULL};

       do
      {
         File.getline(Data,5);

         if(strcmp(Data,NULL)==0)
        break;

         strcat(Infix_expression,Data);
      }
       while(1);

       File.close( );

       infix_to_postfix(Infix_expression);

       getch( );
       return 0;
    }


 //-----------------------------  push(const char)  ----------------------//


 void push(const char Symbol)
    {
       if(top==99)
      cout<<\"Error : Stack is full.\"<<endl;

       else
      {
         top++;
         Stack[top]=Symbol;
      }
    }


 //--------------------------------  pop( )  -----------------------------//


 const char pop( )
    {
       char Symbol=NULL;

       if(top==-1)
      cout<<\"Error : Stack is empty.\"<<endl;

       else
      {
         Symbol=Stack[top];
         Stack[top]=NULL;
         top--;
      }

       return Symbol;
    }


 //---------------------  infix_to_postfix(const char *)  ----------------//


 void infix_to_postfix(const char *Infix)
    {
       char Infix_expression[100]={NULL};
       char Postfix_expression[100]={NULL};

       strcpy(Infix_expression,\"(\");
       strcat(Infix_expression,Infix);
       strcat(Infix_expression,\")\");

       char Symbol[5]={NULL};
       char Temp[5]={NULL};

       for(int i=0;i<strlen(Infix_expression);i++)
      {
         Symbol[0]=Infix_expression[i];

         if(Symbol[0]==\'(\')
        push(Symbol[0]);

         else if(Symbol[0]==\')\')
        {
           Symbol[0]=pop( );

           while(Symbol[0]!=\'(\')
              {
             strcat(Postfix_expression,Symbol);

             Symbol[0]=pop( );
              }
        }

         else if(Symbol[0]==\'$\' || Symbol[0]==\'*\' || Symbol[0]==\'/\'
                    || Symbol[0]==\'+\' || Symbol[0]==\'-\')
        {
           if(Symbol[0]==\'*\' || Symbol[0]==\'/\')
              {
             Temp[0]=pop( );

             while(Temp[0]==\'$\' || Temp[0]==\'*\' || Temp[0]==\'/\')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           else if(Symbol[0]==\'+\' || Symbol[0]==\'-\')
              {
             Temp[0]=pop( );

             while(Temp[0]!=\'(\')
                {
                   strcat(Postfix_expression,Temp);

                   Temp[0]=pop( );
                }

             push(Temp[0]);
              }

           push(Symbol[0]);
        }

         else
        strcat(Postfix_expression,Symbol);
      }

       cout<<Postfix_expression;
    }

 
 
Didn't find what you were looking for? Find more on Program that changes an infix expression to a postfix expression according