# Arithmetic Stacks

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-17-2008, 02:56 AM
unc123w
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

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); }                         }                 }```
• 10-17-2008, 03:04 AM
Fubarable
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.
• 10-17-2008, 04:13 AM
unc123w
I've also posted this message on the sun forums.
forums.sun.com/thread.jspa?messageID=10469244]New To Java - Arithmetic Stacks
• 10-17-2008, 04:19 AM
Norm
What is your question? I see an assignment description and some code. ??? You need to ask a question if you want an answer.
• 10-18-2008, 11:02 AM
Eranga
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.
• 10-18-2008, 10:36 PM
unc123w
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)

}

}
• 10-18-2008, 11:48 PM
Norm
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?
• 10-20-2008, 02:48 AM
unc123w
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.
• 10-20-2008, 04:22 AM
Norm
Quote:

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-20-2008, 04:53 AM
unc123w
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.

Code:

```    char a = 97;                   char m = 109;                                                   if(line.charAt(i) >= a && line.charAt(i) <= m) {                   Object operand1 = operandStack.pop();                   expression[1] = operand1;                   }```
• 10-20-2008, 11:11 AM
Eranga
Debug and see what are the elements in your stack, before pushing and before adding.
• 10-20-2008, 02:36 PM
Norm
Quote:

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.
• 10-21-2008, 12:22 AM
unc123w
Let me try and ask a question
Nevermind the expression array, I'm so confused. Let's play with this code:

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?
• 10-21-2008, 01:02 AM
Norm
Quote:

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.
• 10-21-2008, 01:12 AM
unc123w
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.
• 10-21-2008, 01:14 AM
unc123w

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)
• 10-21-2008, 03:12 AM
Norm
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?
• 10-21-2008, 04:43 AM
unc123w
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.
• 10-21-2008, 02:28 PM
Norm
Quote:

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?
• 10-21-2008, 03:52 PM
unc123w
Getting a postfix string is what I need.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last