2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file calculator.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-07-17 13:38:11 +02:00
|
|
|
#include "calculator.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
|
|
|
#include <QStaticStringData>
|
|
|
|
#include <QStringData>
|
|
|
|
#include <QStringDataPtr>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include "../ifc/../vmisc/def.h"
|
|
|
|
#include "../qmuparser/qmuparsererror.h"
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2014-06-02 09:43:27 +02:00
|
|
|
using namespace qmu;
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-07 18:55:57 +02:00
|
|
|
/**
|
2015-02-08 12:48:27 +01:00
|
|
|
* @brief Calculator class wraper for QMuParser. Make easy initialization math parser.
|
2014-05-30 15:04:38 +02:00
|
|
|
*
|
|
|
|
* This constructor hide initialization variables, operators, character sets.
|
|
|
|
* Use this constuctor for evaluation formula. All formulas must be converted to internal look.
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* const QString formula = qApp->FormulaFromUser(edit->text());
|
2015-06-12 10:55:44 +02:00
|
|
|
* Calculator *cal = new Calculator(data, patternType);
|
2015-10-15 12:07:43 +02:00
|
|
|
* const qreal result = cal->EvalFormula(data->PlainVariables(), formula);
|
2014-05-30 15:04:38 +02:00
|
|
|
* delete cal;
|
|
|
|
*
|
2014-05-07 18:55:57 +02:00
|
|
|
*/
|
2015-10-15 12:07:43 +02:00
|
|
|
Calculator::Calculator()
|
|
|
|
:QmuFormulaBase()
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-05-23 20:11:13 +02:00
|
|
|
InitCharacterSets();
|
2014-08-29 15:42:53 +02:00
|
|
|
setAllowSubexpressions(false);//Only one expression per time
|
2014-09-08 15:17:38 +02:00
|
|
|
|
2014-09-20 17:00:29 +02:00
|
|
|
SetSepForEval();
|
2014-05-23 20:11:13 +02:00
|
|
|
}
|
2014-05-22 14:11:14 +02:00
|
|
|
|
2014-11-22 13:49:09 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-21 15:51:47 +02:00
|
|
|
Calculator::~Calculator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-05-07 18:55:57 +02:00
|
|
|
/**
|
2014-05-21 10:51:16 +02:00
|
|
|
* @brief eval calculate formula.
|
2015-02-08 12:48:27 +01:00
|
|
|
*
|
|
|
|
* First we try eval expression without adding variables. If it fail, we take tokens from expression and add variables
|
|
|
|
* to parser and try again.
|
|
|
|
*
|
2014-05-21 10:51:16 +02:00
|
|
|
* @param formula string of formula.
|
2014-05-07 18:55:57 +02:00
|
|
|
* @return value of formula.
|
|
|
|
*/
|
2015-10-15 12:07:43 +02:00
|
|
|
qreal Calculator::EvalFormula(const QHash<QString, qreal *> &vars, const QString &formula)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2015-02-08 12:48:27 +01:00
|
|
|
// Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown variable
|
|
|
|
// set value to 0.
|
2014-07-29 17:15:18 +02:00
|
|
|
SetVarFactory(AddVariable, this);
|
2014-09-20 17:00:29 +02:00
|
|
|
SetSepForEval();//Reset separators options
|
2014-07-29 17:15:18 +02:00
|
|
|
|
2014-05-21 10:51:16 +02:00
|
|
|
SetExpr(formula);
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
qreal result = 0;
|
|
|
|
result = Eval();
|
|
|
|
|
|
|
|
QMap<int, QString> tokens = this->GetTokens();
|
2014-11-09 15:01:25 +01:00
|
|
|
|
|
|
|
// Remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
|
2014-11-22 13:49:09 +01:00
|
|
|
RemoveAll(tokens, QStringLiteral("-"));
|
2014-11-09 15:01:25 +01:00
|
|
|
|
2015-05-14 18:20:34 +02:00
|
|
|
for (int i = 0; i < builInFunctions.size(); ++i)
|
|
|
|
{
|
|
|
|
if (tokens.isEmpty())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
RemoveAll(tokens, builInFunctions.at(i));
|
|
|
|
}
|
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
if (tokens.isEmpty())
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2015-02-08 12:48:27 +01:00
|
|
|
return result; // We have found only numbers in expression.
|
2014-05-21 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 12:48:27 +01:00
|
|
|
// Add variables to parser because we have deal with expression with variables.
|
2015-10-15 12:07:43 +02:00
|
|
|
InitVariables(vars, tokens, formula);
|
2014-09-06 10:14:59 +02:00
|
|
|
return Eval();
|
2014-07-29 17:15:18 +02:00
|
|
|
}
|
2014-05-21 15:51:47 +02:00
|
|
|
|
2014-07-29 17:15:18 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-02-08 12:48:27 +01:00
|
|
|
/**
|
|
|
|
* @brief Calculator::InitVariables add variables to parser.
|
|
|
|
*
|
|
|
|
* For optimization purpose we try don't add variables that we don't need.
|
|
|
|
*
|
2015-10-15 12:07:43 +02:00
|
|
|
* @param vars list of variables.
|
2015-02-08 12:48:27 +01:00
|
|
|
* @param tokens all tokens (measurements names, variables with lengths) that parser have found in expression.
|
|
|
|
* @param formula expression, need for throwing better error message.
|
|
|
|
*/
|
2015-10-15 12:07:43 +02:00
|
|
|
void Calculator::InitVariables(const QHash<QString, qreal *> &vars, const QMap<int, QString> &tokens,
|
|
|
|
const QString &formula)
|
2014-07-29 17:15:18 +02:00
|
|
|
{
|
|
|
|
QMap<int, QString>::const_iterator i = tokens.constBegin();
|
|
|
|
while (i != tokens.constEnd())
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2014-09-11 19:15:07 +02:00
|
|
|
bool found = false;
|
2015-10-15 12:07:43 +02:00
|
|
|
if (vars.contains(i.value()))
|
2014-05-21 10:51:16 +02:00
|
|
|
{
|
2015-10-15 12:07:43 +02:00
|
|
|
DefineVar(i.value(), vars.value(i.value()));
|
2014-09-06 10:14:59 +02:00
|
|
|
found = true;
|
2013-11-04 21:35:15 +01:00
|
|
|
}
|
2013-07-17 13:38:11 +02:00
|
|
|
|
2015-10-15 12:07:43 +02:00
|
|
|
if (found == false && builInFunctions.contains(i.value()))
|
|
|
|
{// We have found built-in function
|
|
|
|
found = true;
|
2014-05-21 10:51:16 +02:00
|
|
|
}
|
2014-09-06 10:14:59 +02:00
|
|
|
|
|
|
|
if (found == false)
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
throw qmu::QmuParserError (ecUNASSIGNABLE_TOKEN, i.value(), formula, i.key());
|
2014-09-06 10:14:59 +02:00
|
|
|
}
|
2014-07-29 17:15:18 +02:00
|
|
|
++i;
|
2013-07-17 13:38:11 +02:00
|
|
|
}
|
|
|
|
}
|