Results 1 to 20 of 23
Thread: Arithmetic Stacks
- 10-17-2008, 02:56 AM #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.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); } } }
-
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 #3
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 #4
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 #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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 #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)
}
}
- 10-18-2008, 11:48 PM #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?
- 10-20-2008, 02:48 AM #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.
- 10-20-2008, 04:22 AM #9
Can you explain what "it works" means?It works for the / but the operand isn't popping.
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 #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; }
- 10-20-2008, 11:11 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Debug and see what are the elements in your stack, before pushing and before adding.
- 10-20-2008, 02:36 PM #12
You don't say what is wrong with the output.printed expression[0] and expression[1] and it prints like this.
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 #13
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?
- 10-21-2008, 01:02 AM #14
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.put the expression in postfix notation. I want to use the prec method
- 10-21-2008, 01:12 AM #15
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 #16
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)
- 10-21-2008, 03:12 AM #17
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 #18
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 #19
I thought getting a postfix string was the assignment.All I need to do now, is put the expression in postfix notation
What is the assignment?
- 10-21-2008, 03:52 PM #20
Similar Threads
-
Illegal Arithmetic Operations?
By Cruor in forum New To JavaReplies: 13Last Post: 09-19-2008, 04:46 PM -
Stacks, lists...
By little_polarbear in forum New To JavaReplies: 7Last Post: 08-02-2008, 01:59 PM -
Stacks
By Zosden in forum Advanced JavaReplies: 15Last Post: 05-05-2008, 08:16 AM -
Using Stacks
By ravian in forum New To JavaReplies: 7Last Post: 11-28-2007, 09:53 AM -
I want to be able to do this with stacks and queues as well as with vectors
By carl in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks