Skip to main content

Convert and evaluate A+(B*C-(D/E^F)*G)*H

Short answer

Postfix: ABC*DEF^/G*-H*+. Evaluating with A=2, B=3, C=4, D=8, E=2, F=3, G=5, H=2 yields 16.

Convert the following infix expression into postfix and also evaluate it: A+(B*C-(D/E^F)*G)*H

Conversion of A + (B * C − (D / E ^ F) * G) * H

^ has the highest precedence and is right-associative; then * /; then + -.

StepSymbolStack (bottom → top)Postfix output
1A(empty)A
2++A
3(+(A
4B+(AB
5*+(*AB
6C+(*ABC
7-+(-ABC*
8(+(-(ABC*
9D+(-(ABC*D
10/+(-(/ABC*D
11E+(-(/ABC*DE
12^+(-(/^ABC*DE
13F+(-(/^ABC*DEF
14)+(-ABC*DEF^/
15*+(-*ABC*DEF^/
16G+(-*ABC*DEF^/G
17)+ABC*DEF^/G*-
18*+*ABC*DEF^/G*-
19H+*ABC*DEF^/G*-H
end(pop all)(empty)ABC*DEF^/G*-H*+

Postfix: ABC*DEF^/G*-H*+

Evaluation

No values are given, so assume A = 2, B = 3, C = 4, D = 8, E = 2, F = 3, G = 5, H = 2. Operands are pushed; an operator pops the right operand first, then the left.

StepSymbolActionStack (bottom → top)
1Apush 22
2Bpush 32, 3
3Cpush 42, 3, 4
4*pop 4 (right), 3 (left); 3 * 4 = 12; push2, 12
5Dpush 82, 12, 8
6Epush 22, 12, 8, 2
7Fpush 32, 12, 8, 2, 3
8^pop 3 (right), 2 (left); 2 ^ 3 = 8; push2, 12, 8, 8
9/pop 8 (right), 8 (left); 8 / 8 = 1; push2, 12, 1
10Gpush 52, 12, 1, 5
11*pop 5 (right), 1 (left); 1 * 5 = 5; push2, 12, 5
12-pop 5 (right), 12 (left); 12 - 5 = 7; push2, 7
13Hpush 22, 7, 2
14*pop 2 (right), 7 (left); 7 * 2 = 14; push2, 14
15+pop 14 (right), 2 (left); 2 + 14 = 16; push16

Answer: 16 for the assumed values. Check in infix: E^F = 8, D/8 = 1, 1 × G = 5, B × C = 12, 12 − 5 = 7, 7 × H = 14, A + 14 = 16.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 2 questions →