C++ Programming Blog

 
 
 

Image

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

 struct node
 {
    char data;

    node *next;
 };

 node *top=NULL;
 node *bottom=NULL;
 node *entry;
 node *last_entry;
 node *second_last_entry;

 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>>Infix_expression;

       infix_to_postfix(Infix_expression);

       getch( );
       return 0;
    }


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


 void push(const char Symbol)
    {
       entry=new node;

       if(bottom==NULL)
      {
         entry->data=Symbol;
         entry->next=NULL;
         bottom=entry;
         top=entry;
      }

       else
      {
         entry->data=Symbol;
         entry->next=NULL;
         top->next=entry;
         top=entry;
      }
    }


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


 const char pop( )
    {
       char Symbol=NULL;

       if(bottom==NULL)
      cout<<\"\\n\\n\\n\\t ***  Error : Stack is empty. \\n\"<<endl;

       else
      {
         for(last_entry=bottom;last_entry->next!=NULL;
                         last_entry=last_entry->next)
        second_last_entry=last_entry;

         if(top==bottom)
        bottom=NULL;

         Symbol=top->data;

         delete top;

         top=second_last_entry;
         top->next=NULL;
      }

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

    Related Post:
  1. Program to illustrate the implementation of linked list as a Stack

  2. Program to draw a C-Curve of nth order

  3. Program to solve the mystery of Towers of Hanoi using recursion

  4. Program to illustrate the implementation of arrays as a Linear Queue

  5. Program to create heap

  6. Program of nesting member functions, private member functions and array of objects

  7. Program to illustrate the implementation of Scaling Transformation along Arbitrary Direction

  8. Program that provides an example of passing objects to function using call by reference method

  9. Program to capitalize the first charcter of every word in a string

  10. Program of LL Parser

  11. Program to convert an Infix Expression into a Postfix Expression using Linked List as a Stack

  12. Program to construct Newtons Forward Difference Interpolation Formula from the given distinct equally spaced data points

  13. Program to fill a Circle using Scan-Line Circle Fill Algorithm using Polar Coordinates

  14. Program that computes and displays the factorial of the given number using recursion

  15. Program to find largest integer among the three and display it using function with return type

  16. Program to illustrate the implementation of Reflection Transformation about x-axix, y-axis and w.r.t origin

  17. Program that defines template of vector class that provides modify and multiplication facility

  18. Program to show the implementation of Cohen-Sutherland MidPoint Subdivision Line Clipping Algorithm

  19. Program to estimate the value of Fourth Derivative of the function at the given points from the given data using Central Difference Formula of order 4

  20. Program to draw a Sphere using Ellipses