120 likes | 739 Views
Reverse Polish Notation. Post-fix / Post-order. *. +. -. A. B. C. D. Doesn’t need brackets Completely unambiguous Simple to code using stack model HP Calculator. Infix vs Postfix. Match-o-matic. AB+C-D*E/F*G+. A+B/C*D-E*(F+G). AB*C*D+E-FG/+. A-B+C/D*(E+F)*G. AB/CD*+E-FG*+.
E N D
Post-fix / Post-order * + - A B C D
Doesn’t need brackets Completely unambiguous Simple to code using stack model HP Calculator Infix vs Postfix
Match-o-matic AB+C-D*E/F*G+ A+B/C*D-E*(F+G) AB*C*D+E-FG/+ A-B+C/D*(E+F)*G AB/CD*+E-FG*+ A/B+C*D-E+F*G AB-CD/EF+*G*+ (A+B-C)*D/E*F+G AB+CD*+EF*G/- A*B*C+D-E+F/G (A+B+C*D)-(E*F/G) ABC/D*+EFG+*-
Challenge • Write rules (pseudocode) for converting postfix to prefix
Infix to Postfix conversion (manual) • An Infix to Postfix manual conversion algorithm is: 1. Completely parenthesize the infix expression according to order of priority you want. 2. Move each operator to its corresponding right parenthesis. 3. Remove all parentheses. • Examples: 3 + 4 * 5 (3 + (4 * 5) ) 3 4 5 * + a / b ^ c – d * e – a * c ^ 3 ^ 4 a b c ^ / d e * a c 3 4 ^ ^ * - - ((a / (b ^ c)) – ((d * e) – (a * (c ^ (3 ^ 4) ) ) ) ) Using normal mathematical operator precedence Not using normal mathematical operator precedence
Infix to Prefix conversion (manual) • An Infix to Postfix manual conversion algorithm is: 1 Completely parenthesize the infix expression according to order of priority you want. 2 Move each operator to its corresponding left parenthesis. 3 Remove all parentheses. • Examples: 3 + 4 * 5 (3 + (4 * 5) ) 3 4 5 * + a / b ^ c – d * e – a * c ^ 3 ^ 4 a b c ^ / d e * a c 3 4 ^ ^ * - - ( (a / (b ^ c)) – ( (d * e) – (a * (c ^ (3 ^ 4) ) ) ) ) Using normal mathematical operator precedence Not using normal mathematical operator precedence