From 4fa5fef8ef0850869b51675632d6eba2a83b363d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Sun, 15 Jan 2017 20:02:54 +0100 Subject: [PATCH] [poincare] Create a class product Change-Id: I7069b55c7ec17ede42375a83e7f122ff1ed24f88 --- poincare/Makefile | 1 + poincare/include/poincare.h | 1 + poincare/include/poincare/expression.h | 1 + poincare/include/poincare/product.h | 17 +++++++++ poincare/src/expression_lexer.l | 1 + poincare/src/product.cpp | 49 ++++++++++++++++++++++++++ 6 files changed, 70 insertions(+) create mode 100644 poincare/include/poincare/product.h create mode 100644 poincare/src/product.cpp diff --git a/poincare/Makefile b/poincare/Makefile index 8d8236035..759e538ad 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -29,6 +29,7 @@ objs += $(addprefix poincare/src/,\ opposite.o\ parenthesis.o\ power.o\ + product.o\ sine.o\ subtraction.o\ sum.o\ diff --git a/poincare/include/poincare.h b/poincare/include/poincare.h index 9c97b3c16..5ee726d56 100644 --- a/poincare/include/poincare.h +++ b/poincare/include/poincare.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index 75bff507c..190400e78 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -25,6 +25,7 @@ class Expression { Fraction, Parenthesis, Power, + Product, Sine, Sum, Subtraction, diff --git a/poincare/include/poincare/product.h b/poincare/include/poincare/product.h new file mode 100644 index 000000000..4fc8fb1f2 --- /dev/null +++ b/poincare/include/poincare/product.h @@ -0,0 +1,17 @@ +#ifndef POINCARE_PRODUCT_H +#define POINCARE_PRODUCT_H + +#include +#include + +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 diff --git a/poincare/src/expression_lexer.l b/poincare/src/expression_lexer.l index a31ed323c..2586d2132 100644 --- a/poincare/src/expression_lexer.l +++ b/poincare/src/expression_lexer.l @@ -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; } diff --git a/poincare/src/product.cpp b/poincare/src/product.cpp new file mode 100644 index 000000000..d1b6dc77b --- /dev/null +++ b/poincare/src/product.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include "layout/string_layout.h" +#include "layout/horizontal_layout.h" +#include "layout/product_layout.h" +extern "C" { +#include +#include +} + +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()); +}