OSDN Git Service

add arithmetic solver
authorTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 29 Jan 2010 10:46:41 +0000 (19:46 +0900)
committerTomohiro Nishimura <tomohiro68@gmail.com>
Fri, 29 Jan 2010 10:46:41 +0000 (19:46 +0900)
lib/mint/builder.rb
lib/mint/solver/engines/base.rb
lib/mint/solver/engines/maxima/arithmetic.rb [new file with mode: 0644]
spec/solver/engines/maxima/arithmetic_spec.rb [new file with mode: 0644]
spec/spec_helper.rb

index 79d3a8c..7e39cab 100644 (file)
@@ -12,8 +12,8 @@ module Mint
     class << self
       def build(expression)
         parser.parse(expression)
-      rescue
-        raise InvalidExpressionError
+      rescue
+        raise InvalidExpressionError
       end
 
       def parser
index 168783d..5646907 100644 (file)
@@ -5,7 +5,7 @@ module Mint::Solver::Engine
 
     def self.solve(problem)
       answer = solve_problem(problem)
-      Mint::Builder.build(answer)
+      Mint::Builder.build(problem)
     end
 
     private
diff --git a/lib/mint/solver/engines/maxima/arithmetic.rb b/lib/mint/solver/engines/maxima/arithmetic.rb
new file mode 100644 (file)
index 0000000..daa9bdd
--- /dev/null
@@ -0,0 +1,13 @@
+# -*- coding: utf-8 -*-
+
+module Mint::Solver::Engine::Maxima
+
+  class Arithmetic < Base
+
+    private
+    def self.solve_problem(problem)
+      maxima(problem)
+    end
+  end
+end
+
diff --git a/spec/solver/engines/maxima/arithmetic_spec.rb b/spec/solver/engines/maxima/arithmetic_spec.rb
new file mode 100644 (file)
index 0000000..3825f28
--- /dev/null
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+
+require File.dirname(__FILE__) + '/../../../spec_helper.rb'
+
+module Mint::Solver::Engine
+  describe Maxima::Arithmetic do
+    subject { Maxima::Arithmetic }
+
+    [
+      ['-92.92 - (-0.45)', '-92.47'],
+      ['0.18 * (-0.44)',   '-0.0792'],
+      ['91 - (-43)',       '134'],
+      ['15/44 + (-68/54)', '-1091 / 1188']
+    ].each do |exp, ans|
+      it exp do
+        result = subject.solve(exp)
+        answer = Mint::Builder.build(exp)
+        result.should == answer
+      end
+    end
+  end
+end
+
index 04df05f..6ce851d 100644 (file)
@@ -46,6 +46,7 @@ module ProblemExamples
       ['3 * 3', '3 * 3'],
       ['4 / 4', '4 / 4'],
       ['5 div 5', '5 div 5'],
+      ['-233/23', '-233 / 23'],
                        ['(x^2 + 4)(y^3 - 2)', '(x^2 + 4) * (y^3 - 2)'],
                        ['(x^3 + 2)(x^2- 3)', '(x^3 + 2) * (x^2 - 3)'],
       ['2(x^2 + xy + y^2)', '2 * (x^2 + x * y + y^2)'],