[calculation] Create a class RationalListController

This commit is contained in:
Émilie Feral
2019-12-23 19:08:22 +01:00
committed by Léa Saviot
parent 47e7a8407b
commit 54f1984cf0
3 changed files with 68 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ app_calculation_src = $(addprefix apps/calculation/,\
additional_outputs/integer_list_controller.cpp \
additional_outputs/scrollable_input_exact_approximate_expressions_cell.cpp \
additional_outputs/list_controller.cpp \
additional_outputs/rational_list_controller.cpp \
additional_outputs/simple_list_controller.cpp \
additional_outputs/trigonometry_graph_cell.cpp \
additional_outputs/trigonometry_list_controller.cpp \

View File

@@ -0,0 +1,43 @@
#include "rational_list_controller.h"
#include "../app.h"
#include "../../shared/poincare_helpers.h"
#include <poincare_nodes.h>
using namespace Poincare;
using namespace Shared;
namespace Calculation {
int RationalListController::numberOfRows() const {
return 2;
}
Layout RationalListController::layoutAtIndex(int index) {
// TODO: do something simpler using rational?
// TODO: implement mixed fraction
assert(m_expression.type() == ExpressionNode::Type::Division);
Expression c0 = m_expression.childAtIndex(0);
Expression c1 = m_expression.childAtIndex(1);
assert(c0.type() == ExpressionNode::Type::BasedInteger);
assert(c1.type() == ExpressionNode::Type::BasedInteger);
Integer num = static_cast<BasedInteger &>(c0).integer();
Integer denom = static_cast<BasedInteger &>(c1).integer();
Poincare::Context * context = App::app()->localContext();
DivisionQuotient quo = DivisionQuotient::Builder(Rational::Builder(num), Rational::Builder(denom));
PoincareHelpers::Simplify(&quo, context, ExpressionNode::ReductionTarget::User);
DivisionRemainder rem = DivisionRemainder::Builder(Rational::Builder(num), Rational::Builder(denom));
PoincareHelpers::Simplify(&rem, context, ExpressionNode::ReductionTarget::User);
Expression e = Equal::Builder(Rational::Builder(num), Addition::Builder(Multiplication::Builder(Rational::Builder(denom), quo), rem));
return PoincareHelpers::CreateLayout(e);
}
I18n::Message RationalListController::messageAtIndex(int index) {
switch (index) {
case 0:
return I18n::Message::MixedFraction;
default:
return I18n::Message::EuclideanDivision;
}
}
}

View File

@@ -0,0 +1,24 @@
#ifndef CALCULATION_ADDITIONAL_OUTPUTS_RATIONAL_LIST_CONTROLLER_H
#define CALCULATION_ADDITIONAL_OUTPUTS_RATIONAL_LIST_CONTROLLER_H
#include "simple_list_controller.h"
namespace Calculation {
class RationalListController : public SimpleListController {
public:
RationalListController() :
SimpleListController(nullptr) {}
//ListViewDataSource
int numberOfRows() const override;
private:
Poincare::Layout layoutAtIndex(int index) override;
I18n::Message messageAtIndex(int index) override;
};
}
#endif