24const QStringList supportedFuncs = {
"",
"cos",
"sin",
"tan",
"acos",
"asin",
"atan",
"exp",
"ln",
"log10",
"abs"};
26const QRegExp
funcExpr(
"(-)?([a-zA-Z]*[0-9]*)?\\((.+)\\)");
27const QRegExp
numberExpr(
"(-)?([0-9]+\\.?[0-9]*(e[0-9]*)?)");
33double treatFuncs(QString
const& expr,
bool & noProblem);
34double treatLevel1(QString
const& expr,
bool & noProblem);
35double treatLevel2(QString
const& expr,
bool & noProblem);
36double treatLevel3(QString
const& expr,
bool & noProblem);
56 if (noProblem !=
nullptr) {
74 if (noProblem !=
nullptr) {
100 bool lastMetIsNumber =
false;
102 for(
int i = 0; i < expr.size(); i++){
104 if (expr.at(i) ==
'(') {
108 if (expr.at(i) ==
')') {
117 if(i == expr.size()-1 && subCount == 0){
123 if( (expr.at(i) ==
'+' || expr.at(i) ==
'-') &&
126 if (expr.at(i) ==
'-' &&
129 bool cond = !expr.at(i+1).isSpace();
131 if (cond && !lastMetIsNumber) {
137 ret = expr.mid(0, i).trimmed();
138 nextOp = expr.at(i).toLatin1();
139 expr = expr.mid(i+1);
144 if (expr.at(i).isDigit()) {
145 lastMetIsNumber =
true;
146 }
else if (expr.at(i) !=
'.' &&
147 !expr.at(i).isSpace()) {
148 lastMetIsNumber =
false;
171 for(
int i = 0; i < expr.size(); i++){
173 if (expr.at(i) ==
'(') {
177 if (expr.at(i) ==
')') {
186 if(i == expr.size()-1 && subCount == 0){
192 if( (expr.at(i) ==
'*' || expr.at(i) ==
'/') &&
195 ret = expr.mid(0, i).trimmed();
196 nextOp = expr.at(i).toLatin1();
197 expr = expr.mid(i+1);
219 QString exprDestructible = expr;
224 while (!exprDestructible.isEmpty()) {
226 double sign = (nextOp ==
'-') ? -1 : 1;
261 QString exprDestructible = expr;
269 while (!exprDestructible.isEmpty()) {
275 bool needToMultiply = (nextOp ==
'*');
282 if (needToMultiply) {
309 for (
int i = 0; i < expr.size(); i++) {
310 if (expr.at(i) ==
'(') {
312 }
else if(expr.at(i) ==
')') {
318 }
else if (expr.at(i) ==
'^') {
319 if (subLevels == 0) {
326 if (indexCount > 1 || indexPower + 1 >= expr.size()) {
331 if (indexPower > -1) {
334 subExprs << expr.mid(0,indexPower);
335 subExprs << expr.mid(indexPower+1);
340 double base =
treatFuncs(subExprs[0], noProb1);
341 double power =
treatFuncs(subExprs[1], noProb2);
343 return qPow(base, power);
370 if (funcExp.exactMatch(expr.trimmed())) {
372 int sign = funcExp.capturedTexts()[1].isEmpty() ? 1 : -1;
373 QString func = funcExp.capturedTexts()[2].toLower();
374 QString subExpr = funcExp.capturedTexts()[3];
382 if (func.isEmpty()) {
393 val = qCos(val/180*qAcos(-1));
394 }
else if (func ==
"sin") {
395 val = qSin(val/180*qAcos(-1));
396 }
else if (func ==
"tan") {
397 val = qTan(val/180*qAcos(-1));
398 }
else if(func ==
"acos") {
399 val = qAcos(val)*180/qAcos(-1);
400 }
else if (func ==
"asin") {
401 val = qAsin(val)*180/qAcos(-1);
402 }
else if (func ==
"atan") {
403 val = qAtan(val)*180/qAcos(-1);
404 }
else if (func ==
"exp") {
406 }
else if (func ==
"ln") {
408 }
else if (func ==
"log10") {
409 val = qLn(val)/qLn(10.0);
410 }
else if (func ==
"abs") {
415 }
else if(numExp.exactMatch(expr.trimmed())) {
416 return expr.toDouble(&noProblem);
419 double val = QLocale().toDouble(expr, &noProblem);
442 QString exprDestructible = expr;
447 while (!exprDestructible.isEmpty()) {
449 double sign = (nextOp ==
'-') ? -1 : 1;
484 QString exprDestructible = expr;
492 while (!exprDestructible.isEmpty()) {
498 bool needToMultiply = (nextOp ==
'*');
505 if (needToMultiply) {
511 if(std::isinf(result/val) || qIsNaN(result/val)){
541 if (funcExpInteger.exactMatch(expr.trimmed())) {
543 int sign = funcExpInteger.capturedTexts()[1].isEmpty() ? 1 : -1;
544 QString subExpr = funcExpInteger.capturedTexts()[2];
554 }
else if(numberExp.exactMatch(expr.trimmed())) {
555 double value = QVariant(expr).toDouble(&noProblem);
float value(const T *src, size_t ch)
quint64 part(quint64 n1, quint64 n2, int p)
const QRegExp numberExpr("(-)?([0-9]+\\.?[0-9]*(e[0-9]*)?)")
const QRegExp funcExpr("(-)?([a-zA-Z]*[0-9]*)?\\((.+)\\)")
const QVector< char > opLevel2
const QVector< char > opLevel1
QString extractSubExprLevel2(QString &expr, char &nextOp, bool &noProblem)
extractSubExprLevel2 extract from an expression the part of an expression that need to be treated rec...
const QRegExp integerExpr("(-)?([0-9]+)")
double treatFuncs(QString const &expr, bool &noProblem)
treatFuncs treat the last level of recursion: parenthesis and functions.
const QStringList supportedFuncs
QString extractSubExprLevel1(QString &expr, char &nextOp, bool &noProblem)
extractSubExprLevel1 extract from an expression the part of an expression that need to be treated rec...
double treatFuncsInt(QString const &expr, bool &noProblem)
treatFuncs treat the last level of recursion: parenthesis
double treatLevel1(QString const &expr, bool &noProblem)
treatLevel1 treat an expression at the first level of recursion.
double treatLevel3(QString const &expr, bool &noProblem)
treatLevel3 treat a subexpression at the third level of recursion.
double treatLevel1Int(QString const &expr, bool &noProblem)
treatLevel1 treat an expression at the first level of recursion.
double treatLevel2Int(QString const &expr, bool &noProblem)
treatLevel2 treat a subexpression at the second level of recursion.
const QRegExp funcExprInteger("(-)?\\((.+)\\)")
double treatLevel2(QString const &expr, bool &noProblem)
treatLevel2 treat a subexpression at the second level of recursion.
the namespace contains functions to transform math expression written as QString in numbers.
int parseIntegerMathExpr(QString const &expr, bool *noProblem)
parse an expression to an int.
double parseSimpleMathExpr(const QString &expr, bool *noProblem)
parse an expression to a double.