Wednesday, August 24, 2011

Java Program to Perform Infix to PostFix Conversion


The Program given below Converts an Infix Expression to Post Fix Expression.For eg: ((l+i)*n-(o-p)^(q+p)) would be Converted to li+n*op-qp+^- .The Complete Source Code is Provided Below
 import java.lang.*;   
 import java.io.*;
  class array   
  {   
  DataInputStream get=new DataInputStream(System.in);   
  int n,i,top;   
  char s[],a[];   
  String str;   
  void getdata()   
  {   
  try   
   {   
   System.out.println("Enter the expression:");   
   str=get.readLine();   
   n=str.length();   
   s=new char[40];   
   a=new char[40];   
  }   
  catch(Exception e)   
  {   
   System.out.println(e.getMessage());   
  }   
  top=0;   
  }   
  void push(char c)    
  {   
   s[top]=c;   
   top++;   
  }   
  char pop()   
  {   
   char h;   
  if(top!=0)   
   {   
   top--;   
   h=s[top];   
   return h;   
   }   
  else   
   return 0;   
  }   
  void operation()   
  {   
  int j=0;   
  char d=0;   
  char t;   
  for(i=0;i<n;i++)   
   {   
   t=str.charAt(i);   
   switch(t)   
    {   
    case'^':   
      {   
      push(t);   
      break;   
      }   
    case '(':   
      {   
      push(t);   
      break;   
      }   
    case '{':   
      {   
      push(t);   
      break;   
      }   
    case '[':   
      {   
      push(t);   
      break;   
      }   
    case ')':   
      {   
      while((d=pop())!='(')   
      {   
       a[j++]=d;   
      }   
      break;   
      }   
    case '}':   
      {   
      while((d=pop())!='{')   
      {   
       a[j++]=d;   
      }   
      break;   
      }   
    case ']':   
      {   
      while((d=pop())!='[')   
      {   
       a[j++]=d;   
      }   
      break;   
      }   
    case '+':   
      {   
      if(s[top]=='/'||s[top]=='*'||s[top]=='^')   
      {   
      a[j++]=pop();   
      }   
      push(t);   
      break;   
      }   
    case '-':   
      {   
      if(s[top]=='+'||s[top]=='/'||s[top]=='*'||s[top]=='^')   
      {   
       a[j++]=pop();   
      }   
      push(t);   
      break;   
      }   
    case '*':   
      {   
      if(s[top]=='^')   
      {   
       a[j++]=pop();   
      }   
      push(t);   
      break;   
      }   
    case '/':   
      {   
      if(s[top]=='^'||s[top]=='*')   
      {       
       a[j++]=pop();   
      }   
      push(t);   
      break;   
      }   
    default:   
      a[j++]=t;   
    }   
   }   
   while(top!=0)   
   {   
   if(s[top]!='(')   
   {   
    a[j++]=pop();   
    }   
   }   
    System.out.println("The postfix expression is:");   
    for(i=0;i<j;i++)   
    System.out.print(a[i]);   
  }   
  }   
  class postcon   
  {   
  public static void main(String arg[])   
  {   
  array obj=new array();   
  obj.getdata();   
  obj.operation();   
  }   
  } 

7 comments:

  • Anonymous says:
    December 1, 2011 at 5:47 PM

    THank you! The code is not fine .. :)

  • Techman says:
    December 2, 2011 at 2:56 AM

    Thanks for Commenting.Would you please explain where the error occurs.

  • Anonymous says:
    March 11, 2012 at 9:24 AM

    Tooo complex to understand..

  • InfixToPostfixHelp says:
    May 2, 2012 at 7:16 PM

    Hello, The code is fantastic.
    Thank you.

    I ran into a few errors, im hoping you can help me out with them.

    There are two lines of code in your source that i do not understand.

    Both the for loops have 3 semi-colons.
    ex: for(i=0;i<n;i++)

    This also returns a few errors.

    Could you please fix this line, or tell me how i can get my code to work.

    Thank you,

  • Techman says:
    May 3, 2012 at 10:43 AM

    Thanks for reporting the error.The error has been corrected and the updated program has been posted.Sorry for the inconvenience,this error occurred during code formatting.

  • edison bernardez says:
    February 10, 2013 at 3:56 PM

    THanks IDOl

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.