Poincare: Product can now have more than two factors

Change-Id: Ia9d606e1a3a1649265fdcfe5e2ba8579e7fd7c7b
This commit is contained in:
Romain Goyet
2016-03-23 17:52:26 +01:00
parent d3b0841af6
commit 2f77d70369
3 changed files with 33 additions and 11 deletions

View File

@@ -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

View File

@@ -1,25 +1,43 @@
#include <poincare/product.h>
#include "layout/horizontal_layout.h"
extern "C" {
#include <stdlib.h>
}
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; i<m_numberOfFactors; i++) {
delete m_factors[i];
}
}
float Product::approximate(Context& context) {
return m_children[0]->approximate(context) * m_children[1]->approximate(context);
float result = m_factors[0]->approximate(context);
for (size_t i=1; i<m_numberOfFactors; i++) {
result *= m_factors[i]->approximate(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]);
}

View File

@@ -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; i<p->numberOfFactors(); i++) {
Expression * factor = p->factor(i);
if (factor->type() == Integer::Type) {
Integer * integer = (Integer *)factor;
if (*integer == Integer((native_int_t)0)) {