- Joined
- Dec 7, 2009
- Messages
- 2
- Reaction score
- 0
who can help me to solve this i'm begganer to use java and i try to solve it to much but i faild ....... :cry: :cry: :cry: :cry:
We are accustomed to writing arithmetic expressions in infix notation, which means that the operator appears between the operands: 3 * ((4 + 6) / 2). An alternative is to use postfix notation, which means that the operator appears after the operands. The expression below is equivalent to the infix expression above: 3 4 6 + 2 / *
One advantage of postfix is that unlike infix, no parentheses are necessary. Another advantage is that postfix expressions are easily evaluated using a stack. This is done by processing the expression from left to right as follows:
Make an empty stack s
Scan the infix expression one item at a time, for each token (operator * - + / or single digit integers 0, 1, …9) in the infix expression:
If it is an operand, push onto the stack.
If it is an operator,
pop two numbers from the stack,
apply the operator, and
push the result back onto the stack.
If the stack does not have enough operands, the expression is invalid.
At the end of input, check the stack. If it has just one item, that is the answer. Else, the expression is invalid.
Postfix expressions do not need ( ). They have only operands and operators.
Consider the expression 2 3 4 + 9 * 1 - +
Item Stack
empty
2 2
3 2 3
4 2 3 4
+ 2 7
9 2 7 9
* 2 63
1 2 63 1
- 2 62 Note: oprnd1 - oprnd2
+ 64 Answer is 64.
Write a Java program to evaluate postfix expressions
We are accustomed to writing arithmetic expressions in infix notation, which means that the operator appears between the operands: 3 * ((4 + 6) / 2). An alternative is to use postfix notation, which means that the operator appears after the operands. The expression below is equivalent to the infix expression above: 3 4 6 + 2 / *
One advantage of postfix is that unlike infix, no parentheses are necessary. Another advantage is that postfix expressions are easily evaluated using a stack. This is done by processing the expression from left to right as follows:
Make an empty stack s
Scan the infix expression one item at a time, for each token (operator * - + / or single digit integers 0, 1, …9) in the infix expression:
If it is an operand, push onto the stack.
If it is an operator,
pop two numbers from the stack,
apply the operator, and
push the result back onto the stack.
If the stack does not have enough operands, the expression is invalid.
At the end of input, check the stack. If it has just one item, that is the answer. Else, the expression is invalid.
Postfix expressions do not need ( ). They have only operands and operators.
Consider the expression 2 3 4 + 9 * 1 - +
Item Stack
empty
2 2
3 2 3
4 2 3 4
+ 2 7
9 2 7 9
* 2 63
1 2 63 1
- 2 62 Note: oprnd1 - oprnd2
+ 64 Answer is 64.
Write a Java program to evaluate postfix expressions