Example 26: ((a + b) ^ c – d * e / f) + g * h
- Read ‘(‘ and push onto stack: Stack = (
- Repeat steps for inside expression (a + b): Output = ab+
- Read ‘^’ and push onto stack: Stack = (, ^
- Read ‘c’ and add to output: Output = ab+c
- Read ‘-‘ and push onto stack: Stack = (, –
- Read ‘d’ and add to output: Output = ab+c^d
- Read ‘*’ and push onto stack: Stack = (, -, *
- Read ‘e’ and add to output: Output = ab+c^de
- Read ‘/’ and push onto stack: Stack = (, -, /
- Read ‘f’ and add to output: Output = ab+c^de*f
- Read ‘)’ and pop operators until ‘(‘: Output = ab+c^de*f/-, Stack =
- Read ‘+’ and push onto stack: Stack = +
- Read ‘g’ and add to output: Output = ab+c^de*f/-g
- Read ‘*’ and push onto stack: Stack = +, *
- Read ‘h’ and add to output: Output = ab+c^de*f/-gh
- Finish by popping operators from stack to output: Output = ab+c^de*f/-gh*+
Final Result: ab+c^de*f/-gh*+
Example 27: x * y + (a – b / (c + d ^ e))
- Read ‘x’ and add to output: Output = x
- Read ‘*’ and push onto stack: Stack = *
- Read ‘y’ and add to output: Output = xy
- Read ‘+’ and push onto stack: Stack = *, +
- Read ‘(‘ and push onto stack: Stack = *, +, (
- Read ‘a’ and add to output: Output = xya
- Read ‘-‘ and push onto stack: Stack = *, +, (, –
- Read ‘b’ and add to output: Output = xyab
- Read ‘/’ and push onto stack: Stack = *, +, (, –
- Read ‘(‘ and push onto stack: Stack = *, +, (, -, (
- Read ‘c’ and add to output: Output = xyabc
- Read ‘+’ and push onto stack: Stack = *, +, (, -, (, +
- Read ‘d’ and add to output: Output = xyabcd
- Read ‘^’ and push onto stack: Stack = *, +, (, -, (, +, ^
- Read ‘e’ and add to output: Output = xyabcde
- Pop operators until ‘(‘, twice: Output = xyabcde^+/, Stack = *, +, (, –
- Read ‘)’ and pop operators until ‘(‘: Output = xyabcde^+/–
- Finish by popping operators from stack to output: Output = xyabcde^+/–*+
Final Result: xyabcde^+/–*+
Example 28: (w – x / y * z + a ^ b) * (c / d – e * f)
- Read ‘(‘ and push onto stack: Stack = (
- Repeat steps for inside expression (w – x / y * z + a ^ b): Output = wx/yz*-ab^+
- Read ‘)’ and pop operators until ‘(‘: Output = wx/yz*-ab^+, Stack =
- Read ‘*’ and push onto stack: Stack = *
- Read ‘(‘ and push onto stack: Stack = *, (
- Repeat steps for inside expression (c / d – e * f): Output = wx/yz*-ab^+cdf/e*f-
- Read ‘)’ and pop operators until ‘(‘: Output = wx/yz*-ab^+cdf/e*f-*
- Finish by popping operators from stack to output: Output = wx/yz*-ab^+cdf/e*f-*
Final Result: wx/yz*-ab^+cdf/e*f-*