1. ## 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. 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. 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. 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. I was not looking for this ones! I am trying to understand it... Anyhow, thanks!

6. 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. 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.

8. 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...

Reverse Polish notation - Wikipedia, the free encyclopedia

Lots of info there.

10. 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. 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?

12. 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. But technically, can these operations be viewed as a tree?

14. Originally Posted by Overworked_PhD
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. Okay, that was sloppy wording on my part.