You are on page 1of 33

postfix calculations

Scan the postfix expression left to right

 If the character x is a digit


 Push it into the stack
 if the character is an operator (+,-,*,/)
 pop two values from the stack
 Perform the operation
 Push the result of the operation into the stack

When all characters in postfix expression are processed pop the


value from the stack and output it.
postfix calculation
Stack
Postfix Expression
6523+8*+3+*

=
postfix calculation
Stack
Postfix Expression
523+8*+3+*

=

6
postfix calculation
Stack
Postfix Expression
23+8*+3+*

5
6
postfix calculation
Stack
Postfix Expression
3+8*+3+*

2 =
5
6
postfix calculation
Stack
Postfix Expression
+8*+3+*

3
2 =
5
6
postfix calculation
Stack
Postfix Expression

8*+3+*

3
2 +=
5
6
postfix calculation
Stack
Postfix Expression
8*+3+*

2 +3=
-5
(6
postfix calculations
Stack
Postfix Expression
8*+3+*

2+3=

-5
(6
postfix calculations
Stack
Postfix Expression
8
d *– +( e3 + f* )

a
2b++
3 c= -5

-5
*(6
postfix calculations
Stack
Postfix Expression
8
– *( e
++3 f+)*

a
b + c= -d
5
-5
*(6
postfix calculations
Stack
Postfix Expression
*–
(e+( e
3+
+ f )f* )

8
a
b + c= -–dd *
5
-5
-*(6
postfix calculations
Stack
Postfix Expression
+
e
– (3 e
( e+ ++ f*)f )
f )+

8
a
b* + =c 
-– dd *
5
(-5
-*(6
postfix calculations
Stack
Postfix Expression
+
( e(3
– +++
f )e f*)f )

a
b* 8+ =c 
-– dd * e
5
(-5
-*(6
postfix calculations
Stack
Postfix Expression
+( )e(3 e
f– +++
f*)f )

+ a
5b* 8+ =c -– dd * e
(-5
-*(6
postfix calculations
Stack
Postfix Expression
+( e(3 e
)– +++
f*)f )

+ a
5b* 8+ =c 40
-– dd * e f

(-5
-*(6
postfix calculations
Stack
Postfix Expression
+
( e(3 e
– +++
f*)f )

a
b + c= -–dd * e f +
40
-5
-*(6
postfix calculations
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a
b++
 c= -–dd * e f + -
40
-5
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a
b++
40c =-– dd * e f +

-5
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a
5b++
40c =-– d
d * e f +

-
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a
5b++
40c =-– d
d*ef+
45

-
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a b+c=-–dd * e f +

-45
18
-*(6
Stack
Postfix Expression
+
3
( e+
– (* e
+* +
f )f )

a b+c=-–dd * e f +
3
-45
18
-*(6
Stack
Postfix Expression
*–
3( e+
(e+* +
f )f )

a b++c=-–dd * e f +
3
-45
18
-*(6
Stack
Postfix Expression
*–
3( e+
(e+* +
f )f )

a b++3 c=-–dd * e f +

-45
18
-*(6
Stack
Postfix Expression
*–
3( e+
(e+* +
f )f )

-– d
a45b + 3c = d*ef+

-18
-*(6
Stack
Postfix Expression
*–
3( e+
(e+* +
f )f )

a45b + 3c =
-– d48
d*ef+

-18
-*(6
Stack
Postfix Expression
*–
3( e+
(e+* +
f )f )

a b+c =-– d
d * e f +

-18
48
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a b* +c=-–dd * e f +

-18
48
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a b* +48
c -–=dd * e f +

-18
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a6 b* +48 =dd * e f +
c -–

-18
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a6 b* +48
c -–
=dd288
*ef+

-18
-*(6
Stack
Postfix Expression
3
( e+
– (e+* +
f )f )

a b+c=-–dd * e f +

-18
*-(6
288

You might also like