C++ Programming Tutorial

 
 
 
 

Image

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

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

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

    Related Post:
  1. Program to capitalize the first charcter of every word in a string

  2. Program to perform array operations like append, insert, delete, edit, display and search and element

  3. Program to rotate the word Computer by 1 results in rCompute

  4. Program to illustrate the use of operator * and &

  5. Program to construct Newtons Divided Difference Interpolation Formula from the given distinct data points and estimate the value of the function

  6. Program to draw a line using Bresenhams Line Algorithm (BLA) for lines with slopes negative and greater than 1

  7. Program to draw a circle using Polynomial Method

  8. Program to illustrate the binary operator(+) overloading without creating a new object

  9. Program that provides an example of scope resolution operator

  10. Program to estimate the Integral value of the function at the given points from the given data using Romberg Method

  11. Program to illustrate the use of pointer to pointers

  12. Program to illustrate classes with inline functions

  13. Program to draw a 3D Piece-Wise Bezier Curve of nth degree with Zeroth Order Continuity

  14. Program to illustrate the difference between the use of strcomp, strcmpi and stricmp

  15. Program to illustrate the relationship b/w union and structure

  16. Program to check whether a word is palindrome or not (using classes & pointers )

  17. Program of reflection in x axis

  18. Represent a polynomial in terms of a singly linked list and then add two three variables polynomials

  19. Program to show the projection of 3D objects using Orthographics Projection Parallel onto xy-plane

  20. Program to illustrate the implementation of Stack as an Arithmetic Expression Evaluater

 
 
Didn't find what you were looking for? Find more on Program to convert an Infix Expression into a Postfix Expression