C program to implement Infix to Postfix Conversion

Infix to Postfix Conversion using Stack

Step-by-step procedure to convert the infix notation to postfix notation:


  • Scan the Infix string from left to right.

  • Initialize an empty stack.

  • If the scanned character is an operand, add it to the Postfix string.

  • If the scanned character is an operator and if the stack is empty push the character to stack.

  • If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack.

  • If top Stack has higher precedence over the scanned character pop the stack else push the scanned character to stack. Repeat this step until the stack is not empty and top Stack has precedence over the character.

  • Repeat 4 and 5 steps till all the characters are scanned.

  • After all characters are scanned, we have to add any character that the stack may have to the Postfix string.

  • If stack is not empty add top Stack to Postfix string and Pop the stack.

  • Repeat this step as long as stack is not empty.

 EXAMPLE:


A+(B*C-(D/E-F)*G)*H
StackInputOutput
EmptyA+(B*C-(D/E-F)*G)*H-
Empty+(B*C-(D/E-F)*G)*HA
+(B*C-(D/E-F)*G)*HA
+(B*C-(D/E-F)*G)*HA
+(*C-(D/E-F)*G)*HAB
+(*C-(D/E-F)*G)*HAB
+(*-(D/E-F)*G)*HABC
+(-(D/E-F)*G)*HABC*
+(-(D/E-F)*G)*HABC*
+(-(/E-F)*G)*HABC*D
+(-(/E-F)*G)*HABC*D
+(-(/-F)*G)*HABC*DE
+(-(-F)*G)*HABC*DE/
+(-(-F)*G)*HABC*DE/
+(-(-)*G)*HABC*DE/F
+(-*G)*HABC*DE/F-
+(-*G)*HABC*DE/F-
+(-*)*HABC*DE/F-G
+*HABC*DE/F-G*-
+*HABC*DE/F-G*-
+*EndABC*DE/F-G*-H
EmptyEndABC*DE/F-G*-H*+



#include <stdio.h> 
#include <conio.h>
#define SIZE 100
int top = -1;
char stack[SIZE];
void push(char item);
char pop();
int is_operator(char symbol);
int precedence(char symbol);

void main()
{
 int i=0, j=0;
 char infix_exp[SIZE], postfix_exp[SIZE];
 char item, x;
 clrscr();
 printf("\nEnter the Infix notation enclosed in parentheses: \n");
 gets(infix_exp);

 item=infix_exp[i++];
 while(item != '\0')
 {
  if(item == '(')
  {
    push(item);
  }
  else if((item >= 'A'  && item <= 'Z') || (item >= 'a' && item <= 'z'))
  {
   postfix_exp[j++] = item;
  }
  else if(is_operator(item) == 1)
  {
   x=pop();
   while(is_operator(x) == 1 && precedence(x) >= precedence(item))
   {
    postfix_exp[j++] = x;
    x = pop();
   }
   push(x);
   push(item);
  }
  else if(item == ')')
  {
   x = pop();
   while(x != '(')
   {
    postfix_exp[j++] = x;
    x = pop();
   }
  }
  else
  {
   printf("\nInvalid  Expression.\n");
   getch();
   exit(0);
  }
  item = infix_exp[i++];
 }
 postfix_exp[j++] = '\0';
 printf("\n Expression in Postfix notation: ");
 puts(postfix_exp);
 getch();
}

void push(char item)
{
 if(top >= SIZE-1)
 {
  printf("\nStack is Overflow.\n");
 }
 else
 {
  top = top+1;
  stack[top] = item;
 }
}

char pop()
{
 char item = NULL;
 if(top <= -1)
 {
  printf("\nStack Underflow.\n");
 }
 else
 {
  item = stack[top];
  stack[top] = NULL;
  top = top-1;
 }
 return(item);
}

int is_operator(char symbol)
{
 if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-')
 {
  return 1;
 }
 else
 {
  return 0;
 }
}

int precedence(char symbol)
{
 if(symbol == '^')
 {
  return(3);
 }
 else if(symbol == '*' || symbol == '/')
 {
  return(2);
 }
 else if(symbol == '+' || symbol == '-')
 {
   return(1);
 }
 else
 {
  return(0);
 }
}










Happy Coding :)

Comments

Popular posts from this blog

MultiSelection of Item in Recycler View

Upload Image From Android To Php Server

Merge Sort