Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: A Stack For Notation

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    Washington DC
    Posts
    81
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default A Stack For Notation

    Hello guys, I'm dealing with stacks and the general concept if pretty straightforward (FILO/LIFO). I'm using a stack to reformat an infix equation to postfix and prefix notation. I've seen several examples of this online, but I'd like to make it my own--so...

    I've created a postfix class with the following method:
    public void stackPF(String inString) throws PointerOutOfBoundException {
    		inString = inString.trim();
    		// Statements to evaluate array
    		for (char ch : inString.toCharArray()) {
    			// Evaluate equation in parenthesis
    			System.out.println("Inside Loop");
    			if (ch == '(') {
    				System.out.println("paran");
    				operatorStack.push(ch);
    				do {
    					if (ch >= 0 && ch <= 9) {
    						operandStack.push(ch);
    					} else {
    						if (priority(ch) >= operatorStack.peek()) {
    							operatorStack.push(ch);
    						}
    					}
    				} while (ch != ')');
    				//Pop characters within parenthesis
    				System.out.println("End paran");
    				operandStack.pop();
    				operatorStack.pop();
    			} else {
    				if (ch >= 0 && ch <= 9) {
    					System.out.println("No paran");
    					operandStack.push(ch);
    				}
    				// Need to account for precedence!
    				else if (ch == '+' || ch == '-' || ch == '/' || ch == '*') {
    					System.out.println("Precedence");
    					if(operatorStack.stackEmpty()){
    						operatorStack.push(ch);
    					}
    					else if (priority(ch) >= operatorStack.peek()) {
    						operatorStack.push(ch);
    						operatorStack.pop();
    					} else {
    						operatorStack.push(ch);
    					}
    				}
    			}
    			size++;
    		}
    ///Evaluate operators
    	private static int priority(char ch) {
     
    		switch (ch) {
    		case '(' :
    			return 0;
    		case '-' : case '+':
    			return 1;
    		case '*' : case '/':
    			return 2;
    		}
    		return -1;
    	}

    With this, I want to evaluate an equation within the parenthesis, and without parenthesis. In working out the details, I'm unsure whether or not it's necessary to create two separate stacks -- one for the operators and one for the operands. By doing it this way, I can empty the lists depending on parenthesis. The problem is when I enter (10+3)*9, I don't get any results and the program never records any of my values. Any ideas as to what might be causing this?

    Within the stack class, I have the following methods (for clarity) in addition to others not here:

    public T pop ( )
    	{
    		if (stackEmpty ( ))
    		{
    			System.out.println ("empty stack");
    			return null;
    		}
    		else
    		{
    			T popped = (T) head.getData ( );
    			head = head.getLink ( );
    			return popped;
    		}
    	}
     
    public void displayStack() throws PointerOutOfBoundException  {
     
    		System.out.println("----------------");
    		while(!empty()) {
    			if(peek() != "(" ){
    			System.out.print(" " + pop() + " ");
    		} else {
    			break;
    		}
    		System.out.println("\n----------------");
    		}
    	}

    Thanks
    Last edited by javaStooge; July 20th, 2014 at 06:13 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: A Stack For Notation

    Please post a runnable example the demonstrates the problem.

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. [Help]Reverse Polish Notation
    By Cluelesscoder in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 21st, 2014, 02:25 AM
  3. Scientific exponential notation
    By JavaCup in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 9th, 2012, 12:39 AM
  4. Big-O Notation question
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: November 28th, 2011, 09:52 PM
  5. [SOLVED] EVALUATION OF POSTFIX NOTATION
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 7th, 2011, 05:24 PM