PDA

View Full Version : Convert "(scaleValue<0.5)?(4.5*data1):(1.5*data2)" to double value



sankar
8th August 2011, 05:28
Hello,

I would like to convert "(scaleValue<0.5)?(4.5*data1) : (1.5*data2)" to double value with following assumptions,

scaleValue = 1.5;
data1 = 2.0;
data2 = 4.0;

I would like to convert the above string and return result as double value. How to do this?

Regards,
Sankar.

SixDegrees
8th August 2011, 07:34
There is no string in any of the expressions above. Do your assignments in the branch statements.

sankar
8th August 2011, 07:50
I mean the expression is stored in a string as given below,
QString strData = "(scaleValue<0.5)?(4.5*data1) : (1.5*data2)";

I would like to evaluate the expression and returns its resultant value.


Regards,
Sankar.

littletux
8th August 2011, 13:18
You already named it - you need an expression evaluator :-)
Search the internet for Scanner, Parser, expression evaluation. Depending on the functionality you need, this can become quite complex - http://www.arstdesign.com/articles/expression_evaluation.html gives you an idea. For sure there are expression evaluators in C/C++ available on the internet which you can start with and adjust to your needs.

Regards,
Andreas

wysota
8th August 2011, 16:26
QString strData = "(scaleValue<0.5)?(4.5*data1) : (1.5*data2)";
QScriptEngine engine;
QScriptValue result = engine.evaluate(strData);
qreal resultDouble = result.toNumber(); // or qscriptvalue_cast<qreal>(result);

sankar
9th August 2011, 12:18
Thanks Wysota.

Will try to make use of this.

Regards,
Sankar.