PDA

View Full Version : QString - problem with adding strings



pitterb
13th January 2009, 18:35
void Calculator::calculation(QString &operation, QString &text_display)
{
QString a_string = "", b_string = "", result_string = "", temp_string = "";
double a, b, result;
int position, i, j;


position = operation.indexOf("*", 0) ;

for( i = position - 1; i >= 0 ; i-- )
{
if( operation.at( i ) == '*' || operation.at( i ) == '/' || operation.at( i ) == '+' || operation.at( i ) == '-' )
break;
temp_string = a_string;
a_string = operation.at( i ) + temp_string;
}
ui.display->setText( a_string ); //displays a_string on QLineEdit

}

Basically this function has to extract a number placed before '*' operator.
When operation = "3+56*35" a_string = "56" and it works correctly. But when operation="56*35" (without any operator before "56" - that is importanat) it multiplies extraced 56 three times and a_string = "565656". I've been thinking for a few hours about that and the problem still exists.

Thanks for help.

wysota
13th January 2009, 21:03
Build upon this:

QRegExp multiplication("(.*)\*(.*)");
if(multiplication.exactMatch(someString)){
QString firstOperand = multiplication.cap(1);
QString secondOperand = multiplication.cap(2);
// search firstOperand for +,-,/
// search secondOperand for +,-,*,-
} else {
// no multiplication found
}