View Single Post
  #1 (permalink)  
Old 08-16-2007, 12:10 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Icon11 Tutorial: How to Convert Infix to Postfix

Hello everybody,
It's been for a long time that I haven't wrote a tutorial for this website that's because of my business of real life coding XD lol... Anyways, I hope the code below is self explanatory and may be helpful for you to learn and analyze. Feel free to add some comments, additional ideas or suggestion to improve the code!!!

You are using the code at your own risk! Don't hate me if you got an F or get fired from your work. It's tested and worked at JDK 1.6

Have fun coding!

This is a snippet code that will convert an INFIX expression that's passed through the command line into POSTFIX.

PHP Code:
                ArrayList<Objectoperators = new ArrayList<Object>();
                
Stack<Objectchecker = new Stack<Object>();
                for (
int i 1args.lengthi++) {
                    if (
args[i].equals("+")) {
                        if (!
checker.isEmpty()) {
                            if (
checker.peek().equals("x") || checker.peek().equals("/")) {
                                
operators.add(checker.pop());
                                
checker.push(args[i]);
                            } else {
                                
checker.push(args[i]);
                            }
                        } else {
                            
checker.push(args[i]);
                        }
                    } else if (
args[i].equals("-")) {
                        if (!
checker.isEmpty()) {
                            if (
checker.peek().equals("x") || checker.peek().equals("/")) {
                                
operators.add(checker.pop());
                                
checker.push(args[i]);
                            } else {
                                
checker.push(args[i]);
                            }
                        } else {
                            
checker.push(args[i]);
                        }
                    } else if (
args[i].equals("x")) {
                        if (!
checker.isEmpty()) {
                            if (
checker.peek().equals("+") || checker.peek().equals("-")) {
                                
checker.push(args[i]);
                            } else {
                                
operators.add(checker.pop());
                                
checker.push(args[i]);
                            }
                        } else {
                            
checker.push(args[i]);
                        }
                    } else if (
args[i].equals("/")) {
                        if (!
checker.isEmpty()) {
                            if (
checker.peek().equals("+") || checker.peek().equals("-")) {
                                
checker.push(args[i]);
                            } else {
                                
operators.add(checker.pop());
                                
checker.push(args[i]);
                            }
                        } else {
                            
checker.push(args[i]);
                        }
                    } else {
                        
operators.add(args[i]);
                    }
                }
                if (!
checker.isEmpty()) {
                    for (
int i 0<= checker.size(); i++) {
                        
operators.add(checker.pop());
                    }
                } 
I wanted to say thank you to my graduate friend Steven that helped me found this idea

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 08-16-2007 at 12:15 AM.
Reply With Quote