Postfix%20notation - PowerPoint PPT Presentation

About This Presentation
Title:

Postfix%20notation

Description:

Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we ... calculators, such as expensive ones made by HP, use RPN (Reverse Polish Notation) ... – PowerPoint PPT presentation

Number of Views:501
Avg rating:3.0/5.0
Slides: 11
Provided by: davidlm3
Category:

less

Transcript and Presenter's Notes

Title: Postfix%20notation


1
Postfix notation
2
About postfix notation
  • Postfix, or Reverse Polish Notation (RPN) is an
    alternative to the way we usually write
    arithmetic expressions (which is called infix, or
    algebraic notation
  • Postfix refers to the fact that the operator is
    at the end
  • Infix refers to the fact that the operator is
    in between
  • For example, 2 2 postfix is the same as 2 2
    infix
  • There is also a seldom-used prefix notation,
    similar to postfix
  • Advantages of postfix
  • You dont need rules of precedence
  • You dont need rules for right and left
    associativity
  • You dont need parentheses to override the above
    rules
  • Advantages of infix
  • You grew up with it
  • Its easier to see visually what is done first

3
How to evaluate postfix
  • Going from left to right, if you see an operator,
    apply it to the previous two operands (numbers)
  • Example

2 10 4 5 / 9 3
- -
  • Equivalent infix 2 10 4 / 5 (9 3)

4
Computer evaluation of postfix
  • Going left to right,
  • If you see a number, put it on the stack
  • If you see an operator, pop two numbers from the
    stack, do the operation, and put the result back
    on the stack
  • (The top number on the stack is the operand on
    the right)

2 10 4 5 / 9 3
- -
  • The result is the only thing on the stack when
    done
  • If theres more than one thing, there was an
    error in the expression

5
A helpful observation
  • When you convert between infix and postfix, the
    operands (numbers) are always in the same order
  • The operators are probably in a different order
  • Were going to talk about how to convert manually
    between the two systems
  • Your textbook has more information on how to do
    the conversion by computer

6
From infix to postfix
  • Figure out, using the infix rules, which
    operation to perform next
  • Write the new operand or operands in their
    correct places
  • Write the operator at the end
  • Postfix does not use parentheses, but well put
    them in temporarily to help show the way things
    are grouped
  • Example 2 10 4 / 5 (9 3)
  • The multiply is done first 10 4
  • Next, the divide (10 4 ) 5 /
  • The addition 2 (10 4
    5 /)
  • The rightmost subtraction (2 10 4 5 / )
    9 3 -
  • The leftmost subtraction (2 10 4 5 / )
    (9 3 -) -
  • The final result 2 10 4 5 /
    9 3 - -

7
From postfix to infix
  • Why would you want to?
  • OK--working from left to right, for each
    operator, move it between the two preceding
    operands, and put parenthesis around the whole
    group
  • Example
  • 2 10 4 5 / 9 3 - -
  • 2 (10 4) 5 / 9 3 - -
  • 2 ((10 4) / 5) 9 3 - -
  • (2 ((10 4) / 5) 9 3 - -
  • (2 ((10 4) / 5) (9 - 3) -
  • ((2 ((10 4) / 5) - (9 - 3))
  • Now you can remove unnecessary parentheses (if
    you want to)

8
Unary operators
  • In infix, - means both negation (as a unary
    operator) and subtraction (as a binary
    operator)
  • Likewise, means both addition and identity
  • In infix notation, an operator is unary if
  • It is the very first thing in the expression, or
  • It immediately follows a left (opening)
    parenthesis
  • We have no such clues in postfix notation
  • The usual solution is simply to use a different
    symbol, such as , for unary negation

9
Hewlett-Packard calculators
  • Some of the finest calculators, such as expensive
    ones made by HP, use RPN (Reverse Polish
    Notation)
  • Its actually quite easy to get used to using RPN
  • RPN more closely mirrors the way you think about
    doing a calculation
  • With an algebraic calculator, its easier to just
    copy an equation from a textbook into a
    calculator
  • With just a little practice, entering an
    algebraic formula into an RPN calculator becomes
    very easy (and takes fewer keystrokes)
  • Despite the advantages, RPN has a difficult time
    in the marketplace
  • People in general dont like to learn new things
    if they dont have to
  • ...and this isnt necessarily a bad thing

10
The End
Write a Comment
User Comments (0)
About PowerShow.com