diff --git a/poincare/Makefile b/poincare/Makefile index 2c76c3b86..1c0a3dca1 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -1,6 +1,7 @@ SFLAGS += -Ipoincare/include objs += $(addprefix poincare/src/,\ addition.o\ + commutative_operation.o\ context.o\ expression.o\ float.o\ diff --git a/poincare/include/poincare/addition.h b/poincare/include/poincare/addition.h index e97d49f48..6eb1c9577 100644 --- a/poincare/include/poincare/addition.h +++ b/poincare/include/poincare/addition.h @@ -1,18 +1,14 @@ #ifndef POINCARE_ADDITION_H #define POINCARE_ADDITION_H -#include +#include -class Addition : public Expression { +class Addition : public CommutativeOperation { + using CommutativeOperation::CommutativeOperation; public: - Addition(Expression * first_operand, Expression * second_operand); - ~Addition(); ExpressionLayout * createLayout(ExpressionLayout * parent) override; - float approximate(Context& context) override; Type type() override; - private: - Expression * m_left; - Expression * m_right; + float operateApproximatevelyOn(float a, float b) override; }; #endif diff --git a/poincare/include/poincare/commutative_operation.h b/poincare/include/poincare/commutative_operation.h new file mode 100644 index 000000000..563ff3e18 --- /dev/null +++ b/poincare/include/poincare/commutative_operation.h @@ -0,0 +1,20 @@ +#ifndef POINCARE_COMMUTATIVE_OPERATION_H +#define POINCARE_COMMUTATIVE_OPERATION_H + +#include + +class CommutativeOperation : public Expression { + public: + CommutativeOperation(Expression * first_operand, Expression * second_operand); + ~CommutativeOperation(); + Expression * operand(int i); + int numberOfOperands(); + float approximate(Context& context) override; + protected: + virtual float operateApproximatevelyOn(float a, float b) = 0; + private: + int m_numberOfOperands; + Expression ** m_operands; +}; + +#endif diff --git a/poincare/include/poincare/product.h b/poincare/include/poincare/product.h index 557497256..76f3c192c 100644 --- a/poincare/include/poincare/product.h +++ b/poincare/include/poincare/product.h @@ -1,21 +1,14 @@ #ifndef POINCARE_PRODUCT_H #define POINCARE_PRODUCT_H -#include +#include -class Product : public Expression { +class Product : public CommutativeOperation { + using CommutativeOperation::CommutativeOperation; public: - Product(Expression * first_factor, Expression * second_factor); - ~Product(); ExpressionLayout * createLayout(ExpressionLayout * parent) override; - float approximate(Context& context) override; Type type() override; - - Expression * factor(int i); - int numberOfFactors(); - private: - int m_numberOfFactors; - Expression ** m_factors; + float operateApproximatevelyOn(float a, float b) override; }; #endif diff --git a/poincare/src/addition.cpp b/poincare/src/addition.cpp index 9742df74b..beed55a95 100644 --- a/poincare/src/addition.cpp +++ b/poincare/src/addition.cpp @@ -1,24 +1,15 @@ #include #include "layout/horizontal_layout.h" -Addition::Addition(Expression * first_operand, Expression * second_operand) { - m_left = first_operand; - m_right = second_operand; -} - Expression::Type Addition::type() { return Expression::Type::Addition; } -Addition::~Addition() { - delete m_left; - delete m_right; -} - -float Addition::approximate(Context& context) { - return m_left->approximate(context) + m_right->approximate(context); +float Addition::operateApproximatevelyOn(float a, float b) { + return a + b; } ExpressionLayout * Addition::createLayout(ExpressionLayout * parent) { - return new HorizontalLayout(parent, m_left, '+', m_right); + //FIXME: There can be more than two operands now! :-) + return new HorizontalLayout(parent, operand(0), '+', operand(1)); } diff --git a/poincare/src/commutative_operation.cpp b/poincare/src/commutative_operation.cpp new file mode 100644 index 000000000..e61a3c078 --- /dev/null +++ b/poincare/src/commutative_operation.cpp @@ -0,0 +1,35 @@ +#include +extern "C" { +#include +} + +CommutativeOperation::CommutativeOperation(Expression * first_operand, Expression * second_operand) { + m_numberOfOperands = 2; + m_operands = (Expression **)malloc(2*sizeof(Expression *)); + m_operands[0] = first_operand; + m_operands[1] = second_operand; +} + +CommutativeOperation::~CommutativeOperation() { + for (int i=0; iapproximate(context); + for (size_t i=1; iapproximate(context); + result = this->operateApproximatevelyOn(result, next); + } + return result; +} diff --git a/poincare/src/product.cpp b/poincare/src/product.cpp index e8b262939..4635fe7ca 100644 --- a/poincare/src/product.cpp +++ b/poincare/src/product.cpp @@ -4,33 +4,8 @@ extern "C" { #include } -Product::Product(Expression * first_factor, Expression * second_factor) { - m_numberOfFactors = 2; - m_factors = (Expression **)malloc(2*sizeof(Expression *)); - m_factors[0] = first_factor; - m_factors[1] = second_factor; -} - -Product::~Product() { - for (int i=0; iapproximate(context); - for (size_t i=1; iapproximate(context); - } - return result; -} - -int Product::numberOfFactors() { - return m_numberOfFactors; -} - -Expression * Product::factor(int i) { - return m_factors[i]; +float Product::operateApproximatevelyOn(float a, float b) { + return a*b; } Expression::Type Product::type() { @@ -39,5 +14,5 @@ Expression::Type Product::type() { ExpressionLayout * Product::createLayout(ExpressionLayout * parent) { //FIXME: There can be more than two factors now! :-) - return new HorizontalLayout(parent, m_factors[0], '*', m_factors[1]); + return new HorizontalLayout(parent, operand(0), '*', operand(1)); } diff --git a/poincare/src/simplify/simplify_product_zero.cpp b/poincare/src/simplify/simplify_product_zero.cpp index 38e253237..e8e5fef27 100644 --- a/poincare/src/simplify/simplify_product_zero.cpp +++ b/poincare/src/simplify/simplify_product_zero.cpp @@ -7,8 +7,8 @@ Expression * SimplifyProductZero(Expression * e) { return nullptr; } Product * p = (Product *)e; - for (int i=0; inumberOfFactors(); i++) { - Expression * factor = p->factor(i); + for (int i=0; inumberOfOperands(); i++) { + Expression * factor = p->operand(i); if (factor->type() == Expression::Type::Integer) { Integer * integer = (Integer *)factor; if (*integer == Integer((native_int_t)0)) {