Results 1 to 2 of 2
  1. Default Reverse Polish Notation


    A tutorial that my teacher linked shows 9 7 - 2 / being invalid, but I don't see how it is.
    My thought process:
    After 9, 7 is read in, stack is:
    7
    9

    - is read in, 9 and 7 is popped off, and 9 - 7 is pushed in:
    2

    The 2 after - is read in and pushed in:
    2
    2

    / is read in, leading to both 2's being popped off and 2/2 to be pushed in:
    1

    The tutorial shows that right after 9 -7 is calculated, two numbers are attempted to be popped off the stack, leading to an error because the stack currently only has 1 number in it. Am I doing something wrong?

  2. Default Re: Reverse Polish Notation


    It can also be 2 9 7 - / for it to work.

    If you want to visualize it:

    InputOperationStack
    2Push Back[2]
    9Push Back[2,9]
    7Push Back[2,9,7]
    -Subtract[2,(9-7)]
    -Push Result[2,2]
    /Divide[(2/2)]
    /Push Result[1]

    Likewise, I think yours is still correct.

    InputOperationStack
    9Push Back[9]
    7Push Back[9,7]
    -Subtract[9-7]
    -Push Result[2]
    2Push Back[2,2]
    /Divide[2/2]
    /Push Result[1]

  3.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •