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

No comments:

Post a Comment

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...