infix to postfix-7

Example 26: ((a + b) ^ c – d * e / f) + g * h

  1. Read ‘(‘ and push onto stack: Stack = (
  2. Repeat steps for inside expression (a + b): Output = ab+
  3. Read ‘^’ and push onto stack: Stack = (, ^
  4. Read ‘c’ and add to output: Output = ab+c
  5. Read ‘-‘ and push onto stack: Stack = (, –
  6. Read ‘d’ and add to output: Output = ab+c^d
  7. Read ‘*’ and push onto stack: Stack = (, -, *
  8. Read ‘e’ and add to output: Output = ab+c^de
  9. Read ‘/’ and push onto stack: Stack = (, -, /
  10. Read ‘f’ and add to output: Output = ab+c^de*f
  11. Read ‘)’ and pop operators until ‘(‘: Output = ab+c^de*f/-, Stack =
  12. Read ‘+’ and push onto stack: Stack = +
  13. Read ‘g’ and add to output: Output = ab+c^de*f/-g
  14. Read ‘*’ and push onto stack: Stack = +, *
  15. Read ‘h’ and add to output: Output = ab+c^de*f/-gh
  16. 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))

  1. Read ‘x’ and add to output: Output = x
  2. Read ‘*’ and push onto stack: Stack = *
  3. Read ‘y’ and add to output: Output = xy
  4. Read ‘+’ and push onto stack: Stack = *, +
  5. Read ‘(‘ and push onto stack: Stack = *, +, (
  6. Read ‘a’ and add to output: Output = xya
  7. Read ‘-‘ and push onto stack: Stack = *, +, (, –
  8. Read ‘b’ and add to output: Output = xyab
  9. Read ‘/’ and push onto stack: Stack = *, +, (, –
  10. Read ‘(‘ and push onto stack: Stack = *, +, (, -, (
  11. Read ‘c’ and add to output: Output = xyabc
  12. Read ‘+’ and push onto stack: Stack = *, +, (, -, (, +
  13. Read ‘d’ and add to output: Output = xyabcd
  14. Read ‘^’ and push onto stack: Stack = *, +, (, -, (, +, ^
  15. Read ‘e’ and add to output: Output = xyabcde
  16. Pop operators until ‘(‘, twice: Output = xyabcde^+/, Stack = *, +, (, –
  17. Read ‘)’ and pop operators until ‘(‘: Output = xyabcde^+/–
  18. 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)

  1. Read ‘(‘ and push onto stack: Stack = (
  2. Repeat steps for inside expression (w – x / y * z + a ^ b): Output = wx/yz*-ab^+
  3. Read ‘)’ and pop operators until ‘(‘: Output = wx/yz*-ab^+, Stack =
  4. Read ‘*’ and push onto stack: Stack = *
  5. Read ‘(‘ and push onto stack: Stack = *, (
  6. Repeat steps for inside expression (c / d – e * f): Output = wx/yz*-ab^+cdf/e*f-
  7. Read ‘)’ and pop operators until ‘(‘: Output = wx/yz*-ab^+cdf/e*f-*
  8. Finish by popping operators from stack to output: Output = wx/yz*-ab^+cdf/e*f-*

Final Result: wx/yz*-ab^+cdf/e*f-*

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top