2015-11-18 06:14:39 +00:00
|
|
|
calc[rpn_] :=
|
2016-12-05 22:15:40 +01:00
|
|
|
Module[{tokens = StringSplit[rpn], s = "(" <> ToString@InputForm@# <> ")" &, op, steps},
|
|
|
|
|
op[o_, x_, y_] := ToExpression[s@x <> o <> s@y];
|
|
|
|
|
steps = FoldList[Switch[#2, _?DigitQ, Append[#, FromDigits[#2]],
|
|
|
|
|
_, Append[#[[;; -3]], op[#2, #[[-2]], #[[-1]]]]
|
|
|
|
|
] &, {}, tokens][[2 ;;]];
|
|
|
|
|
Grid[Transpose[{# <> ":" & /@ tokens,
|
2015-11-18 06:14:39 +00:00
|
|
|
StringRiffle[ToString[#, InputForm] & /@ #] & /@ steps}]]];
|
|
|
|
|
Print[calc["3 4 2 * 1 5 - 2 3 ^ ^ / +"]];
|