Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program that changes an infix expression to a postfix expression according

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

Write a program that changes an infix expression to a postfix expression according to the following specifications.

1. The infix expression to be converted is in the input file in the format of one character per line, with a maximum of 50 lines in the file. For example, (3+2)*5 would be in the form:
(
3
+
2
)
*
5
2. The input file contains only one infix expression. There are no blank lines.

3. The program will only be designed to handle the binary operators +, -, *, /.

4. The operands will be one digit numerals.

5. The operators * and / have the highest priority. The operators + and - have the lowest priority. Operators at the same precedence level associate from left to right. Parentheses act as grouping symbols that over-ride the operator priorities.

6. The output file will have the postfix expression all on one line.

7. The input will be an expression with valid syntax.

Code for Program that changes an infix expression to a postfix expression according in C++ Programming

 # include <iostream.h>
 # include <fstream.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( );

       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(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 i=0;i<strlen(Infix_expression);i++)
      {
         Symbol[0]=Infix_expression[i];

         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<<Postfix_expression;
    }

  
Share: 



Easy Tutor
Easy Tutor author of Program that changes an infix expression to a postfix expression according 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].

 
No Comment Found, Be the First to post comment!