Uploaded by jeandevelopergames

Stack-Notation.

advertisement
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
Directions: Convert the following Infix expressions into its equivalent postfix and prefix
expressions.
Prefix Expression
1. (A + B) * (C + D) - E
= (A + B) * (C + D) - E
= + AB * (C + D) - E
● Let + A B = R1
= R1 * (C + D) - E
= R1 * +CD - E
● Let + C D = R2
= R1 * R2 - E
= * R1 R2 - E
● Let * R1 R2 = R3
= R3 - E
= - R3 E
Postfix Expression
1. (A + B) * (C + D) - E
= (A + B) * (C + D) - E
= A B + * (C + D) - E
Let A B + = R1
= R1 * (C + D) - E
= R1 * C D+ - E
Let C D + = R2
= R1 * R2 - E
= R1 R2* - E
Let * R1 R2 = R3
= R3 - E
= R3 E -
R1 = + AB
R2 = + CD
R3 = * R1 R2
R1 = A B +
R2 = C D +
R3 = R1 R2 *
= - R3 E
= - * R1 R2 E
= - * + A B R2 E
= - * + AB + CD E
= R3 E = R1 R2 * E = A B + R2 * E = AB + CD + * E -
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
2. A - (B + C) * D + E / F
POSTFIX EXPRESSION
2. A - (B + C) * D + E / F
= A - (B + C) * D + E / F
=A-+BC*D+E/F
● Let + B C = R1
= A - R1 * D + E / F
= A - R1 * D + / E F
● Let / E F = R2
= A - R1 * D + R2
= A - * R1 D + R2
● Let * R1 D = R3
= A - R3 + R2
= A - + R3 R2
● Let + R3 R2 = R4
= A - R4
= - A R4
= A - (B + C) * D + E / F
=A- BC+*D+E/F
Let B C + = R1
= A - R1 * D + E / F
= A - R1 * D + E F /
Let / E F = R2
= A - R1 * D + R2
= A - R1 D * + R2
Let * R1 D = R3
= A - R3 + R2
= A - R3 R2 +
Let + R3 R2 = R4
= A - R4
= A R4 -
R1 = + B C
R2 = / E F
R3 = * R1 D
R4= + R3 R2
R1 = B C +
R2 = E F /
R3 = R1 D *
R4= R3 R2 +
= - A R4
= - A + R3 R2
= - A + * R1 D R2
= - A + * + B C D R2
=-A+*+BCD/EF
= A R4= A R3 R2 + = A R1 D * R2 + = A B C + D * R2 + =ABC+D*EF/+-
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
3. ( (A + B) / (C - D) + E) * F - G
= ( (A + B) / (C - D) + E) * F - G
= ( (+ A B) / (C - D) + E) * F - G
POSTFIX EXPRESSION
( (A + B) / (C - D) + E) * F - G
= ( (A + B) / (C - D) + E) * F - G
= ( ( A B + ) / (C - D) + E) * F - G
● Let + A B = R1
= ( R1 / (C - D) + E) * F - G
=(R1 / (- C D ) + E) * F - G
Let A B + = R1
= ( R1 / (C - D) + E) * F - G
=(R1 / (C D -) + E) * F - G
● Let - C D = R2
= (R1 / R2 + E) * F - G
= ( / R1 R2 + E) * F - G
Let C D - = R2
= (R1 / R2 + E) * F - G
= ( R1 R2 / + E) * F - G
● Let / R1 R2 = R3
= ( R3 + E) * F - G
= + R3 E * F - G
Let R1 R2 / = R3
= ( R3 + E) * F - G
= R3 E + * F - G
● Let + R3 E = R4
= R4 * F - G
= * R4 F - G
Let R3 E + = R4
= R4 * F - G
= R4 F * - G
● Let * R4 F = R5
= R5 - G
= - R5 G
Let R4 F * = R5
= R5 - G
= R5 G -
R1 = + A B
R2 = - C D
R3 = / R1 R2
R4 = + R3 E
R5 = * R4 F
R1 = A B +
R2 = C D R3 = R1 R2 /
R4 = R3 E +
R5 = R4 F *
= - R5 G
= - * R4 F G
= - * +R3 E F G
= - * + / R1 R2 E F G
= - * + / + A B R2 E F G
=-*+/+AB-CDEFG
= R5 G = R4 F * G = R3 E + F * G = R1 R2 / E + F * G = A B + R2 / E + F * G =AB+CD-/E+F*G-
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
4. A + B * (C + D) - E / F * G + H
POSTFIX EXPRESSION
A + B * (C + D) - E / F * G + H
=A + B * (C + D) - E / F * G + H
=A+B*+CD-E/F*G+H
● Let + C D = R1
= A + B * R1 - E / F * G + H
= A + B * R1 - / E F * G + H
● Let / E F = R2
= A + B * R1 - R2 * G + H
= A + *B R1 - R2 * G + H
● Let * B R1 = R3
= A + R3 - R2 *G + H
= A + R3 - * R2 G + H
● Let * R2 G = R4
=A + R3 - R4 + H
= +A R3 - R4 + H
● Let + A R3 = R5
= R5 - R4 + H
= R5 - + R4 H
● Let + R4 H = R6
= R5 - R6
= - R5 R6
=A + B * (C + D) - E / F * G + H
=A+B*CD+-E/F*G+H
Let C D + = R1
= A + B * R1 - E / F * G + H
= A + B * R1 - E F / * G + H
Let E F / = R2
= A + B * R1 - R2 * G + H
= A + B R1 * - R2 * G + H
Let B R1 * = R3
= A + R3 - R2 *G + H
= A + R3 - R2 G * + H
Let R2 G * = R4
=A + R3 - R4 + H
= A R3 + - R4 + H
Let A R3 + = R5
= R5 - R4 + H
= R5 - R4 H +
Let R4 H + = R6
= R5 - R6
= R5 R6 -
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
R1 = + C D
R2 = / E F
R3 = * B R1
R4 = * R2 G
R5 = + A R3
R6 = + R4 H
R1 = C D +
R2 = E F /
R3 = B R1 *
R4 = R2 G *
R5 = A R3 +
R6 = R4 H +
= - R5 R6
= - + A R3 + R6
= - + A * B R1 + R6
= - + A * B + C D + R6
= - + A * B + C D + R4 H
= - + A * B + C D + * R2 G H
=-+A*B+CD+*/EFGH
= R5 R6 =A R3 + R6 = A B R1 * + R6 = A B C D + R6 = A B C D + R4 H + = A B C D + R2 G * H + =ABCD+EF/G*H+-
PREFIX EXPRESSION
5. (AX + (B * C) )
POSTFIX EXPRESSION
(AX + (B * C) )
=(AX + (B * C) )
= AX + * B C
● Let * B C = R1
= AX + R1
= +AX R1
=(AX + (B * C) )
= AX + B C *
Let B C * = R1
= AX + R1
= AX R1 +
R1 = * B C
R1 = B C *
= + AX R1
= + AX * B C
= AX R1 +
= AX B C * +
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
POSTFIX EXPRESSION
6. A/B + C * D - E
= A/B + C * D - E
=/AB+C*D-E
● Let / A B = R1
= R1 + C * D - E
= R1 + * C D - E
● Let * C D = R2
= R1 + R2 - E
= + R1 R2 - E
● Let + R1 R2 = R3
= R3 - E
= - R3 E
A/B + C * D - E
= A/B + C * D - E
=AB/+C*D-E
Let A B / = R1
= R1 + C * D - E
= R1 + C D * - E
Let C D * = R2
= R1 + R2 - E
= R1 R2 + - E
Let R1 R2 + = R3
= R3 - E
= R3 E -
R1 = / A B
R2 = * C D
R3 = + R1 R2
R1 = A B /
R2 = C D *
R3 = R1 R2 +
= - R3 E
= - + R1 R2 E
= - + / A B R2 E
=-+/AB*CDE
= R3 E = R1 R2 E + = A B / R2 E + =AB/CD*E+-
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
7. 4 * 5 + 3 / 2 - 9
POSTFIX EXPRESSION
4*5+3/2-9
=4*5+3/2-9
=4*5+/32-9
● Let / 3 2 = R1
=4 * 5 + R1 - 9
= * 4 5 + R1 - 9
● Let * 4 5 = R2
= R2 + R1 - 9
= + R2 R1 - 9
● Let + R2 R1 = R3
=R3 - 9
= - R3 9
=4*5+3/2-9
=4*5+32/-9
Let 3 2 / = R1
=4 * 5 + R1 - 9
= 4 5 * + R1 - 9
Let 4 5 * = R2
= R2 + R1 - 9
= R2 R1 + - 9
Let R2 R1 + = R3
=R3 - 9
= R3 9 -
R1= / 3 2
R2= * 4 5
R3= + R1 R2
R1= 3 2 /
R2= 4 5 *
R3= R1 R2 +
= - R3 9
= - R1 R2 9
= - / 3 2 R2 9
=-/32*459
= R3 9 = R1 R2 + 9 = 3 2 / R2 + 9 =32/45*+9-
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
8. 9 + 3 * 5 / (10 - 4)
POSTFIX EXPRESSION
9 + 3 * 5 / (10 - 4)
= 9 + 3 * 5 / (10 - 4)
= 9 + 3 * 5 / - 10 4
= 9 + 3 * 5 / (10 - 4)
= 9 + 3 * 5 / 10 4 -
● Let - 10 4 = R1
Let 10 4 - = R1
= 9 + 3 * 5 / R1
= 9 + 3 * / 5 R1
= 9 + 3 * 5 / R1
= 9 + 3 * 5 R1 /
● Let / 5 R1 = R2
Let 5 R1 / = R2
= 9 + 3 * R2
= 9 + * 3 R2
● Let * 3 R2 = R3
= 9 + R3
= + 9 R3
= 9 + 3 * R2
= 9 + 3 R2 *
Let 3 R2 * = R3
= 9 + R3
= 9 R3 +
R1 = - 10 4
R2 = / 5 R1
R3 = * 3 R2
R1 = 10 4 R2 = 5 R1 /
R3 = 3 R2 *
= + 9 R3
= + 9 * 3 R2
= + 9 * 3 / 5 R1
= + 9 * 3 / 5 - 10 4
= 9 R3 +
= 9 3 R2 * +
= 9 3 5 R1 / * +
= 9 3 5 10 4 - / * +
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
9. ( ( A + B) - C * (D / E) ) + F
POSTFIX EXPRESSION
( ( A + B) - C * (D / E) ) + F
= ( ( A + B) - C * (D / E) ) + F
= ( (A + B) - C * (/ D E) ) + F
● Let / D E = R1
= ( (A + B) - C * R1 ) + F
= ( + A B - C * R1 ) + F
● Let + A B = R2
= ( R2 - C * R1) + F
= ( R2 - * C R1) + F
= ( ( A + B) - C * (D / E) ) + F
= ( (A + B) - C * D E / ) + F
Let D E / = R1
= ( (A + B) - C * R1 ) + F
= ( (A B +) - C * R1 ) + F
Let A B + = R2
= ( R2 - C * R1) + F
= ( R2 - C R1 *) + F
● Let * C R1 = R3
Let C R1 * = R3
= ( R2 - R3) + F
= ( - R2 R3) + F
= ( R2 - R3) + F
= ( R2 R3 - ) + F
● Let - R2 R3 = R4
= R4 + F
= + R4 F
Let R2 R3 - = R4
= R4 + F
= R4 F +
R1 = / D E
R2 = + A B
R3 = * C R1
R4 = - R2 R3
R1 = D E /
R2 = A B +
R3 = C R1 *
R4 = R2 R3 -
= + R4 F
= + - R2 R3 F
= + - + A B R3 F
= + - + A B * C R1 F
=+-+AB*C/DEF
= R4 F +
= R2 R3 - F +
= A B + R3 - F +
= A B + C R1 - F +
=AB+CDE/-F+
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
10. (G - (F ^ E / D + C) - BA
POSTFIX EXPRESSION
(G - (F ^ E / D + C) - BA
= (G - (F ^ E / D + C) - BA
= (G - ( ^ F E / D + C) - BA
● Let ^ F E = R1
= (G - (R1 / D + C) - BA
= (G - ( / R1 D + C) - BA
● Let /R1 D = R2
= (G - (R2 + C) - BA
= (G - (+ R2 C) - BA
● Let + R2 C = R3
= G - R3 - BA
= - G R3 - BA
● Let - G R3 = R4
= R4 - BA
= - R4 BA
= (G - (F ^ E / D + C) - BA
= (G - ( F E ^ / D + C) - BA
= Let F E ^ = R1
= (G - (R1 / D + C) - BA
= (G - ( R1 D / + C) - BA
Let R1 D / = R2
= (G - (R2 + C) - BA
= (G - ( R2 C +) - BA
Let R2 C + = R3
= G - R3 - BA
= G R3 - - BA
Let G R3 - = R4
= R4 - BA
= R4 BA -
R1 = ^ F E
R2 = / R1 D
R3 = + R2 C
R4 = - G R3
R1 = F E ^
R2 = R1 D /
R3 = R2 C +
R4 = G R3 -
= - R4 BA
= - - G R3 BA
= - - G + R2 C BA
= - - G + / R1 D C BA
= - - G + / ^ F E D C BA
= R4 BA = G R3 - BA = G R2 C + - BA = G R1 D / C + - BA = G F E ^ D / C + - BA -
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
11. (2 * 4 - (5 + 7 / 3 ^ 4) - 8) 10
= (2 * 4 - (5 + 7 / 3 ^ 4) - 8) 10
= ( 2 * 4 - ( 5 + 7 / ^ 3 4 ) - 8 ) 10
POSTFIX EXPRESSION
(2 * 4 - (5 + 7 / 3 ^ 4) - 8) 10
= (2 * 4 - (5 + 7 / 3 ^ 4) - 8) 10
= ( 2 * 4 - ( 5 + 7 / 3 4 ^ ) - 8 ) 10
● Let ^ 3 4 = R1
= ( 2 * 4 - ( 5 + 7 / R1 ) - 8) 10
= ( 2 * 4 - ( 5 + / 7 R1 ) - 8) 10
Let 3 4 ^ = R1
= ( 2 * 4 - ( 5 + 7 / R1 ) - 8) 10
= ( 2 * 4 - ( 5 + 7 R1 / ) - 8) 10
● Let / 7 R1 = R2
= ( 2 * 4 - ( 5 + R2) - 8) 10
= ( 2 * 4 - ( + 5 R2) -8) 10
Let 7 R1 / = R2
= ( 2 * 4 - ( 5 + R2) - 8) 10
= ( 2 * 4 - ( 5 R2 +) -8) 10
● Let + 5 R2 = R3
= ( 2 * 4 - R3 - 8) 10
= ( * 2 4 - R3 - 8) 10
Let 5 R2 + = R3
= ( 2 * 4 - R3 - 8) 10
= ( 2 4 + - R3 - 8) 10
● Let * 2 4 = R4
= (R4 - R3 - 8) 10
= ( - R4 R3 - 8) 10
Let 2 4 * = R4
= (R4 - R3 - 8) 10
= ( R4 R3 - - 8) 10
● Let -R4 R3 = R5
= (R5 - 8) 10
= - R5 8 10
Let R4 R3 - = R5
= (R5 - 8) 10
= R5 8 10 -
R1 = ^ 3 4
R2 = / 7 R1
R3 = + 5 R2
R4 = * 2 4
R5 = - R4 R3
R1 = 3 4 ^
R2 = 7 R1 /
R3 = 5 R2 +
R4 = 2 4 *
R5 = R4 R3 -
= - R5 8 10
= - - R4 R3 8 10
= - - * 2 4 R3 8 10
= - - * 2 4 + 5 R2 8 10
= - - * 2 4 + 5 / 7 R1 8 10
= - - * 2 4 + 5 / 7 ^ 3 4 8 10
= R5 8 10 = R4 R3 - 8 10 = 2 4 * R3 - 8 10 = 2 4 * 5 R2 - 8 10 = 2 4 * 5 7 R1 / - 8 10 = 2 4 * 5 7 3 4 ^ / - 8 10 -
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
12. (A + B) * (C * D - E) * F / G
= (A + B) * (C * D - E) * F / G
= (A + B) * ( * C D - E) * F / G
POSTFIX EXPRESSION
(A + B) * (C * D - E) * F / G
= (A + B) * (C * D - E) * F / G
= (A + B) * ( C D * - E) * F / G
● Let * C D = R1
= (A + B) * (R1 - E) * F / G
= + A B * (R1 - E) * F / G
Let C D * = R1
= (A + B) * (R1 - E) * F / G
= + A B * (R1 - E) * F / G
● Let + A B = R2
= R2 * (R1 - E) * F / G
= R2 * - R1 E * F / G
Let A B + = R2
= R2 * (R1 - E) * F / G
= R2 * R1 E - * F / G
● Let - R1 E = R3
= R2 * R3 * F / G
= R2 * R3 * / F G
Let R1 E - = R3
= R2 * R3 * F / G
= R2 * R3 * F G /
● Let / F G = R4
= R2 * R3 * R4
= * R2 R3 * R4
Let F G / = R4
= R2 * R3 * R4
= R2 R3 * * R4
● Let * R2 R3 = R5
= R5 * R4
= * R5 R4
Let R2 R3 * = R5
= R5 * R4
= R5 R4 *
R1 = * C D
R2 = + A B
R3 = - R1 E
R4 = / F G
R5 = * R2 R3
R1 = C D *
R2 = A B +
R3 = R1 E R4 = F G /
R5 = R2 R3 *
= * R5 R4
= * R2 R3 R4
= * + A B R3 R4
= * + A B - R1 E R4
= * + A B - * C D E R4
=*+AB-*CDE/FG
= R5 R4 *
= R2 R3 * R4 *
= A B + R3 * R4 *
= A B + R1 E - R4 *
= A B + C D * E - R4 *
=AB+CD*E-FG/*
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
13. A - B / C * D ^ E
POSTFIX EXPRESSION
A-B/C *D^E
=A-B/C*D^ E
=A-B/C*^DE
=A-B/C*D^ E
=A-B/C*DE^
● Let ^ D E = R1
Let D E ^ = R1
= A - B / C * R1
= A - / B C * R1
= A - B / C * R1
= A - B C / * R1
● Let / B C = R2
= A - R2 * R1
= A - * R2 R1
● Let *R2 R1 = R3
= A - R3
= - A R3
Let B C / = R2
= A - R2 * R1
= A - R2 R1 *
Let R2 R1 * = R3
= A - R3
= A R3 -
R1 = ^ D E
R2 = / B C
R3 = * R2 R1
R1 = D E ^
R2 = B C /
R3 = R2 R1 *
= - A R3
= - A * R2 R1
= - A * / B C R1
=-A*/BC^DE
= A R3 = A R2 R1 * = A B C / R1 * =ABC/DE^*-
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
14. P + Q - R * T
POSTFIX EXPRESSION
P+Q-R*T
=P+Q-R*T
=P+Q-*RT
=P+Q-R*T
=P+Q-RT*
● Let * R T = R1
Let R T * = R1
= P + Q - R1
= + P Q - R1
● Let + P Q = R2
= R2 - R1
= - R2 R1
= P + Q - R1
= P Q + - R1
Let P Q + = R2
= R2 - R1
= R2 R1 -
R1 = * R T
R2 = + P Q
R1 = R T *
R2 = P Q +
= - R2 R1
= - + P Q R1
=-+PQ*RT
= R2 R1 = P Q R1 =PQRT*-
Brizuela, Carmeli Jacqueline G.
BAIT 2F
CC104
Stack Notation
PREFIX EXPRESSION
15. X + Y * Z + ( P * Q + R ) * S
=X+Y*Z+(P*Q+R)*S
= X + Y * Z + ( *P Q + R) * S
POSTFIX EXPRESSION
X+Y*Z+(P*Q+R)*S
=X+Y*Z+(P*Q+R)*S
= X + Y * Z + ( P Q * + R) * S
● Let * P Q = R1
= X + Y * Z + ( R1 + R) * S
= X + Y * Z + ( + R1 R ) * S
Let P Q * = R1
= X + Y * Z + ( R1 + R) * S
= X + Y * Z + ( R1 R + ) * S
● Let + R1 R = R2
= X + Y * Z + R2 * S
= X + * Y Z + R2 * S
Let R1 R + = R2
= X + Y * Z + R2 * S
= X + Y Z * + R2 * S
● Let * Y Z = R3
= X + R3 + R2 * S
= X + R3 + * R2 S
Let Y Z * = R3
= X + R3 + R2 * S
= X + R3 + R2 S *
● Let *R2 S = R4
= X + R3 + R4
= + X R3 + R4
Let R2 S * = R4
= X + R3 + R4
= X R3 + + R4
● Let + X R3 = R5
= R5 + R4
= + R5 R4
Let X R3 + = R5
= R5 + R4
= R5 R4 +
R1 = * P Q
R2= + R1 R
R3= * Y Z
R4= * R2 S
R5= + X R3
R1 = P Q *
R2= R1 R +
R3= Y Z *
R4= R2 S *
R5= X R3 +
= + R5 R4
= + X R3 R4
= + X * Y Z R4
= + X * Y Z * R2 S
= + X * Y Z * + R1 R S
=+X*YZ*+*PQRS
= R5 R4 +
= X R3 + R4 +
= X Y Z * + R4 +
= X Y Z * + R2 S * +
= X Y Z * + R1 R + S * +
=XYZ*+PQ*R+S*+
Download