Professional Documents
Culture Documents
12arithmetic Expressions PDF
12arithmetic Expressions PDF
ARITHMETIC EXPRESSIONS
One of the most important applications of stacks is the evaluation of
arithmetic expressions.
Consider the simple arithmetic expression;
A+B
We have three possibilities for the positioning of the operator;
1. Before the operands as
+AB
which is called Prefix notation or Polish notation.
(after the Polish logician Jan Lu Lukasiewicz)
2. Between the operands
A+B
which is called Infix notation .
3. After the operands
AB+
which is called Postfix notation or
Reverse Polish notation .
How to convert
from infix to postfix
from infix to prefix
Arithmetic Expressions
operators
^
priority
highest
*,/
+,-
lowest
Example
A/B^C+D*E-A*C
infix form;
infix form
( ( A / ( B^C ) ) + ( DE ) ( AC )
( ( ( ( ABC^/ ) + ( DE ) ) ( AC ) ) )
( ( ( ABC^/DE )+ ) ( AC ) )
ABC^/DE+AC
postfix form
Arithmetic Expressions
infix form
( ( ( A / (B^C) + ( D E ) ) ( A C ) )
( ( ( A / (^BC)) + ( DE ) ) ( AC ) )
( ( /A^BC)) + (DE)) (AC) )
( + /A^BCDE) (AC) )
+/A^BCDEAC
prefix form
Advantage
1. The need for parantheses is eliminated
2. The precedence rule is no longer relevant
3. The expression is evaluated by making
left to right scan for postfix
right to left scan for prefix
by the use of stacking.
Arithmetic Expressions
Example
(B^2 4 A C )^(1/2)
infix to postfix
(B^2 4 A C )^(1/2)
( (B2^) ((4A) C )^(12/)
( (B2^) ((4AC) )^(12/)
( B2^4AC )^(12/)
( B2^4AC12/^)
postfix notation
infix to prefix
( B^2 4 A C )^(1/2)
( B^2) (( 4 A) C ))^( 1/2)
( (^B2) ( ( 4 A) C ))^(/12)
( (^B2) ( 4 A C ))^(/12)
( (^B24AC ) )^(/12)
(^^B24AC/12)
prefix notation
Arithmetic Expressions
ii)
iii)
3. When end of infix expression is reached, pop and display stack items
until the stack is empty.
Arithmetic Expressions
infix to postfix
an array for
infix expr.
A+BC#
an array for
postfix expr.
operator
stack
empty
# (empty)
+B C#
B C#
#+
#+
C#
AB
C#
AB
#+
ABC
#+
ABC
#+
ABC+
empty
# (empty)
B +C#
B +C#
AB+C#
+C#
AB
C#
AB
C#
AB
#+
ABC
#+
ABC+
If incoming
operator ()
has higher
priority than
top stack
element (+)
then, push it.
pop and
display until
incoming
operator (+)
lower priority
than stack top
or empty.
ABC+
Arithmetic Expressions
Example:
If the table of priorities
Incoming priority
Symbol (operator)
Instack priority
postfix
operator
stack
A (B+CD/E)/F#
(empty)
#(empty)
(B+CD/E)/F#
(B+CD/E)/F#
B+CD/E)/F#
# (
+CD/E)/F#
AB
# (
CD/E)/F#
AB
# (+
ABC
# (+
ABC+
# (
ABC+
# (
/E)/F#
ABC+D
# (
E)/F#
ABC+D
# (/
ABC+DE
# (/
ABC+DE/
# (
ABC+DE/
ABC+DE/
ABC+DE/
F#
ABC+DE/
#/
ABC+DE/ F
#/
ABC+DE/ F/
#(empty)
D/E)/F#
D/E)/F#
)/F#
/F#
= +
/=
Arithmetic Expressions
infix
postfix
(A+B) C #
(empty)
operator
stack
#(empty)
A+B) C #
(empty)
#(
+B) C #
#(
B) C #
# (+
AB
# (+
C#
AB+
#(
C#
AB+
#(
AB+
#(
AB+C
#(
AB+C
#(empty)
)C#
Pop and
display
until left
paranthesis
AB+C
infix
postfix
A B + (C D/E) #
operator
stack
(empty)
#(empty)
B + (C D/E) #
B + (C D/E) #
AB
(C D/E) #
AB
#+
C D/E) #
AB
#+(
D/E) #
AB C
#+(
D/E) #
AB C
#+(
/E) #
AB CD
#+(
E) #
AB CD
#+(/
AB CDE
#+(/
AB CDE/
#+(
AB CDE/
#+
AB CDE/+
+ (C D/E) #
)#
(+=) pop
AB CDE/+
Operators in
stack are in
increasingly order!
Arithmetic Expressions
Postfix
1. Scan the expression from left to right.
postfix
3+4 5
345 +
45 +
5+
3
4
3
5
4
3
infix
postfix
(7+2) 3
72+3
2+3
+3
3
7
2
7
9
3
9
end of expression
27 Empty it.
Arithmetic Expressions
4 95+
95+
5+
9 + 5= 14
8
4
2
14
8
2 4=8
95+
95+
5
9
8
8 - 14= -6
9
8
-6
end of
expression
-6
is the result
Arithmetic Expressions
prefix
infix
prefix
3(2-6)
3-26
(5+3) (6-4)
+53-64
3(-26)
+53(-64)
3(2-6)
+53(6-4)
3(-4)
+53(2)
(-4) 3
(+53)(2)
-12
result
(5+3)(2)
(8)(2)
(8) (2)
16
result