[Tutor] Simple RPN calculator

Orri Ganel singingxduck at gmail.com
Sun Dec 5 09:12:13 CET 2004


Hello all,

>         Well, the best way to explain is to take an example. Let's say you
> want to calculate ((2 + 5) * 3 - (4 / 6)) * ((8 - 2 * 3) / 9 + (10 -
> 1))

<SNIP>

> In other words, if you were
> using a HP48, here's what you'd be inputting:
> 
> 2
> 5
> +
> 3
> *
> 4
> 6
> /
> 8
> 2
> 3
> *
> -
> 9
> /
> 10
> 1
> -
> +
> *

Just to clarify, that would actually be

2
5
+
3
*
4
6
/
- <<
8
2
3
*
-
9
/
10
1
-
+
*

, correct? (There should be a minus after "4 6 /") Also, in order to
find the answer it looks like you want (in Python), you should do

((2 + 5) * 3 - (4.0 / 6)) * ((8 - 2 * 3) / 9.0 + (10 - 1))

and, if your RPN doesn't handle numbers as floats or Decimals (new as
of Python 2.3 I believe),

2
5
+
3
*
4.0
6
/
-
8
2
3
*
-
9.0
/
10
1
-
+
*

Otherwise you get 189 because (4 / 6) evaluates to 0, as does ((8 - 2
* 3) / 9).  By the way, anyone interested in seeing/reviewing an RPN
calculator I wrote, feel free to visit
http://rafb.net/paste/results/BW1hxH51.html (57 lines)


More information about the Tutor mailing list