[poincare][apps] Replace Ans symbol when preprocessing the calculation

This commit is contained in:
Émilie Feral
2018-01-04 10:02:37 +01:00
committed by EmilieNumworks
parent 17fc7998a2
commit 775432efdc
12 changed files with 43 additions and 78 deletions

View File

@@ -1,4 +1,5 @@
#include "calculation.h"
#include "calculation_store.h"
#include <string.h>
#include <cmath>
using namespace Poincare;
@@ -63,9 +64,10 @@ void Calculation::reset() {
tidy();
}
void Calculation::setContent(const char * c, Context * context) {
void Calculation::setContent(const char * c, Context * context, CalculationStore * calculationStore) {
reset();
m_input = Expression::parse(c);
Expression::ReplaceSymbolWithExpression(&m_input, Symbol::SpecialSymbols::Ans, ansExpression(calculationStore, context));
/* We do not store directly the text enter by the user but its serialization
* to be able to compare it to the exact ouput text. */
m_input->writeTextInBuffer(m_inputText, sizeof(m_inputText));
@@ -196,4 +198,16 @@ bool Calculation::shouldDisplayApproximateOutput(Context * context) {
return input()->isApproximate(*context);
}
Expression * Calculation::ansExpression(CalculationStore * calculationStore, Context * context) {
if (calculationStore->numberOfCalculations() == 0) {
static Rational defaultExpression(0);
return &defaultExpression;
}
Calculation * lastCalculation = calculationStore->calculationAtIndex(calculationStore->numberOfCalculations()-1);
if (lastCalculation->input()->isApproximate(*context)) {
return lastCalculation->approximateOutput(context);
}
return lastCalculation->exactOutput(context);
}
}