X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/blast.git/blobdiff_plain/5d4e709cb8d460b2efc083e6e7999f1c3a0eb602..9bfa0c13066918f440ac2b5461fb3f8847f43fd6:/ArithmeticEvaluator.cpp?ds=sidebyside diff --git a/ArithmeticEvaluator.cpp b/ArithmeticEvaluator.cpp index dd8081e..54a9596 100644 --- a/ArithmeticEvaluator.cpp +++ b/ArithmeticEvaluator.cpp @@ -22,13 +22,19 @@ ArithmeticEvaluator::ArithmeticEvaluator() { since spaces are removed. Thus, an expression like sin 10 will lead to sin10 after spaces removal, and thus becomes invalid. */ - fctMarkers << "sin" << "cos" << "log10" << "log2" << "log" << "ceil" << "floor" << "round"; + fctMarkers << "sin" << "cos" << "log10" << "log2" << "log" << "ceil" << "floor" << "round"; } ArithmeticEvaluator::ArithmeticEvaluator(const QString& _expression) throw(int) { opMarkers = "+-*/"; varMarkers = "$"; - fctMarkers << "sin" << "cos" << "log10" << "log2" << "log" << "ceil" << "floor" << "round"; + expression = QStringList(); + /* CAUTION : function are mandatory using ( ) to encapsulate the operand + since spaces are removed. Thus, an expression like sin 10 will lead to + sin10 after spaces removal, and thus becomes invalid. + */ + fctMarkers << "sin" << "cos" << "log10" << "log2" << "log" << "ceil" << "floor" << "round"; + try { setExpression(_expression); } @@ -37,7 +43,10 @@ ArithmeticEvaluator::ArithmeticEvaluator(const QString& _expression) throw(int) } } -void ArithmeticEvaluator::setExpression(const QString& _expression) throw(int) { +void ArithmeticEvaluator::setExpression(const QString& _expression) throw(int) { + + setVariableNames(_expression); + try { convert(_expression); } @@ -46,6 +55,28 @@ void ArithmeticEvaluator::setExpression(const QString& _expression) throw(int) { } } +void ArithmeticEvaluator::setVariablesValue(const QHash& _varValues) { + varValues = _varValues; + /* + QHashIterator iterV(varValues); + while (iterV.hasNext()) { + iterV.next(); + cout << "var " << qPrintable(iterV.key()) << " = " << iterV.value() << endl; + } + */ +} + +void ArithmeticEvaluator::setVariableNames(const QString& _expression) { + varNames.clear(); + QRegularExpression re("[$][a-zA-Z0-9_]+"); + QRegularExpressionMatchIterator matcher = re.globalMatch(_expression); + while(matcher.hasNext()) { + QRegularExpressionMatch m = matcher.next(); + QString var = m.captured(0); + varNames.append(var); + } +} + void ArithmeticEvaluator::print() { foreach(QString elt, expression) { cout << qPrintable(elt) << " "; @@ -84,6 +115,7 @@ double ArithmeticEvaluator::evalFunction(int indexFunc, double value) { } double ArithmeticEvaluator::evaluate() throw(int) { + errorMessage = ""; QStack stack; bool ok; double value1,value2; @@ -102,7 +134,11 @@ double ArithmeticEvaluator::evaluate() throw(int) { stack.push(evalFunction(idFunc,value1)); } else if (varMarkers.contains(c)) { - if (!varValues.contains(elt)) throw(-index); + if (!varValues.contains(elt)) { + errorMessage = "cannot find value of "; + errorMessage += qPrintable(elt); + throw(-index); + } stack.push(varValues.value(elt)); } else if (opMarkers.contains(c)) { @@ -166,18 +202,14 @@ double ArithmeticEvaluator::evaluate() throw(int) { void ArithmeticEvaluator::convert(const QString& _expression) throw(int) { - QString expr = _expression; - cout << "converting " << qPrintable(expr) << endl; + QString expr = _expression; QString result=""; - expr.remove(QChar(' '), Qt::CaseInsensitive); - cout << "converting " << qPrintable(expr) << endl; - foreach(QString func, fctMarkers) { - cout << "for " << qPrintable(func) << endl; + expr.remove(QChar(' '), Qt::CaseInsensitive); + foreach(QString func, fctMarkers) { QString rep = QString("\x1b%1").arg(fctMarkers.indexOf(QRegExp(func))); expr.replace(QRegExp(func),rep); - } - cout << "packed expr: " << qPrintable(expr) << endl; + } int offset = 0; try { @@ -379,7 +411,7 @@ QString ArithmeticEvaluator::getVariable(const QString& _expression, int offset, *size = 1; } - while ((_expression[i].isLetterOrNumber()) || (_expression[i] == '-') || (_expression[i] == '_')) { + while ((_expression[i].isLetterOrNumber()) || (_expression[i] == '_')) { number.append(_expression[i]); i += 1; *size += 1;