Q:

C++ Program For Infix to Postfix Expression Converter

0

Logic:- 

This program makes use of the stack. A stack is used to store elements in LIFO order. The element which comes at the end is deleted first. You can imagine that MS Word uses a stack to store operation in a specific order. When we perform ‘undo’ then it deletes the last operation performed.

This program takes a string of an infix expression and gives a string of postfix operation. It simply considers the every character of the infix string and if the character being considered is a number then it is appended to the postfix string. If it is a symbol then it is pushed onto the stack. Rest of the process is quite simple. The code is also self-explanatory.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<iostream>
#include<cstring>
using namespace std;

class stack
{
  private:
  
   int arr[5];
   int top;
   int size;
public:
  
   stack()
   {
     top=-1;
     size=5;
   }
   bool IsEmpty()
   {
     if(top==-1)
     {
      return true;
     }
     else
     {
      return false;
     }
   }

bool IsFull()
{
    if(top==size-1)
    {
     return true;
    }
    else
    {
     return false;
    }
}

void push(int data)
{
    if(IsFull())
    {
     cout<<"STACK IS FULL..."<<endl;
    }
    else
    {
     top++;
     arr[top]=data;
    }
}

int pop()
{
    if(IsEmpty())
    {
     cout<<"STACK IS EMPTY..."<<endl;
    }
    else
    {
     int c;
     c=arr[top];
     top--;
     return c;
    }
}

void show()
{
    if(IsEmpty())
    {
     cout<<"STACK IS EMPTY..."<<endl;
    }
    else
    {
     for (int i=0;i<=top;i++)
     {
       cout<<"DATA ---- "<<arr[i]<<endl;
     }
    }
}

int getweight(char ch)
{
    switch(ch)
    {
     case '/':
     case '*': return 2;
     case '+':
     case '-': return 1;
     default: return 0;
    }
}

void in2p(char in[],char po[],int size)
{
    int i=0,k=0;
    int w;
    char ch;
    
 while(i<size)
    {
     ch=in[i];
     w=getweight(ch);
     
  if(w==0)
     {
       po[k++]=ch;
     }
     else
     {
       if(IsEmpty())
       {
         push(ch);
       }
    
       else
       {
         while(!IsEmpty() && w<=getweight(arr[top]))
         {
           po[k++]=arr[top];
           pop();
         }
         push(ch);
       }
     }
     i++;
 }
    while(!IsEmpty())
    {
     po[k]=arr[top];
     k++;
     pop();
    }
    po[k]=0;
}
};

int main()
{
  char given[]="1+2*3";
  int s=strlen(given);
  char desired[s];
  stack obj;
  obj.in2p(given,desired,s);
  cout<<"\n\n\n\t\t----------------------------------------------";
  cout<<"\n\t\t|                                            |";
  cout<<"\n\t\t|   INFIX TO POSTFIX EXPRESSION CONVERTER    |";
  cout<<"\n\t\t|                                            |";
  cout<<"\n\t\t----------------------------------------------\n";
  cout<<"\t\tINFIX EXPRESSION = "<<given; 
  cout<<"\n\n\t\tPOSTFIX EXPRESSION = "<<desired<<endl<<endl;
  system("pause");
};

 

 

Output:

----------------------------------------------

|                                                                             |

|   INFIX TO POSTFIX EXPRESSION CONVERTER    |

|                                                                              |

----------------------------------------------

INFIX EXPRESSION = 1+2*3

POSTFIX EXPRESSION = 123*+

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now