1. ## 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.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();

String line = null;

while (true){

System.out.print("Enter an expression: ");

try{

}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. 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. I've also posted this message on the sun forums.
forums.sun.com/thread.jspa?messageID=10469244]New To Java - Arithmetic Stacks

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

5. 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. 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. 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. 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. 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. 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. 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.

12. ## 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.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();

String line = null;

while (true){

System.out.print("Enter an expression: ");

try{

}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?

13. 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.

14. 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.

15. 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)

16. 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?

17. 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.

18. 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?

19. Getting a postfix string is what I need.

Page 1 of 2 12 Last

#### Posting Permissions

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