Different way to write
arithmetic expressions
- There are 3 ways to
write arithmetic expressions:
-
In-fix:
operators
are placed
between
their
operands
(A + B) x C = (A + B) x C
|
-
Pre-fix:
operators
are placed
before their
operands
-
Post-fix:
operators
are placed
after their
operands
|
- The pre-fix and
post-fix
notations
do not
use
parenthesis to
write
arithmetic expressions !
|
Calculator
that made the reverse-polish or
post-fix notation
popular
How to evaluate expressions in
Reverse Polish Notation
- The operator
always
follows its
(2) operands:
- When you evaluate
an operation in
RPN, the
result is used
an operand of
another operation
Example:
3 4 + 1 −
<==> 7 1 −
<==> 6 ( = 7 − 1)
|
- Conclusion:
- Each operator will
operate on
its preceeding 2 operands
- Each operator will
produce
a result that will be
the operand of
some subsequent
operator
|
|
Algorithm to
evaluate a
RPN expression
Algorithm to
evaluate a
RPN expression
- Result:
4 3 + 2 x
^
|
curr input
Stack
+------+
| |
+------+
| |
+------+
| |<---- stackTop
+------+
| 4 |
+------+
|
Continue...
|
Algorithm to
evaluate a
RPN expression
Algorithm to
evaluate a
RPN expression
- Result:
4 3 + 2 x
^
|
curr input
Stack
+------+
| |
+------+
| |<---- stackTop
+------+
| 3 |
+------+
| 4 |
+------+
|
Continue...
|
Algorithm to
evaluate a
RPN expression
Algorithm to
evaluate a
RPN expression
- (1) pop
3
and 4
off the stack,
(2) add 3 + 4 = 7 and
(3)
push
the result (7):
4 3 + 2 x
^
|
curr input
Stack
+------+ +------+
| | | |
+------+ +------+
| | | |
+------+ +------+
| 3 | | |<---- stackTop
+------+ +------+
| 4 | | 7 | (3+4)
+------+ +------+
|
Continue...
|
Algorithm to
evaluate a
RPN expression
Algorithm to
evaluate a
RPN expression
- Result:
4 3 + 2 x
^
|
curr input
Stack
+------+
| |
+------+
| |<---- stackTop
+------+
| 2 |
+------+
| 7 |
+------+
|
Continue...
|
Algorithm to
evaluate a
RPN expression
Algorithm to
evaluate a
RPN expression
- (1) pop
2
and 7
off the stack,
(2) add 7 x 2 = 14 and
(3)
push
the result (14):
4 3 + 2 x
^
|
curr input
Stack
+------+ +------+
| | | |
+------+ +------+
| | | |
+------+ +------+
| 2 | | |<---- stackTop
+------+ +------+
| 7 | | 14 | (7x2)
+------+ +------+
|
Continue...
|
Algorithm to
evaluate a
RPN expression
Algorithm to
evaluate a
RPN expression
- Java method
to evaluate
a RPN expression:
// inp[] = array of String representing a RPN expression (e.g.: "3" "4" "+')
public static int evalRPN(String[] inp)
{
Stack<Integer> opStack; // Stack containing the prior oprands
String s; // Help variable containg the next symbol
for ( int i = 0; i < inp.length; i++ )
{
s = inp[i]; // Next item in input (as String !)
if ( s.equals("x") || s.equals("/") ||
s.equals("+") || s.equals("-") )
{ // Next symbol is an operator
int o2 = opStack.pop(); // Get the last 2 operands
int o1 = opStack.pop();
int r = operate(s, o1, o2); // Perform operation
opStack.push( r ); // Save result on stack
}
else // Next symbol is an number (i.e.: operand)
opStack.push( Integer.parseInt(s) ); // Save it as Integer
}
return opStack.pop(); // Return result (was saved on stack)
}
|
|
Algorithm to
evaluate a
RPN expression
- Process the
next symbol
in the input inp[]:
// inp[] = array of String representing a RPN expression (e.g.: "3" "4" "+')
public static int evalRPN(String[] inp)
{
Stack<Integer> opStack; // Stack containing the prior oprands
String s; // Help variable containg the next symbol
for ( int i = 0; i < inp.length; i++ )
{
s = inp[i]; // s = next item/symbol in input (as String !)
if ( s.equals("x") || s.equals("/") ||
s.equals("+") || s.equals("-") )
{ // Next symbol is an operator
int o2 = opStack.pop(); // Get the last 2 operands
int o1 = opStack.pop();
int r = operate(s, o1, o2); // Perform operation
opStack.push( r ); // Save result on stack
}
else // Next symbol is an number (i.e.: operand)
opStack.push( Integer.parseInt(s) ); // Save it as Integer
}
return opStack.pop(); // Return result (was saved on stack)
}
|
|
Algorithm to
evaluate a
RPN expression
- If the next symbol is
an
operator (x, /, +, -),
we perform the
operation on the
last 2 operands
(popped
off the stack) and
save
the result as a
(future) operand on the
stack:
// inp[] = array of String representing a RPN expression (e.g.: "3" "4" "+')
public static int evalRPN(String[] inp)
{
Stack<Integer> opStack; // Stack containing the prior oprands
String s; // Help variable containg the next symbol
for ( int i = 0; i < inp.length; i++ )
{
s = inp[i]; // s = next item/symbol in input (as String !)
if ( s.equals("x") || s.equals("/") ||
s.equals("+") || s.equals("-") )
{ // Next symbol is an operator
int o2 = opStack.pop(); // Get the last 2 operands
int o1 = opStack.pop();
int r = operate(s, o1, o2); // Perform operation
opStack.push( r ); // Save result (operand) on stack
}
else // Next symbol is an number (i.e.: operand)
opStack.push( Integer.parseInt(s) ); // Save it as Integer
}
return opStack.pop(); // Return result (was saved on stack)
}
|
|
Algorithm to
evaluate a
RPN expression
- Otherwise,
the next symbol must be
an
number (= operand) and
we push it
on the stack
(because the operator is
found
later in the
input):
// inp[] = array of String representing a RPN expression (e.g.: "3" "4" "+')
public static int evalRPN(String[] inp)
{
Stack<Integer> opStack; // Stack containing the prior oprands
String s; // Help variable containg the next symbol
for ( int i = 0; i < inp.length; i++ )
{
s = inp[i]; // s = next item/symbol in input (as String !)
if ( s.equals("x") || s.equals("/") ||
s.equals("+") || s.equals("-") )
{ // Next symbol is an operator
int o2 = opStack.pop(); // Get the last 2 operands
int o1 = opStack.pop();
int r = operate(s, o1, o2); // Perform operation
opStack.push( r ); // Save result (operand) on stack
}
else // Next symbol is an number (i.e.: operand)
opStack.push( Integer.parseInt(s) ); // Save number as Integer
}
return opStack.pop(); // Return result (was saved on stack)
}
|
|
Algorithm to
evaluate a
RPN expression
- When the algorithm has
finished, the
result is on
the stack
⇒
we
return the
result:
// inp[] = array of String representing a RPN expression (e.g.: "3" "4" "+')
public static int evalRPN(String[] inp)
{
Stack<Integer> opStack; // Stack containing the prior oprands
String s; // Help variable containg the next symbol
for ( int i = 0; i < inp.length; i++ )
{
s = inp[i]; // s = next item/symbol in input (as String !)
if ( s.equals("x") || s.equals("/") ||
s.equals("+") || s.equals("-") )
{ // Next symbol is an operator
int o2 = opStack.pop(); // Get the last 2 operands
int o1 = opStack.pop();
int r = operate(s, o1, o2); // Perform operation
opStack.push( r ); // Save result (operand) on stack
}
else // Next symbol is an number (i.e.: operand)
opStack.push( Integer.parseInt(s) ); // Save number as Integer
}
return opStack.pop(); // Return result (was saved on stack)
}
|
|
Algorithm to
evaluate a
RPN expression
The
operate()
method used
to perform an
arithmetic operation:
public static int operate( String op, int o1, int o2 )
{
if ( op.equals("x") ) // Multiply
{
return( o1*o2 );
}
else if ( op.equals("/") )
{
return( o1/o2 );
}
else if ( op.equals("+") )
{
return( o1+o2 );
}
else if ( op.equals("-") )
{
return( o1-o2 );
}
else
return 0;
}
|
DEMO:
demo/09-stack/reverse-polish/Demo.java +
Demo2.java +
DemoX.java +
RPN.java
❮
❯