infix to postfix-6

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

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

  1. Read ‘m’ and add to output: Output = m
  2. Read ‘^’ and push onto stack: Stack = ^
  3. Read ‘(‘ and push onto stack: Stack = ^, (
  4. Read ‘n’ and add to output: Output = mn
  5. Read ‘*’ and push onto stack: Stack = ^, (, *
  6. Read ‘o’ and add to output: Output = mno
  7. Read ‘/’ and push onto stack: Stack = ^, (, /
  8. Read ‘(‘ and push onto stack: Stack = ^, (, /, (
  9. Read ‘p’ and add to output: Output =mnop
  10. Read ‘+’ and push onto stack: Stack = ^, (, /, (, +
  11. Read ‘q’ and add to output: Output =mnopq
  12. Read ‘-‘ and push onto stack: Stack = ^, (, /, (, –
  13. Read ‘r’ and add to output: Output =mnopqr
  14. Pop operators until ‘(‘, twice: Output = mnopq+r-/, Stack = ^
  15. 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)

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

  1. Repeat steps for inside expression (x / y + z): Output = xy/z+
  2. Read ‘^’ and push onto stack: Stack = ^
  3. Repeat steps for inside expression (a – b * c): Output = xy/z+abc*-^
  4. Read ‘/’ and push onto stack: Stack = /
  5. Read ‘d’ and add to output: Output = xy/z+abc*-^d
  6. Finish by popping operators from stack to output: Output = xy/z+abc*-^d/

Final Result: xy/z+abc*-^d/

Leave a Comment

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

Scroll to Top