-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
80 lines (69 loc) · 1.65 KB
/
gen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include "convert.h"
#define BASE 999
#define MIDDLE (BASE * 2)
#define HIGH (BASE * 3)
typedef struct {
unsigned int num;
unsigned int len;
} WORDS;
const char* defstr= "#define BASE 999\n"
"#define MIDDLE (BASE * 2)\n"
"#define HIGH (BASE * 3)\n"
"\n"
"typedef struct {\n"
" unsigned int num;\n"
" unsigned int len;\n"
"} WORDS;\n"
"\n";
unsigned int length(unsigned int v)
{
char tmp[max_words_length];
number_to_words(tmp,v);
return strlen(tmp);
}
int compare(const void *a, const void *b)
{
char ta[max_words_length], tb[max_words_length];
number_to_words(ta,((WORDS*) a)->num);
number_to_words(tb,((WORDS*) b)->num);
return strcmp(ta,tb);
}
void dump(const WORDS* table, const unsigned int size, const char *name)
{
const int stride = 6;
printf("WORDS %s[] = {\n", name);
for (int i=0; i<size; i+=stride) {
printf(" ");
for (int x=i; x<size && x<i+stride; ++x)
printf("{%d,%d}%s", table[x].num,
table[x].len,
x+1<size ? "," : "");
printf("\n");
}
printf("};\n");
}
void generate_data()
{
WORDS low[BASE], mid[MIDDLE], hi[HIGH];
for (int i=0,l=1,m=1000,h=1000000;i<BASE;++i,l+=1,m+=1000,h+=1000000) {
low[i].num=l; low[i].len=length(l);
mid[i+BASE].num=m; mid[i+BASE].len=length(m);
hi[i+MIDDLE].num=h; hi[i+MIDDLE].len=length(h);
hi[i]=low[i]; hi[i+BASE]=mid[i+BASE];
mid[i]=low[i];
}
qsort(low, BASE, sizeof(WORDS), compare);
qsort(mid, MIDDLE, sizeof(WORDS), compare);
qsort(hi, HIGH, sizeof(WORDS), compare);
printf("%s", defstr);
dump(low,BASE,"base");
dump(mid,MIDDLE,"middle");
dump(hi,HIGH,"high");
}
int main()
{
generate_data();
return 0;
}