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);
}
}
}
-void ArithmeticEvaluator::setExpression(const QString& _expression) throw(int) {
+void ArithmeticEvaluator::setExpression(const QString& _expression) throw(int) {
+
+ setVariableNames(_expression);
+
try {
convert(_expression);
}
}
}
+void ArithmeticEvaluator::setVariablesValue(const QHash<QString,double>& _varValues) {
+ varValues = _varValues;
+ /*
+ QHashIterator<QString,double> 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) << " ";
}
double ArithmeticEvaluator::evaluate() throw(int) {
+ errorMessage = "";
QStack<double> stack;
bool ok;
double value1,value2;
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)) {
*/
void ArithmeticEvaluator::convert(const QString& _expression) throw(int) {
- QString expr = _expression;
+
+ QString expr = _expression;
QString result="";
- expr.remove(QChar(' '), Qt::CaseInsensitive);
- foreach(QString func, fctMarkers) {
+ 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 {
*size = 1;
}
- while ((_expression[i].isLetterOrNumber()) || (_expression[i] == '-') || (_expression[i] == '_')) {
+ while ((_expression[i].isLetterOrNumber()) || (_expression[i] == '_')) {
number.append(_expression[i]);
i += 1;
*size += 1;