Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to convert an Infix Expression into a Postfix Expression

Posted By: Easy Tutor     Category: C++ Programming     Views: 25474

A C++ Program to convert an Infix Expression into a Postfix Expression.

Code for Program to convert an Infix Expression into a Postfix Expression in C++ Programming

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

 int top=-1;

 char Stack[100]={NULL};

 void push(constchar);
 constchar pop( );

 void infix_to_postfix(constchar *);


 int main( )
    {
       clrscr( );

       char Infix_expression[100]={NULL};

       cout<<"\n\n Enter the Infix Expression : ";
       cin.getline(Infix_expression,80);

       infix_to_postfix(Infix_expression);

       getch( );
       return 0;
    }


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

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

 /*************************************************************************///--------------------------------  pop( )  -----------------------------///*************************************************************************/constchar 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(constchar *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 count=0;count<strlen(Infix_expression);count++)
      {
         Symbol[0]=Infix_expression[count];

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

         elseif(Symbol[0]==')')
        {
           Symbol[0]=pop( );

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

             Symbol[0]=pop( );
              }
        }

         elseif(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]);
              }

           elseif(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<<"\n\n Postfix Expression : "<<Postfix_expression<<endl;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to convert an Infix Expression into a Postfix Expression is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
VIP GRIL from Oman Comment on: Mar 25
I want sample program to wrute one polynomail ...

VIP GRIL from Oman Comment on: Mar 25
hello ,,
can you explain to me how this code ?
beacause i don't understand it
and the teacher ask me to explan this code >> as soon as possiblae

Program to convert an Infix Expression into a Postfix Expression

View All Comments