That code doesn't seem to be right.
I download the C/C++ code, and use "4.4e-05 + 1" to test, it gives
Result = -0.6
.....
The problem is to split "+" and "-" if they are math operator, but don't split scientific notation, such as "4.4e-05"
Added after 48 minutes:
I use 3 passes and it appears to work
#include <QStringList>
#include <QRegExp>
#include <QDebug>
static QStringList getFormulaVarList
( const QString
& formula
) {
// split the formular in 3 passes
QRegExp noScientificRx
( "\\d+[e|E][+-]\\d+" );
// pass 1 - remove scientific notation
foreach( const QString& scientific, formula.split( noScientificRx, behavior ) ) {
// pass 2 - remove math operator
foreach( const QString& str, scientific.split( opRx, behavior ) ) {
bool ok;
str.toDouble( &ok );
if ( !ok ) {
// pass 3 - remove +/-
varList << str.split( plusMinusRx, behavior );
}
}
}
varList.removeDuplicates();
foreach( const QString& str, varList ) {
// finally remove numbers and math objects
bool ok;
str.toDouble( &ok );
if ( !ok && !str.startsWith( "math.", Qt::CaseInsensitive ) ) {
result << str;
}
}
result.removeDuplicates();
return result;
} // getFormulaVarList
int main( int argc, char** argv )
{
//QString formula( "Stage1 <= 4.4e-05 || Stage == 1.2 && Comp >= 1.4e+03 || A+e-C > D" );
//QString formula( "a * aa+4.4e-05 + math.log( b )" );
//QString formula( argv[ 1 ] );
if ( argc == 1 ) {
formula = "a*aa+4.4e-05+math.log( b )";
} else {
formula = argv[ 1 ];
}
qDebug() << "formula =" << formula;
qDebug() << "variables =" << getFormulaVarList( formula );
}
#include <QStringList>
#include <QRegExp>
#include <QDebug>
static QStringList getFormulaVarList( const QString& formula )
{
QString::SplitBehavior behavior = QString::SkipEmptyParts;
// split the formular in 3 passes
QRegExp noScientificRx( "\\d+[e|E][+-]\\d+" );
QRegExp opRx( "[*/()<>&=| ]" );
QRegExp plusMinusRx( "[+\\-]" );
QStringList varList;
// pass 1 - remove scientific notation
foreach( const QString& scientific, formula.split( noScientificRx, behavior ) ) {
// pass 2 - remove math operator
foreach( const QString& str, scientific.split( opRx, behavior ) ) {
bool ok;
str.toDouble( &ok );
if ( !ok ) {
// pass 3 - remove +/-
varList << str.split( plusMinusRx, behavior );
}
}
}
varList.removeDuplicates();
QStringList result;
foreach( const QString& str, varList ) {
// finally remove numbers and math objects
bool ok;
str.toDouble( &ok );
if ( !ok && !str.startsWith( "math.", Qt::CaseInsensitive ) ) {
result << str;
}
}
result.removeDuplicates();
return result;
} // getFormulaVarList
int main( int argc, char** argv )
{
//QString formula( "Stage1 <= 4.4e-05 || Stage == 1.2 && Comp >= 1.4e+03 || A+e-C > D" );
//QString formula( "a * aa+4.4e-05 + math.log( b )" );
//QString formula( argv[ 1 ] );
QString formula;
if ( argc == 1 ) {
formula = "a*aa+4.4e-05+math.log( b )";
} else {
formula = argv[ 1 ];
}
qDebug() << "formula =" << formula;
qDebug() << "variables =" << getFormulaVarList( formula );
}
To copy to clipboard, switch view to plain text mode
Added after 5 minutes:
I reduce to 2 passes
#include <QStringList>
#include <QRegExp>
#include <QDebug>
static QStringList getFormulaVarList
( const QString
& formula
) {
// split the formular in 2 passes
QRegExp scientificRx
( "\\d+[e|E][+-]\\d+" );
QRegExp opRx
( "[*/()<>&=|+\\- ]" );
// pass 1 - remove scientific notation
foreach( const QString& noScientific,
formula.split( scientificRx, behavior ) ) {
// pass 2 - remove math operator
foreach( const QString& str,
noScientific.split( opRx, behavior ) ) {
bool ok;
str.toDouble( &ok );
if ( !ok ) {
varList << str;
}
}
}
varList.removeDuplicates();
foreach( const QString& str, varList ) {
// finally remove numbers and math objects
bool ok;
str.toDouble( &ok );
if ( !ok && !str.startsWith( "math.", Qt::CaseInsensitive ) ) {
result << str;
}
}
result.removeDuplicates();
return result;
} // getFormulaVarList
int main( int argc, char** argv )
{
//QString formula( "Stage1 <= 4.4e-05 || Stage == 1.2 && Comp >= 1.4e+03 || A+e-C > D" );
//QString formula( "a * aa+4.4e-05 + math.log( b )" );
//QString formula( argv[ 1 ] );
if ( argc == 1 ) {
formula = "a*aa+4.4e-05+math.log( b )";
} else {
formula = argv[ 1 ];
}
qDebug() << "formula =" << formula;
qDebug() << "variables =" << getFormulaVarList( formula );
}
#include <QStringList>
#include <QRegExp>
#include <QDebug>
static QStringList getFormulaVarList( const QString& formula )
{
QString::SplitBehavior behavior = QString::SkipEmptyParts;
// split the formular in 2 passes
QRegExp scientificRx( "\\d+[e|E][+-]\\d+" );
QRegExp opRx( "[*/()<>&=|+\\- ]" );
QStringList varList;
// pass 1 - remove scientific notation
foreach( const QString& noScientific,
formula.split( scientificRx, behavior ) ) {
// pass 2 - remove math operator
foreach( const QString& str,
noScientific.split( opRx, behavior ) ) {
bool ok;
str.toDouble( &ok );
if ( !ok ) {
varList << str;
}
}
}
varList.removeDuplicates();
QStringList result;
foreach( const QString& str, varList ) {
// finally remove numbers and math objects
bool ok;
str.toDouble( &ok );
if ( !ok && !str.startsWith( "math.", Qt::CaseInsensitive ) ) {
result << str;
}
}
result.removeDuplicates();
return result;
} // getFormulaVarList
int main( int argc, char** argv )
{
//QString formula( "Stage1 <= 4.4e-05 || Stage == 1.2 && Comp >= 1.4e+03 || A+e-C > D" );
//QString formula( "a * aa+4.4e-05 + math.log( b )" );
//QString formula( argv[ 1 ] );
QString formula;
if ( argc == 1 ) {
formula = "a*aa+4.4e-05+math.log( b )";
} else {
formula = argv[ 1 ];
}
qDebug() << "formula =" << formula;
qDebug() << "variables =" << getFormulaVarList( formula );
}
To copy to clipboard, switch view to plain text mode
Bookmarks