-
Notifications
You must be signed in to change notification settings - Fork 0
/
tp1.c
213 lines (188 loc) · 4.05 KB
/
tp1.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
# define MAX_COLUMN 80
unsigned int_width(double i)
{
unsigned nb = 0;
if (i > 0)
{
nb = log10(i) + 1;
}
else if (i < 0)
{
i = i * (-1);
nb = log10((double)i) + 2;
}
else
{
nb = 1;
}
return nb;
}
//------------------------------------------------------
unsigned ints_width(const int* tab, unsigned count)
{
unsigned nb[count];
unsigned max_nb = 0;
if(tab != NULL)
{
for (unsigned i = 0; i < count; ++i)
{
nb[i] = int_width(tab[i]);
#if 0
( if (tab[i] > 0)
{
nb[i] = log10(tab[i]) + 1;
}
else if (i < 0)
{
i = (-1) * i;
nb[i] = log10(tab[i]) + 2;
}
else
{
nb[i] = 1;
}
#endif
}
for(unsigned i = 0; i < count; ++i)
{
if(max_nb < nb[i])
{
max_nb = nb[i];
}
}
return max_nb;
}
return -1;
}
//----------------print_int_array----------------------------------------
void print_int_array(FILE* out, const int* tab, unsigned count)
{
//int column = 0;
int max_size = ints_width(tab, count);
//printf("maxsize is %i\n", max_size);
for (int i = 0; i < int_width(count - 1) - 1; i++)
{
fprintf(stdout, " ");
//column++;
}
fprintf(stdout, "[0]");
// column = column + 3;
int j;
for(int i = 0; i < count; i++)
{
//换行条件: 当x <= 80/最大size+1 - [x]占用格数
for (int x = 1; x <= (MAX_COLUMN / (max_size + 1)); x++)
{
if ((MAX_COLUMN / (max_size + 1) - 1) * x == i)
{
//column = 0;
printf("\n");
for(j = 0; j < int_width(count - 1)- 1 - int_width(j); j++)
{
fprintf(stdout, " ");
// column++;
}
fprintf(stdout, "[%i]", i);
//column = column + 3;
}
}
int space = max_size - int_width(tab[i]) + 1;
for (int s = 0; s < space; s++)
{
fprintf(stdout, " ");
//column ++;
}
fprintf(stdout, "%i", tab[i]);
//column = column + max_size;
}
printf("\n");
}
void insert_sort(int* tab, unsigned count)
{
int i, j;
int temp;
for(i = 0; i < count; ++i)
{
temp = *(tab + i);
for (j = i; j > 0 && *(tab + j - 1) > temp; j--)
{
*(tab + j) = *(tab + j - 1);
}
*(tab + j) = temp;
}
}
void insert_sort_cmp(int* tab, unsigned count, int (*cmp)(int a, int b))
{
double temp = 0;
int j, i = 0;
for(i = 0; i < count; i++)
{
temp = *(tab + 1);
for(j = i; j > 0 && cmp(*(tab + j - 1), *(tab + j)); j--)
{
*(tab + j) = *(tab + j - 1);
}
*(tab + j) = temp;
}
}
double increasing(double a, double b)
{
if (a < b)
return -1;
return a > b;
}
double decreasing(double a, double b)
{
if (a > b)
return -1;
return a < b;
}
int main (void)
{
#if 0
int a[] = {-42, -1, 0, 13, 9999, 10000, 10001};
for (unsigned i = 0; i < sizeof(a) / sizeof(*a); ++i)
printf("int_width(%d) == %d\n", a[i], int_width(a[i]));
int a[] = {
1, 2, 3, 4, 5, 6, 13, -35, 129, -4, 123, -4555, 1341, 2432, 111, 0, 1230};
printf("%d\n", ints_width(a, sizeof(a)/sizeof(*a)));
a[3] = 99999999;
printf("%d\n", ints_width(a, sizeof(a)/sizeof(*a)));
#endif
int a[] = {
1, 2, 3, 4, 5, 6, 13, -35, 129, -4, 123, -4555, 1341, 2432, 111, 0, 1230
};
int b[] = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6
};
puts("-- a[]");
print_int_array(stdout, a, sizeof(a)/sizeof(*a));
puts("\n-- b[]");
print_int_array(stdout, b, sizeof(b)/sizeof(*b));
puts("\n-- b[] after b[8] = 123456");
b[8] = 123456;
print_int_array(stdout, b, sizeof(b)/sizeof(*b));
puts("\n-- a[0..9]");
print_int_array(stdout, a, 10);
puts("\n-- a[0..10]");
print_int_array(stdout, a, 11);
#if 0
int a[] = {
1, 2, 3, 4, 5, 6, 13, -35, 129, -4, 123, -4555, 1341, 2432, 111, 0, 1230
};
unsigned asize = sizeof(a)/sizeof(*a);
puts("before");
print_int_array(stdout, a, asize);
insert_sort(a, asize);
puts("after");
print_int_array(stdout, a, asize);
#endif
return 0;
}