PDA

View Full Version : Not able to recognize "-"



munna
16th June 2006, 10:50
Hi,

I am using Qt 3 and trying a parse a string entered by the user. For this i am extracting each character and playing with it. The problem is when i try to compare a QString which is contains "-" with "-" the result is false.

Has someone encountered this kind of problem ? Am I doing something wrong? Here is the code:




for(pos=0;pos<expr.length();pos++)
{
QString curr = expr.at(pos);
if(curr == ":")
{
OperatorParse("+");
}
else if((curr == QString(QChar(0xD7)))||(curr==QString(QChar(0xDD)) )||
(curr == "+")||(curr == "-")||(curr == "*")||(curr == "/")||(curr == "^"))
{
OperatorParse(curr);
}
else if(curr == "(")
{
LeftParse(0, pos);
}
else if(curr == "{")
{
LeftParse(1, pos);
}
else if(curr == "[")
{
LeftParse(2, pos);
}
else if((curr == ")")||(curr == "}")||(curr == "]"))
{
RightParse();
}
}



expr is the string that i am parsing.
I entered "2 - 2" but the - is not recongnized

Thanks

wysota
16th June 2006, 10:59
I guess this would be more efficient:

for(pos=0;pos<expr.length();pos++){
const char curr = expr[pos];
switch(curr){
case ':':
OperatorParse('+');break;
case '+':
case '-':
case '*':
case '/':
case '^':
OperatorParse(curr); break;
case '(':
LeftParse(0, pos); break;
case '{':
LeftParse(1, pos); break;
case '[':
LeftParse(2, pos); break;
case ')':
case '}':
case ']':
RightParse(); break;
default:
std::cerr << "Ignored: " << curr << " (" << (int)curr << ")" << std::endl;
}
}
This way you'll know exactly what happens at each moment.

munna
16th June 2006, 11:07
I guess this would be more efficient

Thanks for the idea, but can I use

QString(QChar(0xD7)) and (curr==QString(QChar(0xDD)) with switch case ?

Also the I have noticed that there are more than one character that is similar to "-". The user was actually not typing minus but some other similar character.

Thanks a lot

wysota
16th June 2006, 11:13
Thanks for the idea, but can I use

QString(QChar(0xD7)) and (curr==QString(QChar(0xDD)) with switch case ?

case 0xd7: case 0xdd: ...

You can also use QChar::unicode() and use unicode values in switch cases.


Also the I have noticed that there are more than one character that is similar to "-". The user was actually not typing minus but some other similar character
Probably &ndash; or &mdash; These are available as unicode characters, you should either disallow them or convert them to a minus before parsing. The alphabet should be as simple as possible.

munna
16th June 2006, 12:38
const char curr = expr[pos];

gives me the following error


error C2666: 'QString::operator`[]'' : 3 overloads have similar conversions


When I use


const char curr = expr.constref(pos);

The case 0xdd and 0xd7 doesn't work.

Thanks a lot

wysota
16th June 2006, 12:43
So use QChar instead of const char :) And make a conversion to some int type in the switch so you can make case labels. You have latin1 and unicode conversions available.