10 # 生成する式の項数を1以上の整数で指定します。
13 # *, / の2種類から使用したいものを配列で指定します。
15 # 生成する式の各項の最小値を0以上の整数で指定します。
17 # 生成する式の各項の最大値を0以上の整数で指定します。
18 # _min_ よりも小さい値を指定することは出来ません。
20 # 真を指定すると分数形式の項を生成します。
22 class ComplexNumberArithmetic < Arithmetic
28 option :fractional_mode, false
29 option :operators, %w[ * / ]
32 if options[:fractional_mode]
33 @options[:operators] = %w[ + - ]
39 if options[:fractional_mode]
40 create_fractional_complex_number
44 return "#{result}^2" if options[:term_number] == 1
45 return conjugate_complex(last_operand) if last_operator == '/'
49 def create_complex_number
50 real_part = create_integer(options[:min], options[:max], false)
51 imaginary_part = create_integer(options[:min], options[:max], false)
52 return (2 + rand(8)).to_s if [real_part, imaginary_part].include? 0
53 operator = %w[ + - ].sample
54 "(#{real_part} #{operator} #{imaginary_part.to_s.sub('1', '')}%i)"
57 def create_fractional_complex_number
58 numerator_part = 1 + rand(9)
59 denominator_part = create_complex_number
60 "#{numerator_part} / #{denominator_part}"
63 def conjugate_complex(expression)
64 a, op, b = *expression.split
65 op = op == '+' ? '-' : '+'