Example 22: (a + b) * (c / d – e ^ f) / g
- Read ‘(‘ and push onto stack: Stack = (
- Read ‘a’ and add to output: Output = a
- Read ‘+’ and push onto stack: Stack = (, +
- Read ‘b’ and add to output: Output = ab
- Read ‘)’ and pop operators until ‘(‘: Output = ab+, Stack =
- Read ‘*’ and push onto stack: Stack = *
- Repeat steps 1-5 for inside expression (c / d – e ^ f): Output = ab+cd/e^f-*
- Read ‘/’ and push onto stack: Stack = /
- Read ‘g’ and add to output: Output = ab+cd/e^f-*g
- Finish by popping operators from stack to output: Output = ab+cd/e^f-*g/
Final Result: ab+cd/e^f-*g/
Example 23: m ^ (n * o / (p + q – r))
- Read ‘m’ and add to output: Output = m
- Read ‘^’ and push onto stack: Stack = ^
- Read ‘(‘ and push onto stack: Stack = ^, (
- Read ‘n’ and add to output: Output = mn
- Read ‘*’ and push onto stack: Stack = ^, (, *
- Read ‘o’ and add to output: Output = mno
- Read ‘/’ and push onto stack: Stack = ^, (, /
- Read ‘(‘ and push onto stack: Stack = ^, (, /, (
- Read ‘p’ and add to output: Output =mnop
- Read ‘+’ and push onto stack: Stack = ^, (, /, (, +
- Read ‘q’ and add to output: Output =mnopq
- Read ‘-‘ and push onto stack: Stack = ^, (, /, (, –
- Read ‘r’ and add to output: Output =mnopqr
- Pop operators until ‘(‘, twice: Output = mnopq+r-/, Stack = ^
- Finish by popping operators from stack to output: Output = mnopq+r-/^
Final Result: mnopq+r-/^
Example 24: (a + b ^ c) * (d – e / f) / (g + h)
- Repeat steps for inside expression (a + b ^ c): Output = abc^+
- Read ‘*’ and push onto stack: Stack = *
- Repeat steps for inside expression (d – e / f): Output = abc^+def/f-
- Read ‘*’ from stack and add to output: Output = abc^+def/f-*
- Read ‘/’ and push onto stack: Stack = /
- Read ‘(‘ and push onto stack: Stack = /, (
- Read ‘g’ and add to output: Output = abc^+def/f-*g
- Read ‘+’ and push onto stack: Stack = /, (, +
- Read ‘h’ and add to output: Output = abc^+def/f-*gh
- Read ‘)’ and pop operators until ‘(‘: Output = abc^+def/f-*gh+/, Stack =
- Finish by popping operators from stack to output: Output = abc^+def/f-*gh+/
Final Result: abc^+def/f-*gh+/
Example 25: (x / y + z) ^ (a – b * c) / d
- Repeat steps for inside expression (x / y + z): Output = xy/z+
- Read ‘^’ and push onto stack: Stack = ^
- Repeat steps for inside expression (a – b * c): Output = xy/z+abc*-^
- Read ‘/’ and push onto stack: Stack = /
- Read ‘d’ and add to output: Output = xy/z+abc*-^d
- Finish by popping operators from stack to output: Output = xy/z+abc*-^d/
Final Result: xy/z+abc*-^d/