The Calculator class for calculation formula. More...
#include <calculator.h>
Public Member Functions | |
Calculator () | |
Calculator class wraper for QMuParser. Make easy initialization math parser. More... | |
virtual | ~Calculator () Q_DECL_EQ_DEFAULT |
qreal | EvalFormula (const QHash< QString, QSharedPointer< VInternalVariable > > *vars, const QString &formula) |
eval calculate formula. More... | |
Public Member Functions inherited from qmu::QmuFormulaBase | |
QmuFormulaBase () | |
virtual | ~QmuFormulaBase () Q_DECL_OVERRIDE |
virtual void | InitCharSets () Q_DECL_OVERRIDE |
InitCharSets init character set for parser. More... | |
Public Member Functions inherited from qmu::QmuParser | |
QmuParser () | |
Constructor. More... | |
virtual void | InitFun () Q_DECL_OVERRIDE |
Initialize the default functions. More... | |
virtual void | InitConst () Q_DECL_OVERRIDE |
Initialize constants. More... | |
virtual void | InitOprt () Q_DECL_OVERRIDE |
Initialize operators. More... | |
virtual void | OnDetectVar (const QString &pExpr, int &nStart, int &nEnd) Q_DECL_OVERRIDE |
qreal | Diff (qreal *a_Var, qreal a_fPos, qreal a_fEpsilon=0) const |
Numerically differentiate with regard to a variable. More... | |
Public Member Functions inherited from qmu::QmuParserBase | |
QmuParserBase () | |
Constructor. More... | |
QmuParserBase (const QmuParserBase &a_Parser) | |
Copy constructor. More... | |
QmuParserBase & | operator= (const QmuParserBase &a_Parser) |
Assignement operator. More... | |
virtual | ~QmuParserBase () |
qreal | Eval () const |
Calculate the result. More... | |
qreal * | Eval (int &nStackSize) const |
Evaluate an expression containing comma seperated subexpressions. More... | |
void | Eval (qreal *results, int nBulkSize) const |
int | GetNumResults () const |
Return the number of results on the calculation stack. More... | |
void | SetExpr (const QString &a_sExpr) |
Set the formula. More... | |
void | SetVarFactory (facfun_type a_pFactory, void *pUserData=nullptr) |
Set a function that can create variable pointer for unknown expression variables. More... | |
void | ResetLocale () |
Resets the locale. More... | |
void | EnableOptimizer (bool a_bIsOn=true) |
Enable or disable the formula optimization feature. More... | |
void | EnableBuiltInOprt (bool a_bIsOn=true) |
Enable or disable the built in binary operators. More... | |
bool | HasBuiltInOprt () const |
Query status of built in variables. More... | |
void | AddValIdent (identfun_type a_pCallback) |
Add a value parsing function. More... | |
void | DefineOprt (const QString &a_sName, fun_type2 a_pFun, unsigned a_iPrec=0, EOprtAssociativity a_eAssociativity=oaLEFT, bool a_bAllowOpt=false) |
Define a binary operator. More... | |
void | DefineConst (const QString &a_sName, qreal a_fVal) |
Add a user defined constant. More... | |
void | DefineStrConst (const QString &a_strName, const QString &a_strVal) |
Define a new string constant. More... | |
void | DefineVar (const QString &a_sName, qreal *a_pVar) |
Add a user defined variable. More... | |
void | DefinePostfixOprt (const QString &a_sFun, fun_type1 a_pFun, bool a_bAllowOpt=true) |
Add a user defined operator. More... | |
void | DefineInfixOprt (const QString &a_sName, fun_type1 a_pFun, int a_iPrec=prINFIX, bool a_bAllowOpt=true) |
Add a user defined operator. More... | |
void | ClearVar () |
Clear all user defined variables. More... | |
void | ClearFun () |
Clear all functions. More... | |
void | ClearConst () |
Clear all user defined constants. More... | |
void | ClearInfixOprt () |
Clear the user defined Prefix operators. More... | |
void | ClearPostfixOprt () |
Clear all user defined postfix operators. More... | |
void | ClearOprt () |
Clear all user defined binary operators. More... | |
void | RemoveVar (const QString &a_strVarName) |
Remove a variable from internal storage. More... | |
const varmap_type & | GetUsedVar () const |
Return a map containing the used variables only. More... | |
const varmap_type & | GetVar () const |
Return a map containing the used variables only. More... | |
const valmap_type & | GetConst () const |
Return a map containing all parser constants. More... | |
const QString & | GetExpr () const |
Retrieve the formula. More... | |
const funmap_type & | GetFunDef () const |
Return prototypes of all parser functions. More... | |
QMap< int, QString > | GetTokens () const |
QMap< int, QString > | GetNumbers () const |
void | DefineNameChars (const QString &a_szCharset) |
Define the set of valid characters to be used in names of functions, variables, constants. More... | |
void | DefineOprtChars (const QString &a_szCharset) |
Define the set of valid characters to be used in names of binary operators and postfix operators. More... | |
void | DefineInfixOprtChars (const QString &a_szCharset) |
Define the set of valid characters to be used in names of infix operators. More... | |
const QString & | ValidNameChars () const |
Virtual function that defines the characters allowed in name identifiers. More... | |
const QString & | ValidOprtChars () const |
Virtual function that defines the characters allowed in operator definitions. More... | |
const QString & | ValidInfixOprtChars () const |
Virtual function that defines the characters allowed in infix operator definitions. More... | |
void | SetArgSep (char_type cArgSep) |
Set argument separator. More... | |
QChar | GetArgSep () const |
Get the argument separator character. More... | |
void Q_NORETURN | Error (EErrorCodes a_iErrc, int a_iPos=-1, const QString &a_sTok=QString()) const |
Create an error containing the parse error position. More... | |
template<typename T > | |
void | DefineFun (const QString &a_strName, T a_pFun, bool a_bAllowOpt=true) |
Define a parser function without arguments. More... | |
void | setAllowSubexpressions (bool value) |
QLocale | getLocale () const |
void | setLocale (const QLocale &value) |
QChar | getDecimalPoint () const |
void | setDecimalPoint (const QChar &c) |
QChar | getThousandsSeparator () const |
void | setThousandsSeparator (const QChar &c) |
Private Member Functions | |
void | InitVariables (const QHash< QString, QSharedPointer< VInternalVariable > > *vars, const QMap< int, QString > &tokens, const QString &formula) |
Calculator::InitVariables add variables to parser. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from qmu::QmuFormulaBase | |
static void | RemoveAll (QMap< int, QString > &map, const QString &val) |
RemoveAll remove token from token list. More... | |
Static Public Member Functions inherited from qmu::QmuParserBase | |
static void | EnableDebugDump (bool bDumpCmd, bool bDumpStack) |
Enable the dumping of bytecode amd stack content on the console. More... | |
static QString | GetVersion (EParserVersionInfo eInfo=pviFULL) |
Returns the version of muparser. More... | |
static const QStringList & | GetOprtDef () |
Get the default symbols used for the built in operators. More... | |
Protected Member Functions inherited from qmu::QmuFormulaBase | |
void | SetSepForTr (bool osSeparator, bool fromUser) |
SetSepForTr set separators for translation expression. More... | |
void | SetSepForEval () |
SetSepForEval set separators for eval. Each expression eval in internal (C) locale. More... | |
Protected Member Functions inherited from qmu::QmuParserBase | |
void | Init () |
Initialize user defined functions. More... | |
Static Protected Member Functions inherited from qmu::QmuFormulaBase | |
static qreal * | AddVariable (const QString &a_szName, void *a_pUserData) |
Static Protected Member Functions inherited from qmu::QmuParser | |
static int | IsVal (const QString &a_szExpr, int *a_iPos, qreal *a_fVal, const QLocale &locale, const QChar &decimal, const QChar &thousand) |
Default value recognition callback. More... | |
static qreal | Sinh (qreal) |
static qreal | Cosh (qreal) |
static qreal | Tanh (qreal) |
static qreal | ASinh (qreal) |
static qreal | ACosh (qreal) |
static qreal | ATanh (qreal) |
static qreal | DegreeToRadian (qreal) |
static qreal | RadianToDegree (qreal) |
static qreal | SinD (qreal) |
static qreal | CosD (qreal) |
static qreal | TanD (qreal) |
static qreal | ASinD (qreal) |
static qreal | ACosD (qreal) |
static qreal | ATanD (qreal) |
static qreal | Log2 (qreal) |
static qreal | Log10 (qreal) |
static qreal | Abs (qreal) |
static qreal | Rint (qreal) |
static qreal | Sign (qreal) |
static qreal | FMod (qreal, qreal) |
static qreal | UnaryMinus (qreal v) |
Callback for the unary minus operator. More... | |
static qreal | Sum (const qreal *, int) |
Callback for adding multiple values. More... | |
static qreal | Avg (const qreal *, int) |
Callback for averaging multiple values. More... | |
static qreal | Min (const qreal *, int) |
Callback for determining the minimum value out of a vector. More... | |
static qreal | Max (const qreal *, int) |
Callback for determining the maximum value out of a vector. More... | |
Protected Attributes inherited from qmu::QmuParserBase | |
QLocale | m_locale |
The locale used by the parser. More... | |
QChar | m_decimalPoint |
QChar | m_thousandsSeparator |
Static Protected Attributes inherited from qmu::QmuParserBase | |
static const QStringList | c_DefaultOprt |
Identifiers for built in binary operators. More... | |
static bool | g_DbgDumpCmdCode = false |
static bool | g_DbgDumpStack = false |
The Calculator class for calculation formula.
Main purpose make easy evaluate value of formula and get tokens. Note. If created to many parser for different purpes in the same time parser can work wrong. Example: EditFormulaDialog *dialog = new EditFormulaDialog(data); dialog->SetFormula(formula); if (dialog->exec() == QDialog::Accepted) { formula = dialog->GetFormula(); //Need delete dialog here because parser in dialog don't allow use correct separator for parsing here. //Don't know why. delete dialog; QScopedPointer<Calculator> cal(new Calculator()); result = cal->EvalFormula(data->PlainVariables(), formula); }
Definition at line 83 of file calculator.h.
Calculator::Calculator | ( | ) |
Calculator class wraper for QMuParser. Make easy initialization math parser.
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()); Calculator *cal = new Calculator(data, patternType); const qreal result = cal->EvalFormula(data->PlainVariables(), formula); delete cal;
Definition at line 77 of file calculator.cpp.
References qmu::QmuFormulaBase::InitCharSets(), qmu::QmuParserBase::setAllowSubexpressions(), and qmu::QmuFormulaBase::SetSepForEval().
|
virtual |
qreal Calculator::EvalFormula | ( | const QHash< QString, QSharedPointer< VInternalVariable > > * | vars, |
const QString & | formula | ||
) |
eval calculate formula.
First we try eval expression without adding variables. If it fail, we take tokens from expression and add variables to parser and try again.
formula | string of formula. |
Definition at line 96 of file calculator.cpp.
References qmu::QmuFormulaBase::AddVariable(), builInFunctions, qmu::QmuParserBase::Eval(), qmu::QmuParserBase::GetTokens(), InitVariables(), qmu::QmuFormulaBase::RemoveAll(), qmu::QmuParserBase::SetExpr(), qmu::QmuFormulaBase::SetSepForEval(), and qmu::QmuParserBase::SetVarFactory().
Referenced by anonymous_namespace{vlayoutpiece.cpp}::FindGrainlineGeometry(), PatternPieceTool::FindGrainlineGeometry(), anonymous_namespace{vlayoutpiece.cpp}::FindLabelGeometry(), PatternPieceTool::FindLabelGeometry(), PatternPieceDialog::getFormulaValue(), PatternPieceDialog::updateGrainlineValues(), PatternPieceDialog::updatePatternLabelValues(), and PatternPieceDialog::updatePieceLabelValues().
|
private |
Calculator::InitVariables add variables to parser.
For optimization purpose we try don't add variables that we don't need.
vars | list of variables. |
tokens | all tokens (measurements names, variables with lengths) that parser have found in expression. |
formula | expression, need for throwing better error message. |
Definition at line 142 of file calculator.cpp.
References builInFunctions, qmu::QmuParserBase::DefineVar(), and qmu::ecUNASSIGNABLE_TOKEN.
Referenced by EvalFormula().