Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Reversh polish calculator behaviour

Jack Higgs
Greenhorn
Posts: 5
Ok, it's a reverse polish calculator I'm analysing.

When I input:

1234
2345
3456
d
+
d
+
d
=

I get an output:

1234
2345
3456
d
1234
2345
3456
+
d
1234
5801
+
d
7035
=
7035

I'm unsure as to what the behaviour of d is. I need to be able to program what d does to do this question on a past exam paper (I'm revising). Any ideas how to represent what d does?

Cheers

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Are you comforatable with how RPN works? Do this sequence with pencil and paper; write out the stack after each operation and see if the meaning of "d" jumps out at you.

Jack Higgs
Greenhorn
Posts: 5
I can see what it's doing (sort of!), I just don't know how to express it mathematically or as an algorithm.

Joel Jorgensen
Greenhorn
Posts: 22
When I am trying to figure out how to express something algorithmically, I often find it helps for me to write out what I think it does in language that I am most familiar with (sort of like pseudo-code) with gaps to be filled in later for the parts that I'm not sure about.

You say that you can sort of see what d is doing--what is it that you can see, and where are the gaps that you can't see yet?

Jim Yingst
Wanderer
Sheriff
Posts: 18671
What happens if you repeat the "d" two or three times in a row? Does this have any effect on subsequent results? Or, what if you omit some (or all) of the "d" commands? I would suggest that what the "d" does is fairly simple, but much of the interesting action here occurs on "+" instead.

I'm also a bit mystified about why an RPN calculator would need a "=" at all. But that's mostly a side issue at the moment, I think.

 It is sorta covered in the JavaRanch Style Guide.