Tuesday 21 August 2012

To implement application of stack Infix – Postfix.



/*Program for infix to postfix*/


import java.util.*;
import java.io.*;

class Stack
{
   int top=-1, max;
   char s[];
   
   Stack( int k)
   {
      max=k;
      s= new char[max];
   }
    
   void push (char item)
   {
      if (top==max)
      System.out.println("Stack overflow");
      else
      s[++top]=item;
   }

   int isempty()
   {
      if (top<0)
      return 1;
      else
      return 0;
   }
  
   char pop()
   {
      if (isempty()==1)
      {  
         System.out.println("Stack underflow");
         return 0;
      }
      else
         return (s[top--]);
   }
}/*end of Stack*/

class Functions
{
   void postfix( char infix[], char post[], int k)
   {
      int position, und=1;
      int outpos=0;
      char topsymb='+';
      char symb;
      Stack opstk= new Stack(k);
      opstk.top=-1;
      
      for (position=0;(symb=infix[position])!='\0';position++)
      {
           if (isoperand(symb))
              post[outpos++]=symb;
           else 
             { 
                if (opstk.isempty()==1)
                und=1;
                else
                { und=0;
                  topsymb=opstk.pop();
                }
                while (und==0 && prcd(topsymb,symb)==1)
                {
                   post[outpos++]=topsymb;
                   if (opstk.isempty()==1)
                      und=1;
                   else
                   {
                     und=0;
                     topsymb=opstk.pop();
                   }
                }//end while
                if(und==0)
                  opstk.push(topsymb);
                if(und==1||(symb!=')'))
                  opstk.push(symb);
                else
                  topsymb=opstk.pop();
             }//end else
       }//end for
       while(opstk.isempty()==0)
       post[outpos++]=opstk.pop();
       post[outpos]='\0';
   }//end postfix function


   int prcd(char topsymb,char symb)
    {
     /*check precedence and return 0 or 1*/
       if(topsymb=='(')
       return 0;
       if(symb=='(')
       return 0;
       if(symb==')')
       return 1;
       if (topsymb=='$'&&symb=='$')
       return 0;
       if (topsymb=='$'&&symb!='$')
       return 1;
       if (topsymb!='$'&&symb=='$')
       return 0;
       if((topsymb=='*'||topsymb=='/')&&(symb!='$'))
       return 1;
       if((topsymb== '+'||topsymb=='-')&&(symb=='-'||symb=='+'))
       return 1;
       if((topsymb== '+'||topsymb=='-')&&(symb=='*'||symb=='/'))
       return 0;
       return 1;
     }/*end prcd function*/
   
    boolean isoperand(char symb)
     {
        /*Return 1 if symbol is digit and 0 otherwise*/
        if((symb>='0'&& symb<='9') || (symb>='a' && symb<='z'))
        return true;
        else
        return false;
     }/* end isoperand function*/
}

class InPo
{
   public static void main (String args [])
{
 /*Read infix expression and call postfix function*/
 int x;
 Scanner s = new Scanner(System.in);
  Functions f=new Functions();
  System.out.println("Size of array =");
  x= s.nextInt();
  char infix[]=new char[x];
  char post[]=new char[x];
  int pos=0;char c;
  int k;


  DataInputStream in=new DataInputStream(System.in);
  System.out.println("Enter the infix string");
   try
  {
   do
   {
    c=(char)in.read();
    infix[pos++]=c;
   }while(c!='\n');
  }
  catch(Exception e)
  {
   System.out.println("I/O Error");
  }
  
  infix[--pos]='\0';
  k=pos;
  Stack s1= new Stack(k);
  System.out.println("The original infix expression is : ");
  for (int i=0;i<pos;i++)
  System.out.print(infix[i]);
  f.postfix(infix,post,k);
  System.out.println("\nThe postfix expression is : ");
  for (int i=0;post[i]!='\0';i++)
  System.out.print(post[i]);
}//end main
}


===========OUTPUT===============================

C:\Program Files\Java\jdk1.6.0_17\bin>javac InPo.java

C:\Program Files\Java\jdk1.6.0_17\bin>java InPo
Size of array =
99
Enter the infix string
a+(b*c)-c
The original infix expression is :
a+(b*c)-c
The postfix expression is :
abc*+c-
C:\Program Files\Java\jdk1.6.0_17\bin>

No comments:

Post a Comment