Results 1 to 2 of 2
- 06-27-2012, 07:29 PM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
need help converting from infix to postfix notation
guys, I have an expression like: 1 + 2 * 3, and I need to convert to postfix notation: 1 2 3 * +
This is my code, but for some reason I'm not displaying the operators, any help appreciated
Java Code:package postfixstack; import java.io.*; import java.util.*; public class Main { static Stack<String> optor = new Stack(); static Stack<Integer> oprand = new Stack(); static String strOut = ""; public static void main(String[] args) throws FileNotFoundException { File inFile = new File("datafile.txt"); Scanner in = new Scanner(inFile); while(in.hasNext()) { String next = in.next(); do { if(optor.isOperator(next)) { //process operator processOptor(next); } else { oprand.push(Integer.parseInt(next)); strOut += next+" "; } next = in.next(); }while(!next.equals("$")); } System.out.println(strOut); } //processing operators public static void processOptor(String op) { do { if(optor.isEmpty()) { optor.push(op); return; } if(optor.priority(op) > optor.priority(optor.top())) { optor.push(op); return; } if(optor.priority(op) == optor.priority(optor.top())) { if(optor.associativity(optor.priority(op)).equals("right")) { optor.push(op); return; } } System.out.println("here!"); strOut += optor.top()+" "; optor.pop(); }while(true); } }
- 06-27-2012, 09:10 PM #2
Re: need help converting from infix to postfix notation
Also posted at: need help converting from infix to postfix notation
If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Trouble with converting infix to postfix
By mustachio in forum New To JavaReplies: 7Last Post: 07-21-2011, 03:48 AM -
Infix to Postfix using array
By Franneldort in forum New To JavaReplies: 0Last Post: 10-11-2010, 05:57 PM -
Postfix-Notation
By little_polarbear in forum New To JavaReplies: 9Last Post: 09-09-2008, 04:24 PM -
any can help me? About MDAS and PEMDAS rules and Infix-Prefix, Infix-Postfix rules
By darlineth in forum New To JavaReplies: 1Last Post: 07-05-2008, 03:08 PM -
How to convert infix arithmetic expressions to postfix
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks