C++ or C program to convert infix expression into postfix expression using stack

File name:-infpostf.cpp
#include<iostream.h>
#include<conio.h>
#define SIZE 100
int top = -1;
char stack[SIZE];
void push(char item);
char pop();
int isopr(char ch);
int precedence(char ch);
void main()
{
 int i;
 int j;
 char infix_exp[SIZE], postfix_exp[SIZE];
 char item;
 char x;
 clrscr();
 cout<<endl<<"Enter Infix expression in parentheses: ";
 gets(infix_exp);
 i=0;
 j=0;
 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(isopr(item) == 1)
  {
   x=pop();
   while(isopr(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
  {
   cout<<endl<<"Invalid Arithmetic Expression."<<endl;
   getch();
   exit(0);
  }
  item = infix_exp[i++];
 }
  postfix_exp[j++] = '\0';
  cout<<endl<<"Arithmetic expression in Postfix notation: ");
  puts(postfix_exp);
 getch();
}
void push(char item)
{
 if(top >= SIZE-1)
 {
  cout<<endl<<"Stack Overflow. Push not possible."<<endl;
 }
 else
 {
  top = top+1;
  stack[top] = item;
 }
}
char pop()
{
 char item = NULL;
 if(top <= -1)
 {
  cout<<endl<<"Stack Underflow. Pop not possible."<<endl;
 }
 else
 {
  item = stack[top];
  stack[top] = NULL;
  top = top-1;
 }
 return(item);
}
int isopr(char ch)
{
 if(ch == '^' || ch == '*' || ch == '/' ||
 ch == '+' || ch == '-')
 {
  return 1;
 }
 else
 {
  return 0;
 }
}
int precedence(char ch)
{
 if(ch == '^')
 {
  return(3);
 }
 else if(ch == '*' || ch == '/')
 {
  return(2);
 }
 else if(ch == '+' || ch == '-')
 {
  return(1);
 }
 else
 {
  return(0);
 }
}

File name:-infpostf.c
#include<stdio.h>
#include<conio.h>
#define SIZE 100
int top = -1;
char stack[SIZE];
void push(char item);
char pop();
int isopr(char ch);
int precedence(char ch);
void main()
{
 int i;
 int j;
 char infix_exp[SIZE], postfix_exp[SIZE];
 char item;
 char x;
 clrscr();
 printf("\nEnter Infix expression in parentheses: \n");
 gets(infix_exp);
 i=0;
 j=0;
 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(isopr(item) == 1)
  {
   x=pop();
   while(isopr(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 Arithmetic Expression.\n");
   getch();
   exit(0);
  }
  item = infix_exp[i++];
 }
  postfix_exp[j++] = '\0';
  printf("\nArithmetic expression in Postfix notation: ");
  puts(postfix_exp);
 getch();
}
void push(char item)
{
 if(top >= SIZE-1)
 {
  printf("\nStack Overflow. Push not possible.\n");
 }
 else
 {
  top = top+1;
  stack[top] = item;
 }
}
char pop()
{
 char item = NULL;
 if(top <= -1)
 {
  printf("\nStack Underflow. Pop not possible.\n");
 }
 else
 {
  item = stack[top];
  stack[top] = NULL;
  top = top-1;
 }
 return(item);
}
int isopr(char ch)
{
 if(ch == '^' || ch == '*' || ch == '/' ||
 ch == '+' || ch == '-')
 {
  return 1;
 }
 else
 {
  return 0;
 }
}
int precedence(char ch)
{
 if(ch == '^')
 {
  return(3);
 }
 else if(ch == '*' || ch == '/')
 {
  return(2);
 }
 else if(ch == '+' || ch == '-')
 {
  return(1);
 }
 else
 {
  return(0);
 }
}

No comments:

Post a Comment

Priority Queue

Priority queue:-  It is a special type of queue which stores group of elements. Each element has a priority number associated with it. Prior...