Example 10: (a – b) * c – d
- 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 = *
- Read ‘c’ and add to output: Output = ab-c
- Read ‘-‘ and pop ‘*’ to output, push ‘-‘: Output = ab-c*, Stack = –
- Read ‘d’ and add to output: Output = ab-c*d
- Finish by popping operators from stack to output: Output = ab-c*d-
Final Result: ab-c*d-
Example 11: a / (b + c * d)
- Read ‘a’ and add to output: Output = a
- Read ‘/’ and push onto stack: Stack = /
- Read ‘(‘ and push onto stack: Stack = /, (
- Read ‘b’ and add to output: Output = ab
- Read ‘+’ and push onto stack: Stack = /, (, +
- Read ‘c’ and add to output: Output = abc
- Read ‘*’ and push onto stack: Stack = /, (, +, *
- Read ‘d’ and add to output: Output = abcd
- Read ‘)’ and pop operators until ‘(‘: Output = abcd*+, Stack = /
- Finish by popping operators from stack to output: Output = abcd*+/
Final Result: abcd*+/
Example 12: a ^ b * c + d
- Read ‘a’ and add to output: Output = a
- Read ‘^’ and push onto stack: Stack = ^
- Read ‘b’ and add to output: Output = ab
- Read ‘*’ and push onto stack: Stack = ^, *
- Read ‘c’ and add to output: Output = abc
- Read ‘+’ and pop ‘*’ and ‘^’ to output, push ‘+’: Output = abc*^, Stack = +
- Read ‘d’ and add to output: Output = abc*^d
- Finish by popping operators from stack to output: Output = abc*^d+
Final Result: abc*^d+
Example 13: (a + b) / (c – d * e)
- 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 second (c – d * e): Output = ab+cde*-
- Finish by popping operators from stack to output: Output = ab+cde*-/
Final Result: ab+cde*-/