Ангел обнови решението на 14.11.2012 16:56 (преди около 12 години)
+class Expr
+ def Expr.build(expr)
+ case expr.first
+ when :number then Number.new expr[1]
+ when :variable then Variable.new expr[1]
+ when :+ then Addition.new expr[1..2]
+ when :* then Multiplication.new expr[1..2]
+ when :- then Subtraction.new expr[1..2]
+ when :sin then Sine.new expr[1]
+ when :cos then Cosine.new expr[1]
+ end
+ end
+ def ==(other)
+ other.is_a? self.class and equal_operands? other
+ end
+end
+
+class Unary < Expr
+ def initialize(operand)
+ @operand = Expr.build operand
+ end
+ attr_reader :operand
+ def equal_operands?(other)
+ self.operand == other.operand
+ end
+end
+
+class Binary < Expr
+ def initialize(operands)
+ @operands = operands.map { |operand| Expr.build operand }
+ end
+ attr_reader :operands
+ def equal_operands?(other)
+ self.operands == other.operands
+ end
+end
+
+class Number < Unary
+ def initialize(number)
+ @operand = number
+ end
+end
+
+class Addition < Binary
+end
+
+class Multiplication < Binary
+end
+
+class Variable < Unary
+ def initialize(variable)
+ @operand = variable
+ end
+end
+
+class Subtraction < Binary
+end
+
+class Sine < Unary
+end
+
+class Cosine < Unary
+end