Hi,
I wanted to have a numbering scheme for columns like the one provided by Office calculation programs.
My approach so far is this:
#include <QList>
#include <QStringList>
#include <QDebug>
QString getLabelFromInt
(uint digit
) {
uint base = 26;
uint remainder;
uint quotient = digit;
QList<int> l;
do {
remainder = quotient % base;
quotient = quotient / base;
l.prepend(remainder);
} while (quotient != 0);
for (int i = 0; i < l.size(); ++i)
strList <<
QString(char(l
[i
] + 'a'));
return strList.join("");
}
int main(int argc, char** arg) {
for (int i = 0; i < 200; ++i)
qDebug() << getLabelFromInt(i);
}
#include <QList>
#include <QStringList>
#include <QDebug>
QString getLabelFromInt(uint digit)
{
uint base = 26;
uint remainder;
uint quotient = digit;
QList<int> l;
do {
remainder = quotient % base;
quotient = quotient / base;
l.prepend(remainder);
} while (quotient != 0);
QStringList strList;
for (int i = 0; i < l.size(); ++i)
strList << QString(char(l[i] + 'a'));
return strList.join("");
}
int main(int argc, char** arg) {
for (int i = 0; i < 200; ++i)
qDebug() << getLabelFromInt(i);
}
To copy to clipboard, switch view to plain text mode
but it generates a-z but then continues with ba - zz and then baa - zzz. Considering an alphabet with two symbols (0 and 1) it would have to go like this:
0
1
00
01
10
11
000
001
010
011
100
101
110
111
instead of
0
1
10
11
100
101
110
111
Of course I see the pattern: the first one has 2^3+2^2+2^1 elements (since it includes the preceding combinations) while the second (normal) one has 2^3 elements but I can't put the pieces together.
Is there a simple way to implement this?
Thanx in advance
momesana
Bookmarks