Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default Arithmetic Stacks

    My program distinguishes between the operators and operands and places them in separate stacks. If the character is an operator, I want compare its precedence to that of the operator on top of the operator stack. If the current operator has higher precedence than the one currently on top of the stack, or the stack is empty, it should be pushed onto the operator stack. If the current operator has the same or lower precedence, the operator on top of the operator stack must be evaluated next. This is done by popping that operator off the operator stack along with a pair of operands from the operand stack and writing a new line in the output table. help


    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Stack;
     
    class Prog3<T> extends MyArrayList<T> {
    	
     
    // method compares operators to establish precedence
    	private static int prec(char x) {
    	if (x == '+' || x == '-')
    		return 1;
    	if (x == '*' || x == '/' || x == '%')
    		return 2;
    	return 0;
    	}
     
     
    	
    	
    	
     
    	public static void main (String args []) 
    	{
    		
    		Stack operatorStack = new Stack();
    		Stack operandStack = new Stack();
    	
    	
    		BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
    		String line = null;
     
    		while (true){
     
    		      System.out.print("Enter an expression: ");
     
    		      try{
     
    		            line = keyboard.readLine();
     
    		      }catch(IOException e){}
    		      
    		      for(int i=0; i < line.length(); i++){
                      if(line.charAt(i) == ' '){
                      }else if(line.charAt(i) == '/'){
                          operatorStack.push('/');
                      }else if(line.charAt(i) == '*'){
                          operatorStack.push('*');
                      }else if(line.charAt(i) == '+'){
                          operatorStack.push('+');
                      }else if(line.charAt(i) == '-'){
                          operatorStack.push('-');
                      }else if(line.charAt(i) >= 97 && line.charAt(i) <= 109){
                       operandStack.push(line.charAt(i));
                     }
     
    		     
     
    		}
    		      
    		   
    		      
    		      
    		      System.out.println(operatorStack);
    		      System.out.println(operandStack);
    }
    		
    	}
    	
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This post has been cross-posted in other forums.

    Cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to provide links in both cross-posts to each other.

  3. #3
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    I've also posted this message on the sun forums.
    forums.sun.com/thread.jspa?messageID=10469244]New To Java - Arithmetic Stacks

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,885
    Rep Power
    25

    Default

    What is your question? I see an assignment description and some code. ??? You need to ask a question if you want an answer.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes explain your question here to get any help form us. We don't know know what you want. If you get any messages in your code comes with them too.

  6. #6
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Ok.... so I want to start off with a while loop

    while(line !=null) {



    if {

    operatorStack.charAt(0) <= operatorStack.charAt(1)
    operatorStack.pop();

    How do I take the first two numbers from the operandStack and get the popped operator between them?

    operandStack.charAt(0) operandStack.charAt(1)


    }

    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,885
    Rep Power
    25

    Default

    Where are you putting what you take off the stack? Your code doesn't have any assignment statement which are used to save the values returned by a method:
    char aChar = operatorStack.charAt(0);

    What does "get ... between them" mean?
    Are you creating a String with the three values in it separated by spaces?
    Something like:
    operand operator operand

    What does the pop method do? Does it return something?

  8. #8
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    I decided to create an array to put what I take off the stack.

    Object[] expression;
    expression = new Object[100];
    if(line.charAt(i) == '/') {
    Object op1 = operatorStack.pop();
    expression[0] = op1;

    if(line.charAt(i) >= 97 && line.charAt(i) <= 109) {
    Object operand1 = operandStack.pop();
    expression[1] = operand1;
    }


    It works for the / but the operand isn't popping.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,885
    Rep Power
    25

    Default

    It works for the / but the operand isn't popping.
    Can you explain what "it works" means?
    Where is the "operand" supposed to pop?

    Add a println() to show the values you're working with. Like what is the value of line.charAt(i)?

    What is the significance of the values 97 and 109?
    Can they be documented by using a constant such as 'a' and 'm'?

  10. #10
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    When I throw a println for line.charAt(i) in there, my expression gets printed like this:

    Enter an expression: a / b
    a

    /

    b

    I also printed expression[0] and expression[1] and it prints like this.
    Enter an expression: a / b
    null
    a
    null
    null
    /
    null
    null
    null
    null
    b

    The constants can be documented using a - m.

    Java Code:
        char a = 97;
                      char m = 109;
                      
    				if(line.charAt(i) >= a && line.charAt(i) <= m) {
                      Object operand1 = operandStack.pop();
                      expression[1] = operand1;
                      }

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,885
    Rep Power
    25

    Default

    printed expression[0] and expression[1] and it prints like this.
    You don't say what is wrong with the output.
    What do the print statements look like that you are using to do the output?
    What variables are null that you are printing?

    println() adds a newline character to the end of the output so the next thing printed will be on the next line.
    If you want the output on the same line, use print() for all but the last item on a line.

  13. #13
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default Let me try and ask a question

    Nevermind the expression array, I'm so confused. Let's play with this code:


    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Stack;
    
    
    import org.omg.CORBA.Current;
    
    class Prog3<T> extends MyArrayList<T> {
    	
    
    // method compares operators to establish precedence
    	private static int prec(char x) {
    	if (x == '+' || x == '-')
    		return 1;
    	if (x == '*' || x == '/' || x == '%')
    		return 2;
    	return 0;
    	}
    	
    
    	 
    
    
    	
    	
    	
    
    	public static void main (String args []) 
    	{
    		
    		Stack operatorStack = new Stack();
    		Stack operandStack = new Stack();
    	
    	
    		BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in));
    		String line = null;
    
    		while (true){
    
    		      System.out.print("Enter an expression: ");
    
    		      try{
    
    		            line = keyboard.readLine();
    
    		      }catch(IOException e){}
    		      
    		      for(int i=0; i < line.length(); i++){
                      if(line.charAt(i) == ' '){
                      }else if(line.charAt(i) == '/'){
                          operatorStack.push('/');
                      }else if(line.charAt(i) == '*'){
                          operatorStack.push('*');
                      }else if(line.charAt(i) == '+'){
                          operatorStack.push('+');
                      }else if(line.charAt(i) == '-'){
                          operatorStack.push('-');
                      }else if(line.charAt(i) >= 97 && line.charAt(i) <= 109){
                       operandStack.push(line.charAt(i));
                      } 
      
                      
                   
    				
    				
    			
    				
    				
    				
    			
                      
    		      }
    		      System.out.println(operatorStack);
    		      System.out.println(operandStack);
    		      
        		      
    		      }
    		      
    		     
    		      
    		      
    		     
    		    	  
    		      
    		
    	}
    	
    
    	}

    Here is an example of what happens when I run the program.
    Enter an expression: a + b - c / d * f
    [+, -, /, *]
    [a, b, c, d, f]

    The operators and operands get popped to the respective stacks that I created. All I need to do now, is put the expression in postfix notation. I want to use the prec method to do so. How can I do this?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,885
    Rep Power
    25

    Default

    put the expression in postfix notation. I want to use the prec method
    Do you have the algorithm for doing that? I haven't been a student in CS for a long time and forget the definitions for all this stuff.

  15. #15
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    Example

    The infix expression "5 + ((1 + 2) * 4) − 3" can be written down like this in RPN(PostFix):

    5 1 2 + 4 * + 3 −

    The expression is evaluated left-to-right, with the inputs interpreted as shown in the following table (the Stack is the list of values the algorithm is "keeping track of" after the Operation given in the middle column has taken place):
    Input Operation Stack Comment
    5 Push operand 5
    1 Push operand 5, 1
    2 Push operand 5, 1, 2
    + Add 5, 3 Pop two values (1, 2) and push result (3)
    4 Push operand 5, 3, 4
    * Multiply 5, 12 Pop two values (3, 4) and push result (12)
    + Add 17 Pop two values (5, 12) and push result (17)
    3 Push operand 17, 3
    − Subtract 14 Pop two values (17, 3) and push result (14)

    When a computation is finished, its result remains as the top (and only) value in the stack; in this case, 14.


    My program doesn't have to evaluate anything, I just need it to print the letters and operators in postfix.

  16. #16
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    sorry for the bad spacing

    Input Operation Stack Comment
    5 Push operand 5
    1 Push operand 5, 1
    2 Push operand 5, 1, 2
    + Add 5, 3 Pop two values (1, 2) and push result (3)
    4 Push operand 5, 3, 4
    * Multiply 5, 12 Pop two values (3, 4) and push result (12)
    + Add 17 Pop two values (5, 12) and push result (17)
    3 Push operand 17, 3
    − Subtract 14 Pop two values (17, 3) and push result (14)

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,885
    Rep Power
    25

    Default

    Thanks for the explanation.
    Now where's your code to do it?
    What does the code you've posted do?
    The algorithm you've posted does the operation and creates a result, but your assignment is to create a postfix string.
    Would that be a different agorithm?

  18. #18
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    So you think I need a postfix string? I don't know how to write the code, it's what I need help with. I gave you the example as an explanation on how to run through postfix algorithm.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,885
    Rep Power
    25

    Default

    All I need to do now, is put the expression in postfix notation
    I thought getting a postfix string was the assignment.
    What is the assignment?

  20. #20
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Illegal Arithmetic Operations?
    By Cruor in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 05:46 PM
  2. Stacks, lists...
    By little_polarbear in forum New To Java
    Replies: 7
    Last Post: 08-02-2008, 02:59 PM
  3. Stacks
    By Zosden in forum Advanced Java
    Replies: 15
    Last Post: 05-05-2008, 09:16 AM
  4. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 10:53 AM
  5. Replies: 1
    Last Post: 08-07-2007, 08:05 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
  •