Logo 
Search:

C++ Programming Articles

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

Program to convert a Postfix Expression into an Infix Expression

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

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

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

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

 int top=-1;

 char Stack[25][100]={NULL};

 void push(constchar *);
 constchar* pop( );

 void postfix_to_infix(constchar *);

 int main( )
    {
       clrscr( );

       char Postfix_expression[100]={NULL};

       cout<<"\n\n\t Enter the Postfix Expression : ";
       cin.getline(Postfix_expression,80);

       postfix_to_infix(Postfix_expression);

       getch( );
       return 0;
    }


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

       else
      {
         top++;
         strcpy(Stack[top],Symbol);
      }
    }

 /*************************************************************************///--------------------------------  pop( )  -----------------------------///*************************************************************************/constchar* pop( )
    {
       char Symbol[100]={NULL};

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

       else
      {
         strcpy(Symbol,Stack[top]);
         strset(Stack[top],NULL);
         top--;
      }

       return Symbol;
    }

 /*************************************************************************///---------------------  postfix_to_infix(const char *)  ----------------///*************************************************************************/void postfix_to_infix(constchar *Postfix)
    {
       char Infix_expression[100]={NULL};
       char Postfix_expression[100]={NULL};

       strcpy(Postfix_expression,Postfix);
       strcat(Postfix_expression,"=");

       int count=0;

       char Symbol_scanned[5]={NULL};

       do
      {
         Symbol_scanned[0]=Postfix_expression[count];

         if(Symbol_scanned[0]=='/' || Symbol_scanned[0]=='*' ||
        Symbol_scanned[0]=='-' || Symbol_scanned[0]=='+' ||
                       Symbol_scanned[0]=='^' )
        {
           char Value_1[100]={NULL};
           char Value_2[100]={NULL};
           char Result[100]={NULL};

           strcpy(Value_1,pop( ));
           strcpy(Value_2,pop( ));

           if(Infix_expression[(count+1)]!='=')
              strcpy(Result,"(");

           strcat(Result,Value_2);
           strcat(Result,Symbol_scanned);
           strcat(Result,Value_1);

           if(Infix_expression[(count+1)]!='=')
              strcat(Result,")");

           push(Result);
        }

         else
        push(Symbol_scanned);

         count++;
      }
       while(Postfix_expression[count]!='=');

       strset(Infix_expression,NULL);
       strcpy(Infix_expression,pop( ));

       cout<<"\n\n\t Infix Expression is : "<<Infix_expression;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to convert a Postfix Expression into an Infix 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

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

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

 
No Comment Found, Be the First to post comment!