Results 1 to 4 of 4
- 09-05-2012, 10:30 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Trouble with loops/classes/something
Hey all,
So I have coded this application, to test a (Method? Function?) before integrating it into a larger program. It's supposed to convert an Infix equation to a Postfix one.
So I'm having trouble with this loop, (see code below) where I want to check to see if the top of the Operator Stack (opStack) is equal to an open parentheses '('.
However, the loop seems to never notice that the top of the stack has indeed become a left parentheses, or my Stack class is broken, or my Linked list class, or something... -_-
The code should compile, I've linked a zip with the other source files required to compile it (Just compile IToFTest.java and it should auto compile all other dependencies).
Note: I have to use the provided DSAStack, DSAQueue, DSALinkedList classes, as I coded them myself, and we are required to use our own code. Sorry it's so full of my debug code.
If you guys could help. I would be *immensely* greatful.
Thanks :)
Zip: Get it here!
Java Code:import java.io.*; import io.*; import java.util.*; import java.lang.Character; public class IToFTest { public static void main(String [] args) { DSAQueue<Object> postfixEqn = new DSAQueue<Object>(); String equation; equation = ConsoleInput.readLine("Enter equation: "); postfixEqn = parseInfixToPostfix(equation); while(!postfixEqn.isEmpty()) { System.out.println(postfixEqn.dequeue()); } } private static DSAQueue<Object> parseInfixToPostfix(String equation) { String cursor; char charCursor = 'a'; DSAQueue<Object> postfixEqn = new DSAQueue<Object>(); DSAStack<Object> opStack = new DSAStack<Object>(); StringTokenizer strTok = new StringTokenizer(equation, "+-*/()", true); while(strTok.hasMoreTokens()) { cursor = strTok.nextToken(); System.out.println("Cursor: " + cursor); if(!Character.isDigit(cursor.charAt(0))) { charCursor = cursor.charAt(0); System.out.println("Char cursor: " + charCursor); } if(charCursor == ('+')||charCursor == ('-')||charCursor == ('*')||charCursor == ('/')) { System.out.println("Char Curson in loop: " + charCursor); if(!opStack.isEmpty()) { while(precedenceOf(charCursor) < precedenceOf((char)opStack.top())) { postfixEqn.enqueue(opStack.pop()); } opStack.push(charCursor); System.out.println("Pushing: " + charCursor); System.out.println("Top of op stack: " + opStack.top()); } else { opStack.push(charCursor); System.out.println("Pushing: " + charCursor); System.out.println("Top of op stack: " + opStack.top()); } charCursor = 'a'; } else if(charCursor == '(') { opStack.push(charCursor); System.out.println("Pushing: " + charCursor); System.out.println("Top of op stack: " + opStack.top()); charCursor = 'a'; } else if(charCursor == ')') { while(opStack.top() != '(') { postfixEqn.enqueue(opStack.pop()); } opStack.pop(); charCursor = 'a'; } else { postfixEqn.enqueue(cursor); } } while(!opStack.isEmpty()) { postfixEqn.enqueue(opStack.pop()); } return postfixEqn; } private static int precedenceOf(char operator) { int precedence = 0; if(operator == '+' || operator == '-') { precedence = 1; } else if(operator == '*' || operator == '/') { precedence = 2; } return precedence; } }
- 09-05-2012, 11:31 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Re: Trouble with loops/classes/something
Ideas anybody?
- 09-05-2012, 12:03 PM #3
Re: Trouble with loops/classes/something
Have patience.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-05-2012, 12:34 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Re: Trouble with loops/classes/something
Figured it out.
I noobishly forgot to update my tail value for my double ended Linked list, which the DSAStack class is based off. Thus, opStack.top() would always return the last added value to the stack, regardless of wether or not it had been popped off of the stack.
I am sure that given time this forum would have helped me overcome my problem.
Thanks,
:)
Similar Threads
-
Trouble with arrays and classes
By CuddlyKittens11 in forum Advanced JavaReplies: 3Last Post: 04-25-2011, 12:42 AM -
Having trouble with Classes
By Skyton in forum EclipseReplies: 7Last Post: 03-04-2011, 01:33 PM -
trouble with showing the average with functions and loops
By anonymous445 in forum New To JavaReplies: 12Last Post: 01-29-2011, 10:30 PM -
trouble with loops
By relics in forum New To JavaReplies: 1Last Post: 09-22-2010, 11:01 PM -
trouble creating program using loops for multiplication table
By cuse17 in forum New To JavaReplies: 2Last Post: 02-23-2009, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks