Example 6: (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 = /
- Repeat steps 1-5 for second (c * d): Output = ab-cd*, Stack = /
- Finish by popping operators from stack to output: Output = ab-cd*/
Final Result: ab-cd*/
Example 7: 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 pop ‘/’ to output, push ‘+’: Output = ab/, 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 8: (a + b) ^ c
- 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
- Finish by popping operators from stack to output: Output = ab+c^
Final Result: ab+c^
Example 9: 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 pop operators until ‘(‘: Output = abc+, Stack = *
- Read ‘/’ and pop ‘*’ 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/