Thursday, November 11, 2010

Infix to Postfix Conversion Using Java


Hey, guys! Yeah, I just wanna share my knowledge even NOW I'm still bustling with my Mid Semester Test! Ummh, where should I start? Okay, firstly you have to understand what infix and postfix are. Well, just read here and here. :D

Now, we will make a program in Java that can convert infix notation to postfix notation instantly. Ok, there are so many kind of source code that you could find on internet. But the source code below, I made it by my self. I'm sorry if this source code isn't really good. I'm still in learning process. Here we go:




/**
 *
 * @author ilham hasymi effendi
 * @homepage http://hamzcraze.blogspot.com
 *
 */


import java.util.Scanner;

public class InfixToPostfix {
    public static void main(String[] args) {
        String infix = "";
        String postfix = "";
        Scanner scan = new Scanner(System.in);
        LinkedStack myStack = new LinkedStack();
        myStack.push('(');
        System.out.println("insert infix notation:");
        infix = scan.next();
        infix+=')';
        for (int i = 0; i < infix.length(); i++) {
            char cek = infix.charAt(i);
            if(cek == '('){
                myStack.push(cek);
            }else if(cek == ')'){
                while(myStack.peek()!=(Object)'('){
                    postfix+=myStack.pop();
                }
                myStack.pop();
            }else if(cek == '+'||cek == '-'){
                if(myStack.peek()==(Object)'*' 
                        || myStack.peek()==(Object)'/' 
                        || myStack.peek()==(Object)'^' 
                        || myStack.peek()==(Object)'+' 
                        || myStack.peek()==(Object)'-'){
                    postfix+=myStack.pop();
                    myStack.push(cek);
                }else{
                    myStack.push(cek);
                }
            }else if(cek == '*'||cek == '/'){
                if(myStack.peek()==(Object)'^' 
                        || myStack.peek()==(Object)'*' 
                        || myStack.peek()==(Object)'/'){
                    postfix+=myStack.pop();
                    myStack.push(cek);
                }else{
                    myStack.push(cek);
                }
            }else if(cek == '^'){
                myStack.push(cek);
            }else{
                postfix+=cek;
            }
        }
        System.out.println("");
        System.out.println("postfix notation:");
        System.out.println(postfix);
        
    }
}

WARNING: you need some additional source code to run the source code above. Here these additional source codes:


/**
 *
 * @author ilham hasymi effendi
 * @homepage http://hamzcraze.blogspot.com
 *
 */

import java.util.EmptyStackException;

public class LinkedStack implements Stack {

    protected ChainNode topNode;
    protected static int num = -1;

    public LinkedStack(int initialCapacity) {
        // the default initial value of topNode is null

    }

    public LinkedStack() {
        this(0);
    }

    public boolean empty() {
        return topNode == null;
    }

    public Object peek() {
        if (empty()) {
            throw new EmptyStackException();
        }
        return topNode.element;
    }

    public Object peekSpec(int i){
        ChainNode tmp = topNode;
        if(i>num){
            return null;
        }
        for (int j = 0; j < i; j++) {
            tmp = tmp.next;
        }
        
        return tmp.element;
    }

    public void push(Object theElement) {
        topNode = new ChainNode(theElement, topNode);
        num++;
    }

    public Object pop() {
        if (empty()) {
            throw new EmptyStackException();
        }
        Object topElement = topNode.element;
        topNode = topNode.next;
        num--;
        return topElement;
    }

    public int getSize(){
        return num+1;
    }
}


/**
 *
 * @author ilham hasymi effendi
 * @homepage http://hamzcraze.blogspot.com
 *
 */


public class ChainNode
{
   // package visible data members
   Object element;
   ChainNode next;

   // package visible constructors
   ChainNode() {}

   ChainNode(Object element)
      {this.element = element;}

   ChainNode(Object element, ChainNode next)
      {this.element = element;
       this.next = next;}
}

That's all buddy.  Ask me if my source code is unclear to read or hard to understand. Thanks for reading this post. Just lemme know if you find some bugs. :)

2 comment(s):

abc said...

programnya g jalan masbro,tp hbs di kelas yg LinkedStack, cb diubah yang bagian :

public class LinkedStack implements Stack

diubah jadi :

public class LinkedStack

hbs itu bs run :D

zwha said...

trimakasih banyaaaakk :D

Post a Comment

feel free to write your comment here.. :)