Past exam question: 2066 Bhadra (Regular / Back), Q2
Open in the Padhaiko appQuestion
Convert the following expression to postfix and prefix: ((A+B)*C-(D-E))\$(F+G)
Solution
1. Conversion to Postfix (((A+B)*C-(D-E))\$(F+G)): Sub-expression breakdown & stack precedence (\$ has higher precedence than *, /, which have higher precedence than +, -):
(A+B)AB+(A+B)*CAB+C*(D-E)DE-((A+B)*C-(D-E))AB+C*DE--(F+G)FG+- Combining across
\$:AB+C*DE--FG+\$.
2. Conversion to Prefix: Reversing the infix expression ((G+F)\$((E-D)-C*(B+A))), converting to postfix, and reversing back:
(A+B)+AB(A+B)*C*+ABC(D-E)-DE((A+B)*C-(D-E))-*+ABC-DE(F+G)+FG- Combining across
\$:\$-*+ABC-DE+FG.
Answer:
- Postfix:
AB+C*DE--FG+\$ - Prefix:
\$-*+ABC-DE+FG