Thread: polish calculator

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    polish calculator

    Dear all,

    I would like kindly to ask you if someone has the source code (please let me also know how to run it) of the K&R example as I am not able to understand how I will insert as inputs i.e. 1 2 - 4 5 + * means -> (1 - 2) * (4 + 5) and how all this functions which are quite spread out at K&R will work out?

    Hope you understand my problem! If now please let me know and I will be back to you.

    P.S. I would really like to understand this example (seems is quite important)

    Thank You!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm confused, but that's because I don't have K&R. If it's an example, then surely the source code is in the book? If it's an exercise, then I would guess the point is to work it out.

    (Also: this is not a polish calculator, since a polish calculator would be * + 4 5 - 1 2. What we have here is reverse polish.)

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    OK it is a reverse polish... do you have any idea where I can find a source code about it? If you have could you share?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    A cut 'n' paste from one of these examples might save you some finger time. But it's often easier to learn if you have to type it all yourself ;-)

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    I was not looking for this ones! I am trying to understand it... Anyhow, thanks!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So now I'm more confused. If the source code is in the book, with comments, as the badger's link indicates, then why are you asking for source code? If you have questions about the source code that you're reading, then ask them.

  7. #7
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    I think I have the complete source code to the reverse polish calculator found in K&R. Later on today, when I'm at school, I'll check to see if I still have the solution on the SUN machines. Anyways, if I remember correctly, the K&R reverse polish calculator is similar to converting a recursive inorder traversal of a binary tree to an iterative one.
    Last edited by Overworked_PhD; 04-09-2010 at 10:30 AM.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    The full K&R source is linked to from the page I linked earlier, which is why I -- like tabstop -- am confused about what the OP was after...

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Would this be helpful?

    Reverse Polish notation - Wikipedia, the free encyclopedia

    Lots of info there.

  10. #10
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    1 2 - 4 5 + * means -> (1 - 2) * (4 + 5)
    1 gets pushed to stack

    stack: 1

    2 gets pushed to stack

    stack: 1 2

    -

    1 - 2 = -1

    -1 gets pushed to stack
    stack : -1

    4

    push 4

    stack: -1 4

    5

    push 5

    stack: -1 4 5

    +

    do operation

    stack: -1 9

    *

    stack: -9
    he K&R reverse polish calculator is similar to converting a recursive inorder traversal of a binary tree to an iterative one.
    what on earth do binary trees have to do with this?

  11. #11
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    I don't have the code with my on hand, but I think the push and pop operations to calculate reverse polish notations would be similar to doing push and pop operations on some kind of traversal iterative binary tree.

    For example, if my input is

    3 4 +

    This could be viewed as the following tree
    +
    / \
    3 4

    Now what if I would do some kind of iterative traversal on this tree? Wouldn't this just boil down to a series of push and pop operations?
    Last edited by Overworked_PhD; 04-09-2010 at 03:28 PM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm worried that you actually think "traversal iterative binary tree" is an actual phrase, with an actual meaning.

    But: RPN is the postorder traversal of the expression tree, so you could build the expression tree back from the list of symbols (if, for instance, you wanted to rewrite it in Polish/prefix or standard/infix forms). But (just) to calculate a result, building the tree is not the way to go.

  13. #13
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    But technically, can these operations be viewed as a tree?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Overworked_PhD View Post
    But technically, can these operations be viewed as a tree?
    You can represent the calculation that the user wants to perform as an expression tree. You cannot represent "push, push, pop, add, etc." as a tree.

  15. #15
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Okay, that was sloppy wording on my part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse polish calculator
    By Tool in forum C Programming
    Replies: 3
    Last Post: 12-30-2009, 04:46 PM
  2. pushback question (reverse polish calculator)
    By Tool in forum C Programming
    Replies: 8
    Last Post: 12-29-2009, 02:29 PM
  3. reverse polish calculator
    By Tool in forum C Programming
    Replies: 1
    Last Post: 12-24-2009, 05:25 PM
  4. Reverse Polish Notation Calculator
    By jazzyqueen in forum C Programming
    Replies: 1
    Last Post: 09-02-2009, 03:55 AM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 09:31 AM