OSDN Git Service

move generate_problem into base class
[mint/mint-lib.git] / lib / mint / generator / complex_fractional_arithmetic.rb
1 # -*- coding: nil -*-
2
3 module Mint::Generator
4
5   #
6   # 繁分数の問題を生成するジェネレータ
7   #
8   # オプション ::
9   #               以下のオプションが使用出来る
10   #               [_term_number_] 生成する項の数 (ex. 1)
11   #               [_operators_] 使用する演算子 (ex. %w[ + - * ])
12   #               [_numerator_term_min_] 分母の項数の最小値 (ex. 1)
13   #               [_numerator_term_max_] 分母の項数の最大値 (ex. 1)
14   #               [_denominator_term_min_] 分子の項数の最小値 (ex. 2)
15   #               [_denominator_term_max_] 分子の項数の最大値 (ex. 2)
16   #
17   class ComplexFractionalArithmetic < Arithmetic
18
19     private
20
21     option :numerator_term_min,   1
22     option :numerator_term_max,   1
23     option :denominator_term_min, 2
24     option :denominator_term_max, 2
25     option :operators,            %w[ + - * ]
26
27     # for inner use
28     option :numerator_min,   1
29     option :numerator_max,   9
30     option :denominator_min, 1
31     option :denominator_max, 9
32     option :minus,           false
33
34     def expression
35       result = []
36       result << numerator
37       result << "(#{denominator})"
38       result.join(' / ')
39     end
40
41     def operand
42       opts = { :min => 1, :max => 9, :minus => false }
43       do_with(opts) { integer }
44     end
45
46     [:numerator, :denominator].each do |name|
47       define_method(name) do
48         result = []
49         term_number(name).times do
50           result << fraction
51           result << options[:operators].sample
52         end
53         result[0..-2].join(' ')
54       end
55     end
56
57     def term_number(position)
58       min = options[:"#{position}_term_min"]
59       max = [min, options[:"#{position}_term_max"]].max
60       (min..max).to_a.sample
61     end
62   end
63 end
64