Permalink
Browse files

Add complex operations

  • Loading branch information...
1 parent 17fd5e9 commit 0faa12a3c5fc9be033955cb648803492d7900483 Rodrigo Flores committed Jun 19, 2012
Showing with 9 additions and 1 deletion.
  1. +5 −1 dojo-20120619/lib/rpn.ex
  2. +4 −0 dojo-20120619/test/rpn_test.exs
View
@@ -20,7 +20,11 @@ defmodule Rpn do
end
defp do_calc(['/'|tokens], [h|[j|stack]]) do
- do_calc(tokens, [j/h|stack])
+ do_calc(tokens, [j/h|stack])
+ end
+
+ defp do_calc(['^'|tokens], [h|[j|stack]]) do
+ do_calc(tokens, [:math.pow(h, j)|stack])
end
defp do_calc([h|tokens], stack) do
@@ -30,4 +30,8 @@ defmodule RpnTest do
test "solve with two non-consecutive operations" do
assert Rpn.calc("8 3 - 15 +") == 20
end
+
+ test "solve a complex expression" do
+ assert Rpn.calc("10 5 + 2 * 3 - 42 7 / + 3 3 ^ +") == 60
+ end
end

0 comments on commit 0faa12a

Please sign in to comment.