You are not logged in.

1

Sunday, January 9th 2005, 5:38pm

Math Operations with QString in a loop

Hi,
I've been trying to do this for hours without any luck. Maybe someone here can give me a hand.
I have a QString with a number like: 12010
Now i need to do the following math operation: Add by 2 numbers, so, 12010 becomes something like this:
12010
3211
532
85
So, my final number is a 2 digit number.
So far, i have tried doing a loop:

Source code

1
2
3
4
for( int i = 0; i < mystring.length(); i++ )
{
string.at(i) = string.at(i+1)  // and things like that, but without any luck. 
}


I'm really out of ideas of how can i do this.
Thanks a lot in advance.

Latem

Intermediate

  • "Latem" is male

Posts: 278

Location: New Brunswick, Canada

Occupation: Student/Programmer

  • Send private message

2

Monday, January 10th 2005, 2:18pm

You arent converting your characters to numbers

after using QString::at(), you get a QChar, and then you need to use QChar::digitValue(), to get the integer value, so you can add the numbers.

Also your logic is wrong, and you need two for loops for sure.
I don't have the time to think at out completely now, but I know that your for loop should not give you the answer like you described.

Latem
The march of progress:
C:
printf("%10.2f", x);
C++:
cout << setw(10) << setprecision(2) << showpoint << x;
Java:
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

3

Wednesday, January 12th 2005, 2:07am

Hi Latem.
I tried doing something simpler, converting the qstring to a simple c++ integer ( int x = string.toInt(); and I tried out several times, different ideas I had, but I still can't do it.
I know that my logic is wrong, because I'm not getting the expected results, but i'm really really out of ideas. I can't really think of a method for doing this.
I tried looking at a code made in C which is for a similar app, but i don't really understand it. Maybe someone else understands it and can help me in doing it in c++.
The code is like this:

Source code

1
2
3
4
5
6
7
8
char scr[64], tmp[64]="", *tmp2;
int sc, i, n=0;
while(strlen(scr)>2) {
while(scr[i+1] && scr[i+1]!='\n') {
[B]sc=(scr[i]-48)+(scr[i+1]-48);[/B]
i++;
}
}


Thanks a lot in advance.

mick

Trainee

Posts: 55

Location: Vancouver, BC, Canada

Occupation: Biologist

  • Send private message

4

Wednesday, January 12th 2005, 2:46am

RE: Math Operations with QString in a loop

Hi,

Here is one way of doing it:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
QString start = "12010";
QString next = "";

while(start.length()>2) {
    for (int i=0; i<start.length()-1; i++) {
      int firstPos = start[i].digitValue();
      int secondPos = start[i+1].digitValue();
      next += QString::number(firstPos+secondPos);
    };
  start = next;
  next = "";
};

bool ok;
int result = start.toInt(&ok,10);


the int "result" now contains the two-digit number you want.

However be warned - your algorithm won't always generate a two-digit number. For example try doing it starting with "99999". Each iteration will make the number longer rather than shorter.

So you need to rethink the logic of you algorithm and decide what to do when each iteration of the algorithm makes the string get longer rather than shorter; obviously this depends what the purpose of the algorithm is.

I hope this helps,

Mick

This post has been edited 1 times, last edit by "mick" (Jan 12th 2005, 2:53am)


5

Wednesday, January 12th 2005, 4:52am

mick,
THANK YOU VERY, VERY MUCH! I've been trying to do this for days without luck. Thank you very much for your help.
Your code worked great and about the algorithm, I will do a if statement inside the loop telling it that if the number is over 30 digits, abort.
Again, thank you very very much.

Greetings,
Mauricio.