From 2f77d7036950cd0533a89be2b7414c2e7fcfa694 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Wed, 23 Mar 2016 17:52:26 +0100 Subject: [PATCH] Poincare: Product can now have more than two factors Change-Id: Ia9d606e1a3a1649265fdcfe5e2ba8579e7fd7c7b --- poincare/include/poincare/product.h | 8 ++++-- poincare/src/product.cpp | 32 ++++++++++++++++++----- poincare/src/simplify/simplifier_zero.cpp | 4 +-- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/poincare/include/poincare/product.h b/poincare/include/poincare/product.h index 6b62782d4..2ca3f6e12 100644 --- a/poincare/include/poincare/product.h +++ b/poincare/include/poincare/product.h @@ -11,8 +11,12 @@ class Product : public Expression { ExpressionLayout * createLayout(ExpressionLayout * parent) override; float approximate(Context& context) override; expression_type_t type() override; - //private: - Expression * m_children[2]; + + Expression * factor(int i); + int numberOfFactors(); + private: + int m_numberOfFactors; + Expression ** m_factors; }; #endif diff --git a/poincare/src/product.cpp b/poincare/src/product.cpp index 831eb6bc9..3d932a459 100644 --- a/poincare/src/product.cpp +++ b/poincare/src/product.cpp @@ -1,25 +1,43 @@ #include #include "layout/horizontal_layout.h" +extern "C" { +#include +} Product::Product(Expression * first_factor, Expression * second_factor) { - m_children[0] = first_factor; - m_children[1] = second_factor; + m_numberOfFactors = 2; + m_factors = (Expression **)malloc(2*sizeof(Expression *)); + m_factors[0] = first_factor; + m_factors[1] = second_factor; } Product::~Product() { - delete m_children[1]; - delete m_children[0]; + for (int i=0; iapproximate(context) * m_children[1]->approximate(context); + float result = m_factors[0]->approximate(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]; } expression_type_t Product::type() { return Product::Type; } - ExpressionLayout * Product::createLayout(ExpressionLayout * parent) { - return new HorizontalLayout(parent, m_children[0], '*', m_children[1]); + //FIXME: There can be more than two factors now! :-) + return new HorizontalLayout(parent, m_factors[0], '*', m_factors[1]); } diff --git a/poincare/src/simplify/simplifier_zero.cpp b/poincare/src/simplify/simplifier_zero.cpp index cb9a07fa1..83853409f 100644 --- a/poincare/src/simplify/simplifier_zero.cpp +++ b/poincare/src/simplify/simplifier_zero.cpp @@ -7,8 +7,8 @@ Expression * SimplifierZero(Expression * e) { return nullptr; } Product * p = (Product *)e; - for (int i=0; i<2; i++) { - Expression * factor = p->m_children[i]; + for (int i=0; inumberOfFactors(); i++) { + Expression * factor = p->factor(i); if (factor->type() == Integer::Type) { Integer * integer = (Integer *)factor; if (*integer == Integer((native_int_t)0)) {