Facebook

adsense

Friday 3 April 2015

Convert a Infix to Postfix Notation in java

SAVE THE PROGRAM WITH NAME ""InToPostfixGui.java" and run

import java.util.Scanner;
import javax.swing.JOptionPane;

public class InToPostfixGui{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String output;
        String ea = JOptionPane.showInputDialog("Please Enter The PostFix Notation ");
        String eq = ea + ")";
     
        StackChar sk = new StackChar(eq);
     
        char postfix[] = new char[eq.length()];
     
        int j=0;
     
        sk.pushChar('(');

output = "  INFIX NOTATION  " +  ea ;
output+="\n----------------------------------------------------------------------------------\n";
     
        output += "  CHARACTER          :          STACK         :          POSTFIX\n";
        output+="\n----------------------------------------------------------------------------------\n";
        for(int i=0;i<eq.length();i++){
         
            char ch = eq.charAt(i);      
       
            if(Character.isDigit(ch))
                postfix[j++]=ch;
         
            else if(ch=='(')
                sk.pushChar(ch);
       
            else if(ch=='*'  || ch=='^' || ch=='+' || ch == '-' || ch =='/'){
                if(  (ch=='+' || ch=='-')  && (sk.peep()!='(' ) ){
                    do{
                        postfix[j++] = sk.popChar();
                    }while(sk.peep()!='(');
                 
                    sk.pushChar(ch);
                }
                 else if(  (ch=='*' || ch=='/')  && (sk.peep()!='(') && (sk.peep()!='+' && sk.peep()!='-') ){
                    do{
                        postfix[j++] = sk.popChar();
                    }while(sk.peep()!='(' && (sk.peep()!='+' && sk.peep()!='-'));
                    sk.pushChar(ch);
                }
             
                else
                     sk.pushChar(ch);
            }
             
            else if(ch==')'){
             
                    while(sk.peep()!='(')  
                        postfix[j++] = sk.popChar();
                     
                sk.popChar();
                }
            output+= "           "+ch+ "                                    ";
            output+=sk.display();
            output+="                              ";
            output+=dis(postfix,j);
            output+="\n\n";
                 
            }
output+="\n----------------------------------------------------------------------------------\n";
        output+="POSTFIX NOTATION ==  ";
        for(int i=0;i<postfix.length;i++)
            output+=postfix[i];
     

       JOptionPane.showMessageDialog(null, output);
     
    }
    public static String dis(char[] ch,int k){
        String out=" ";
for(int i=0 ; i<k;i++)
            out+=ch[i];
return out;
    }
}


class StackChar {
 
    String st ;
    char[]  chr ;
    int top ;
 
    public StackChar(String stg){
        st = stg ;
        chr = new char[stg.length()];
        top=-1;
    }
   
    public void pushChar(char num){
        if(top==st.length()-1){
            System.out.println("NO FREE SPACE ");
            return;
        }
        else
            chr[++top]=num ;
    }
 
    public char popChar(){
        char item ;
        if(top==-1){
            System.out.println("ALREADY Empty");
            return 0 ;
        }
        else
            item = chr[top--];
            return item;
    }
 
    public char peep(){
        if(top==-1){
            System.out.println("IS EMPTY");
            return 0 ;
        }
        return chr[top];
         
    }
 
    public String display(){
String st=" ";
        for(int i=0;i<=top ;i++)
            st+=""+chr[i]+" ";
 
        return st;
    }
 
}
Output




No comments:

Post a Comment