Results 1 to 1 of 1
Thread: Infix to Post fix
- 05-03-2009, 09:21 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
Infix to Post fix
Alright, so again another assignment, I've gotten it down two three problems. now granted I haven't actually gotten to test it and see what the code itself is going to do. Because its a little buggy.
The assignment required us to build a class for storing the infix notation, converting to postfix and storing the post fix notation. Here's the test method.
And this is the post fix Method. Now. Again I had to go looking online to find a code to use. So some of it I'm not sure if its a personal class they have or its just not something I have saved to the disk like the prec on teh last while statement, which is where the problems are occuring.Java Code:import java.util.*; public class Problem11 { //Start class static Scanner console = new Scanner (System.in); //Scanner to get the infix notation from user. public static void main (String []args) { //start Main String infix; Scanner input = new Scanner(System.in); String postfix; //get mathematical expression from user System.out.println("Please input a mathematical expression"); infix=console.next(); postfix=convertPost(infix); System.out.println("Infix Expression: " + infix); System.out.println("Postfix Expression: " + postfix); } //end Main } //end class
Anything you see that You can suggest may make the code better I'm all ears. but the main problems is that the test method isn't recognizing the method call itself. And the prec methods which I have yet to puzzle out to what they're supposed to be. most of it I understand and have been able to Make work for what code methods have been provided for use in the class and what I've personally made. Like The stack opporators (S) which is a declared veriable for the class this is part of, If you need to see the rest of the class tell me and I'll post it.Java Code:public String convertPost(String infix) { StringTokenizer s = new StringTokenizer(infix); String symbol; postfix = ""; while(s.hasMoreTokens()); { symbol=s.nextToken(); if (Character.isDigit(symbol.charAt(0))) postfix = postfix + " " + (Integer.parseInt(symbol)); else if (symbol.equals("(")) // push "(" { Character operator = new Character('('); S.push(operator); } else if (symbol.equals(")")) // push everything back to "(" { while (S.peek()!= '(') { postfix = postfix + " " + S.peek(); } S.pop(); } // print operatorStack occurring before it that have greater precedence { while (!S.empty() && !(S.peek()).equals("(") && prec(symbol.charAt(0)) <= prec((S.peek()).charValue())) postfix = postfix + " " + S.peek(); Character operator = new Character(symbol.charAt(0)); S.push(operator); S.pop(); } } while (!S.empty()) postfix = postfix + " " + S.peek(); return postfix; }
Similar Threads
-
my first post
By kaviyasivashanmugham in forum New To JavaReplies: 2Last Post: 03-13-2009, 09:50 AM -
First post out of the way..
By sirwiggles in forum IntroductionsReplies: 0Last Post: 02-06-2009, 10:44 PM -
First post ever
By pbpersson in forum IntroductionsReplies: 4Last Post: 08-16-2008, 05:30 AM -
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

Bookmarks