Results 1 to 4 of 4
  1. #1
    SykoPyro is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Unhappy 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;
    		
    	}
    }

  2. #2
    SykoPyro is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Re: Trouble with loops/classes/something

    Ideas anybody?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Trouble with loops/classes/something

    Have patience.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    SykoPyro is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default 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

  1. Trouble with arrays and classes
    By CuddlyKittens11 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2011, 01:42 AM
  2. Having trouble with Classes
    By Skyton in forum Eclipse
    Replies: 7
    Last Post: 03-04-2011, 02:33 PM
  3. trouble with showing the average with functions and loops
    By anonymous445 in forum New To Java
    Replies: 12
    Last Post: 01-29-2011, 11:30 PM
  4. trouble with loops
    By relics in forum New To Java
    Replies: 1
    Last Post: 09-23-2010, 12:01 AM
  5. Replies: 2
    Last Post: 02-23-2009, 03:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •