[poincare] Create a class product

Change-Id: I7069b55c7ec17ede42375a83e7f122ff1ed24f88
This commit is contained in:
Émilie Feral
2017-01-15 20:02:54 +01:00
parent ec4178b2b9
commit 4fa5fef8ef
6 changed files with 70 additions and 0 deletions

View File

@@ -29,6 +29,7 @@ objs += $(addprefix poincare/src/,\
opposite.o\
parenthesis.o\
power.o\
product.o\
sine.o\
subtraction.o\
sum.o\

View File

@@ -24,6 +24,7 @@
#include <poincare/opposite.h>
#include <poincare/parenthesis.h>
#include <poincare/power.h>
#include <poincare/product.h>
#include <poincare/sine.h>
#include <poincare/subtraction.h>
#include <poincare/sum.h>

View File

@@ -25,6 +25,7 @@ class Expression {
Fraction,
Parenthesis,
Power,
Product,
Sine,
Sum,
Subtraction,

View File

@@ -0,0 +1,17 @@
#ifndef POINCARE_PRODUCT_H
#define POINCARE_PRODUCT_H
#include <poincare/function.h>
#include <poincare/n_context.h>
class Product : public Function {
public:
Product();
float approximate(Context & context) const override;
Type type() const override;
Expression * cloneWithDifferentOperands(Expression ** newOperands,
int numberOfOperands, bool cloneOperands = true) const override;
ExpressionLayout * createLayout() const override;
};
#endif

View File

@@ -90,6 +90,7 @@ tan { poincare_expression_yylval.expression = new Tangent(); return FUNCTION; }
log { poincare_expression_yylval.expression = new Logarithm(); return FUNCTION; }
root { poincare_expression_yylval.expression = new NthRoot(); return FUNCTION; }
sum { poincare_expression_yylval.expression = new Sum(); return FUNCTION; }
product { poincare_expression_yylval.expression = new Product(); return FUNCTION; }
\+ { return PLUS; }
\- { return MINUS; }
\* { return MULTIPLY; }

49
poincare/src/product.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include <poincare/product.h>
#include <poincare/symbol.h>
#include <poincare/float.h>
#include "layout/string_layout.h"
#include "layout/horizontal_layout.h"
#include "layout/product_layout.h"
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
Product::Product() :
Function("product")
{
}
Expression::Type Product::type() const {
return Type::Product;
}
Expression * Product::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(numberOfOperands == 2);
assert(newOperands != nullptr);
Product * p = new Product();
p->setArgument(newOperands, numberOfOperands, cloneOperands);
return p;
}
float Product::approximate(Context& context) const {
NContext nContext = NContext(&context);
Symbol nSymbol = Symbol('n');
int start = m_args[1]->approximate(context);
int end = m_args[2]->approximate(context);
float result = 1.0f;
for (int i = start; i <= end; i++) {
Float iExpression = Float(i);
nContext.setExpressionForSymbolName(&iExpression, &nSymbol);
result = result*m_args[0]->approximate(nContext);
}
return result;
}
ExpressionLayout * Product::createLayout() const {
ExpressionLayout ** childrenLayouts = (ExpressionLayout **)malloc(2*sizeof(ExpressionLayout *));
childrenLayouts[0] = new StringLayout("n=", 2);
childrenLayouts[1] = m_args[1]->createLayout();
return new ProductLayout(new HorizontalLayout(childrenLayouts, 2), m_args[2]->createLayout(), m_args[0]->createLayout());
}