1 # -*- coding: utf-8 -*-
5 class ExpressionNode # :nodoc:
6 attr_reader :left, :right, :operator
7 attr_accessor :minus, :parenthesis
8 def initialize(left, right)
24 result.parenthesis = false
29 "#{left} #{operator} #{right}"
32 "#{left.to_latex} #{operator} #{right.to_latex}"
35 return "(-(#{string}))" if minus
36 return "(#{string})" if parenthesis
41 class AdditionNode < ExpressionNode # :nodoc:
42 def initialize(left, right)
48 class SubtractionNode < ExpressionNode # :nodoc:
49 def initialize(left, right)
54 "#{left} #{operator} #{right}"
58 class MultipleNode < ExpressionNode # :nodoc:
59 def initialize(left, right)
65 if right.instance_of?(LiteralNode) ||
66 right.instance_of?(RootNode) ||
67 right.instance_of?(FactorialNode)
69 unless right.to_s.match(/\A[-\.\d]+\z/)
70 return "#{left.to_latex}#{right.to_latex}"
73 "#{left.to_latex} \\times #{right.to_latex}"
77 class DivisionNode < ExpressionNode # :nodoc:
78 def initialize(left, right)
83 "#{left.to_latex} \\div #{right.naked.to_latex}"
87 class FractionNode < ExpressionNode # :nodoc:
88 def initialize(left, right)
93 "\\frac{#{left.to_latex}}{#{right.naked.to_latex}}"
97 class LiteralNode # :nodoc:
98 attr_accessor :value, :minus, :parenthesis
113 result.parenthesis = false
124 return "-#{string}" if minus
125 return "(#{string})" if parenthesis
126 return "(-#{string})" if minus && parenthesis
131 class DecimalNode < LiteralNode # :nodoc:
133 def initialize(value)
137 round_n(value.to_f, 3).to_s
141 def round_n(num, nth)
142 result = num * (10 ** nth)
143 (result.round() * (10 ** -nth)).to_f
147 class FactorialNode < LiteralNode # :nodoc:
148 attr_reader :value, :power
149 def initialize(value, power)
157 return to_s if value.instance_of?(String)
158 "#{value.to_latex}^#{power.to_latex}"
162 class RootNode < LiteralNode # :nodoc: