目前正在努力更新中

函数题没有上传上去

实验4

编程题

7.1

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
#include <stdio.h>
int main()
{
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++)
{
int a, b;
scanf("%d %d", &a, &b);
int SA = 0, SB = 0;
int temp_a = a;
int temp_b = b;
while (temp_a != 0) {
SA += temp_a % 10;
temp_a /= 10;
}

while (temp_b != 0) {
SB += temp_b % 10;
temp_b /= 10;
}
if (a % SB == 0&&b % SA != 0) {
printf("A\n");
} else if (b % SA == 0&&a % SB != 0) {
printf("B\n");
} else if (a > b) {
printf("A\n");
} else {
printf("B\n");
}
}

return 0;
}

7.2

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
#include <stdio.h>
#include <math.h>
int fun1(int a);
int fun2(int a);
int fun3(int a);

int main()
{
int N,a=0,b=0;
scanf("%d",&N);
if(fun1(N)==1)
{
a=fun2(N);
printf("%d",a);
}
else{
b=fun3(N);
printf("%d",b);
}
}
int fun1(int n) {
if (n <= 1) {
return 0;
}
if (n == 2) {
return 1;
}
if (n % 2 == 0) {
return 0;
}
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int fun2(int a)
{
int sum=0;
while(a!=0)
{
sum+=a%10;
a=a/10;
}
return sum;

}

int fun3(int a)
{
if (a == 0)
{
return 0;
}
int sum=1;
while(a!=0)
{
sum*=a%10;
a=a/10;
}
return sum;

}

7.3

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
#include<stdio.h>
#include <limits.h>

int commondivisor(int a,int b){
int temp;
a=a>0?a:-a;
b=b>0?b:-b;
while(b!=0){
temp=b;
b=a%b;
a=temp;
}
return a;
}

int commonmultiple(int x,int y){
int a=x*y/commondivisor(x,y);
if(x<0&&y<0){
return -a;
}else{
return a;
}
}

int main()
{
int m,n;
while(scanf("%d %d",&m,&n)!=EOF){
if(m==0||n==0)
{
return 0;
}
if (n > 0 && m > 0)
{
if (n > INT_MAX / m)
{
continue;
}
}
printf("%d %d\n",commondivisor(m,n),commonmultiple(m,n));
}
return 0;
}

7.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int ackermann(int m, int n) {
if (m == 0) {
return n + 1;
} else if (n == 0) {
return ackermann(m - 1, 1);
} else {
return ackermann(m - 1, ackermann(m, n - 1));
}
}

int main() {
int m, n;
scanf("%d %d", &m, &n);
int result = ackermann(m, n);
printf("%d\n", result);
return 0;
}

7.5

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
#include <stdio.h>
void Title();
void Lyric();
void Win();
int main() {
Win();
return 0;}
void Title() {
printf("============\n");
printf(" 爱拼才会赢\n");
printf("============\n");
printf("词曲:陈百潭\n");
printf("演唱:叶启田\n");
}
void Lyric() {
printf("一时失志不免怨叹\n");
printf("一时落魄不免胆寒\n");
printf("那通失去希望\n");
printf("每日醉茫茫\n");
printf("无魂有体亲像稻草人\n");
printf("人生可比是海上的波浪\n");
printf("有时起有时落\n");
printf("好运歹运\n");
printf("总嘛要照起工来行\n");
printf("三分天注定\n");
printf("七分靠打拼\n");
printf("爱拼才会赢\n");
}
void Win() {
Title();
printf("\n");
Lyric();
printf("\n");
Lyric();
printf("\n");
Lyric();
}

7.6

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
#include <stdio.h>
float max(float d1, float d2, float d3);
int main() {
int n;
scanf("%d", &n);
float best_overall_score = 0.0;
int best_player_id = 0;
for (int i = 1; i <= n; i++) {
float s1, s2, s3;
scanf("%f %f %f", &s1, &s2, &s3);
float current_best = max(s1, s2, s3);
if (current_best > best_overall_score) {
best_overall_score = current_best;
best_player_id = i;
}
}

printf("%d %.2f\n", best_player_id, best_overall_score);

return 0;
}

float max(float d1, float d2, float d3) {
float temp_max = d1;

if (d2 > temp_max) {
temp_max = d2;
}
if (d3 > temp_max) {
temp_max = d3;
}

return temp_max;
}

作业4

6.1

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


int MaxDayMonth(int year, int month)
{
if (year <= 0 || month < 1 || month > 12) {
return 0;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if (IsLeapYear(year)) {
return 29;
} else {
return 28;
}
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 0;
}
}

6.2

1
2
3
4
5
6
7
double result;
if (x != 5) {
result = 2 / fabs(5 - x);
} else {
result = exp(x);
}
return result;

6.3

1
2
3
4
5
6
7
8
9
10
int getDigit(long long n)
{
int count = 0;
do {
count++;
n /= 10;
} while (n > 0);

return count;
}

6.4

1
2
3
4
5
6
7
int fun(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fun(n - 1) + fun(n - 2);
}
}

实验5

7-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
int main() {
int mood[24];
int i, query;
for (i = 0; i < 24; i++) {
scanf("%d", &mood[i]);
}
while (scanf("%d", &query) == 1) {
if (query < 0 || query > 23) {
break;
}
if (mood[query] > 50) {
printf("%d Yes\n", mood[query]);
} else {
printf("%d No\n", mood[query]);
}
}

return 0;
}

7-2

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
#include <stdio.h>
#include <stdlib.h>

int main() {
int a1, a2, n;
if (scanf("%d %d %d", &a1, &a2, &n) != 3) {
return 0;
}

if (n == 0) {
return 0;
}
if (n == 1) {
printf("%d\n", a1);
return 0;
}
int *seq = (int*)malloc(n * sizeof(int));
if (seq == NULL) {
return 1;
}
seq[0] = a1;
seq[1] = a2;
int size = 2;
int current_index = 0;

while (size < n) {
int product = seq[current_index] * seq[current_index + 1];

if (product >= 10) {
seq[size] = product / 10;
size++;

if (size < n) {
seq[size] = product % 10;
size++;
}
} else {
seq[size] = product;
size++;
}
current_index++;
}

for (int i = 0; i < n; ++i) {
printf("%d%c", seq[i], (i == n - 1 ? '\n' : ' '));
}
free(seq);

return 0;
}

7-3

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
#include <stdio.h>
#include <stdlib.h>

int cmp(const void *a, const void *b) { return *(int*)b - *(int*)a; }

int main() {
int n, k, s, t, p, i, j;
if (scanf("%d%d%d", &n, &k, &s) != 3) return 1;

int T[305] = {0}, P[305] = {0}, U[305], idx = 0;
for (i = 0; i < n; i++) {
scanf("%d%d", &t, &p);
if (t >= 175) {
if (!T[t]) U[idx++] = t;
T[t]++;
if (p >= s) P[t]++;
}
}

qsort(U, idx, sizeof(int), cmp);

int B[k];
for (i = 0; i < k; i++) B[i] = 301;

long long ans = 0;
for (i = 0; i < idx; i++) {
int sc = U[i], tot = T[sc], pat = P[sc];
qsort(B, k, sizeof(int), cmp);

int avl = 0;
for (j = 0; j < k; j++)
if (B[j] > sc) avl++; else break;

int base = tot < avl ? tot : avl;
int np = tot - pat;
int use_np = base < np ? base : np;

ans += base + (pat - (base - use_np));

for (j = 0; j < base; j++) B[j] = sc;
}

printf("%lld\n", ans);
return 0;
}

7-4

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
#include <stdio.h>

int main() {
int n, k, x, i, j;
int c[1001] = {0};
int ans = 0, mx = 0;

scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &k);
for (j = 0; j < k; j++) {
scanf("%d", &x);
c[x]++;
}
}

for (i = 1; i <= 1000; i++) {
if (c[i] >= mx) {
mx = c[i];
ans = i;
}
}

printf("%d %d\n", ans, mx);
return 0;
}

7-5

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
#include <stdio.h>
#include <stdlib.h>

int main() {
int ra, ca, rb, cb, i, j, k;

scanf("%d%d", &ra, &ca);
int *A = (int*)malloc(ra * ca * sizeof(int));
for (i = 0; i < ra * ca; i++) scanf("%d", &A[i]);

scanf("%d%d", &rb, &cb);
int *B = (int*)malloc(rb * cb * sizeof(int));
for (i = 0; i < rb * cb; i++) scanf("%d", &B[i]);

if (ca != rb) {
printf("Error: %d != %d\n", ca, rb);
return 0;
}

int *C = (int*)calloc(ra * cb, sizeof(int));
for (i = 0; i < ra; i++)
for (j = 0; j < cb; j++)
for (k = 0; k < ca; k++)
C[i * cb + j] += A[i * ca + k] * B[k * cb + j];

printf("%d %d\n", ra, cb);
for (i = 0; i < ra; i++) {
for (j = 0; j < cb; j++)
printf("%d%c", C[i * cb + j], j == cb - 1 ? '\n' : ' ');
}

free(A); free(B); free(C);
return 0;
}

7-6

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
#include <stdio.h>

int main() {
int a[6][6], n;
if (scanf("%d", &n) != 1) return 0;
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}

int k = 0, y = 0, flag = 1, p = 0;
if (n == 1) {
printf("0 0");
} else {
for (i = 0; i < n; i++) {
y = i;
k = 0;

for (p = 0; p < n; p++) {
if (a[i][k] <= a[i][p]) {
k = p;
}
}
for (j = 0; j < n; j++) {
if (a[y][k] > a[j][k]) {
y = j;
break;
}
}

if (i == y) {
flag = 0;
break;
}
}

if (flag == 0)
printf("%d %d", i, k);
else
printf("NONE");
}

return 0;
}

7-7

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
#include <stdio.h>

int main() {
int n;
if (scanf("%d", &n) != 1) return 0;

while (n--) {
int ok = 1;
int r[9][10] = {0}, c[9][10] = {0}, b[9][10] = {0};
int i, j, x;

for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
scanf("%d", &x);
if (ok) {
int k = (i / 3) * 3 + (j / 3);
if (x < 1 || x > 9 || r[i][x] || c[j][x] || b[k][x]) {
ok = 0;
} else {
r[i][x] = c[j][x] = b[k][x] = 1;
}
}
}
}
printf("%d\n", ok);
}
return 0;
}

作业5

7-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
int main() {
int N;
scanf("%d", &N);
if (N <= 0) {
return 0;
}

int a[N];
int max_value;
int max_index = 0;
scanf("%d", &a[0]);
max_value = a[0];
for (int i = 1; i < N; i++) {
scanf("%d", &a[i]);
if (a[i] > max_value) {
max_value = a[i];
max_index = i;
}
}
printf("%d %d\n", max_value, max_index);
return 0;
}

7-2

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
#include <stdio.h>

int main() {
int n;
scanf("%d", &n);
int max_total_score = 0;
int max_chinese_score = 0;
int max_math_score = 0;
int max_english_score = 0;
for (int i = 0; i < n; i++) {
int chinese, math, english;
scanf("%d %d %d", &chinese, &math, &english);
int current_total_score = chinese + math + english;
if (current_total_score > max_total_score) {
max_total_score = current_total_score;
}
if (chinese > max_chinese_score) {
max_chinese_score = chinese;
}
if (math > max_math_score) {
max_math_score = math;
}

if (english > max_english_score) {
max_english_score = english;
}
}
printf("%d %d %d %d\n", max_total_score, max_chinese_score, max_math_score, max_english_score);

return 0;
}

7-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main() {
int m, n;
scanf("%d %d", &m, &n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%4d", i + j);
}
printf("\n");
}

return 0;
}

实验6

7-1

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
#include <stdio.h>
#include <string.h>

int main()
{
int N;
char s[100];
if (scanf("%d", &N) != 1) return 0;
getchar();

while (N--)
{
if (fgets(s, 100, stdin) == NULL) break;
int len = strlen(s);
if (len > 0 && s[len - 1] == '\n')
{
s[len - 1] = '\0';
len--;
}

if (len < 6)
{
printf("Your password is tai duan le.\n");
}
else
{
int has_digit = 0;
int has_alpha = 0;
int has_illegal = 0;

for (int i = 0; i < len; i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
has_digit = 1;
}
else if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
{
has_alpha = 1;
}
else if (s[i] == '.')
{
// . is valid
}
else
{
has_illegal = 1;
}
}

if (has_illegal)
{
printf("Your password is tai luan le.\n");
}
else if (!has_digit)
{
printf("Your password needs shu zi.\n");
}
else if (!has_alpha)
{
printf("Your password needs zi mu.\n");
}
else
{
printf("Your password is wan mei.\n");
}
}
}
return 0;
}

7-2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main()
{
char ch;
while ((ch = getchar()) != '\n' && ch != EOF)
{
if (ch >= 'A' && ch <= 'Z')
{
ch = 'Z' - (ch - 'A');
}
putchar(ch);
}
putchar('\n');
return 0;
}

7-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
int main()
{
int ascii[128] = {0};
int ch;

while ((ch = getchar()) != '\n' && ch != EOF)
{
if (ch >= 0 && ch < 128)
{
ascii[ch] = 1;
}
}

for (int i = 0; i < 128; i++)
{
if (ascii[i])
{
putchar(i);
}
}

return 0;
}

7-4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()
{
char s[33];
if (scanf("%32s", s) != 1) return 0;

int parts[4] = {0};
for (int i = 0; i < 32; i++)
{
parts[i / 8] = parts[i / 8] * 2 + (s[i] - '0');
}

printf("%d.%d.%d.%d\n", parts[0], parts[1], parts[2], parts[3]);

return 0;
}

7-5

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
#include <stdio.h>

int main()
{
int N;
if (scanf("%d", &N) != 1) return 0;

int weight[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char m_code[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
int all_passed = 1;
char id[100];

for (int i = 0; i < N; i++)
{
scanf("%s", id);
int sum = 0;
int flag = 1;

for (int j = 0; j < 17; j++)
{
if (id[j] >= '0' && id[j] <= '9')
{
sum += (id[j] - '0') * weight[j];
}
else
{
flag = 0;
break;
}
}

if (flag)
{
int z = sum % 11;
if (m_code[z] != id[17])
{
flag = 0;
}
}

if (!flag)
{
printf("%s\n", id);
all_passed = 0;
}
}

if (all_passed)
{
printf("All passed\n");
}

return 0;
}

7-6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main()
{
int N;
if (scanf("%d", &N) != 1) return 0;

char name[20];
double base, bonus, deduct;

for (int i = 0; i < N; i++)
{
scanf("%s %lf %lf %lf", name, &base, &bonus, &deduct);
printf("%s %.2f\n", name, base + bonus - deduct);
}

return 0;
}

7-7

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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>

char Menu(const char *prompt, const char *item);
int YesNo(const char *question);
void GetStr(char *str);

char Menu(const char *prompt, const char *option)
{
char choice;
int err;
do
{
printf(prompt);
scanf(" %c", &choice);
choice = toupper(choice);
err = !strchr(option, choice);
if (err)
{
puts("Incorrect choice!");
}
}
while (err);
return choice;
}

int YesNo(const char *question)
{
char answer;
int err;
do
{
printf(question);
scanf(" %c", &answer);
answer = toupper(answer);
err = !strchr("YN", answer);
if (err)
{
puts("Incorrect answer!");
}
}
while (err);
return answer == 'Y';
}

void GetStr(char *str)
{
scanf(" %c", str);
gets(str + 1);
}

typedef struct
{
short year;
char month, day;
} DATE;

int IsLeapYear(int year);
int MaxDayMonth(int year, int month);
int IsValidDate(int year, int month, int day);
void DateInput(DATE *date);
void DateOutput(const DATE *date);
int DateCmp(const DATE *date1, const DATE *date2);

int IsLeapYear(int year)
{
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}

int MaxDayMonth(int year, int month)
{
const static char mdm[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return year > 0 && month >= 1 && month <= 12
? mdm[month - 1] + (2 == month && IsLeapYear(year)) : 0;
}

int IsValidDate(int year, int month, int day)
{
return year > 0 && month >= 1 && month <= 12
&& day >= 1 && day <= MaxDayMonth(year, month);
}

void DateInput(DATE *date)
{
int year = 0, month = 0, day = 0;
while (scanf("%d / %d / %d", &year, &month, &day) < 3
|| !IsValidDate(year, month, day))
{
printf("Incorrect date! Please reenter: ");
while (getchar() != '\n');
}
date->year = year;
date->month = month;
date->day = day;
}

void DateOutput(const DATE *date)
{
printf("%04d/%02d/%02d", date->year, date->month, date->day);
}

int DateCmp(const DATE *date1, const DATE *date2)
{
int r = date1->year - date2->year;
if (!r) r = date1->month - date2->month;
if (!r) r = date1->day - date2->day;
return r;
}

typedef enum _bookFmt_ {card, table} BOOKFMT;

typedef struct
{
char isbn[16];
char title[32];
char author[32];
char pub[32];
DATE press;
double price;
} BOOK;

void SetItem(int i);
int GetItem();
void SelectItem();
void SelectAll();

void Card();
void Table();
void Head();

void Input(BOOK *book);
void Output(const BOOK *book);

int BookCmp(const BOOK *book1, const BOOK *book2);

#define epsilon 1e-6

static int item;

void SetItem(int i)
{
if (i >= 1 && i <= 6)
{
item = i;
}
}

int GetItem()
{
return item;
}

void SelectItem()
{
item = Menu("Item: 1)ISBN 2)Title 3)Author 4)Publisher 5)Pub date 6)Price > ",
"123456") - '0';
}

void SelectAll()
{
item = 0;
}

static BOOKFMT bookFmt = card;

void Card()
{
bookFmt = card;
}

void Table()
{
bookFmt = table;
}

void Head()
{
puts("ISBN--------- Title------------------------- Author-------------- "
"Publisher--------------------- Pub-date-- Price---");
}

void Input(BOOK *book)
{
switch (item)
{
case 0:
printf(" ISBN: ");
GetStr(book->isbn);
printf(" Title: ");
GetStr(book->title);
printf(" Author: ");
GetStr(book->author);
printf("Publisher: ");
GetStr(book->pub);
printf(" Pub date: ");
DateInput(&book->press);
printf(" Price: ");
scanf("%lg", &book->price);
break;
case 1:
printf("ISBN: ");
GetStr(book->isbn);
break;
case 2:
printf("Title: ");
GetStr(book->title);
break;
case 3:
printf("Author: ");
GetStr(book->author);
break;
case 4:
printf("Publisher: ");
GetStr(book->pub);
break;
case 5:
printf("Pub date: ");
DateInput(&book->press);
break;
case 6:
printf("Price: ");
scanf("%lg", &book->price);
break;
}
}

void Output(const BOOK *book)
{
switch (bookFmt)
{
case card:
switch (item)
{
case 0:
printf(" ISBN: %s\n", book->isbn);
printf(" Title: %s\n", book->title);
printf(" Author: %s\n", book->author);
printf("Publisher: %s\n", book->pub);
printf(" Pub date: ");
DateOutput(&book->press);
putchar('\n');
printf(" Price: %.2f\n", book->price);
break;
case 1:
printf("ISBN: %s\n", book->isbn);
break;
case 2:
printf("Title: %s\n", book->title);
break;
case 3:
printf("Author: %s\n", book->author);
break;
case 4:
printf("Publisher: %s\n", book->press);
break;
case 5:
printf("Pub date: ");
DateOutput(&book->press);
putchar('\n');
break;
case 6:
printf("Price: %.2f\n", book->price);
break;
}
break;
case table:
printf("%-13s %-30s %-20s %-30s ",
book->isbn, book->title, book->author, book->pub);
DateOutput(&book->press);
printf(" %8.2f\n", book->price);
break;
}
}

int BookCmp(const BOOK *book1, const BOOK *book2)
{
int ok;
switch (item)
{
case 1:
ok = strcmp(book1->isbn, book2->isbn);
break;
case 2:
ok = strcmp(book1->title, book2->title);
break;
case 3:
ok = strcmp(book1->author, book2->author);
break;
case 4:
ok = strcmp(book1->pub, book2->pub);
break;
case 5:
ok = DateCmp(&book1->press, &book2->press);
break;
case 6:
if (fabs(book1->price - book2->price) < epsilon)
{
ok = 0;
}
else if (book1->price > book2->price)
{
ok = 1;
}
else
{
ok = -1;
}
break;
}
return ok;
}

#define LibSize 1024
#define LibInc 1024

typedef struct
{
int size, number;
BOOK *book;
} LIB;

void Create(LIB *lib);
void Destroy(LIB *lib);
void Append(LIB *lib);
void Find(LIB *lib);
void Remove(LIB *lib);
void Modify(LIB *lib);
void Show(LIB *lib);

static void Resize(LIB *lib, int size);
static int Search(const LIB *lib, const BOOK *book, int start);
static void Sort(LIB *lib);

void Create(LIB *lib)
{
lib->size = LibSize;
lib->number = 0;
lib->book = (BOOK*)malloc(lib->size * sizeof(BOOK));
}

void Destroy(LIB *lib)
{
lib->size = lib->number = 0;
free(lib->book);
}

void Append(LIB *lib)
{
BOOK x;
SelectAll();
Input(&x);
if (lib->number == lib->size)
{
Resize(lib, lib->size + LibInc);
}
lib->book[lib->number] = x;
++lib->number;
}

void Find(LIB *lib)
{
int k, r, i;
BOOK book;
SetItem(2);
Input(&book);
r = Search(lib, &book, 0);
Table();
if (r < 0)
{
puts("Not found!");
}
else
{
Head();
}
while (r >= 0)
{
Output(&lib->book[r]);
r = Search(lib, &book, r + 1);
}
}

void Remove(LIB *lib)
{
int i, r = 0;
char answer;
BOOK book;
SetItem(1);
Input(&book);
r = Search(lib, &book, r);
if (r < 0)
{
puts("Not found!");
}
while (r >= 0)
{
if (YesNo("Remove(y/n)? "))
{
--lib->number;
if (r < lib->number)
{
lib->book[r] = lib->book[lib->number];
}
}
else
{
++r;
}
SetItem(1);
r = Search(lib, &book, r);
}
}

void Modify(LIB *lib)
{
int i, r;
char answer;
BOOK book;
SetItem(1);
Input(&book);
r = Search(lib, &book, 0);
if (r < 0)
{
puts("Not found!");
}
while (r >= 0)
{
if (YesNo("Modify(y/n)? "))
{
SelectAll();
Input(&lib->book[r]);
r = -1;
}
else
{
SetItem(1);
r = Search(lib, &book, r + 1);
}
}
}

void Show(LIB *lib)
{
int k;
SetItem(1);
Sort(lib);
Head();
Table();
SelectAll();
for (k = 0; k < lib->number; ++k)
{
Output(&lib->book[k]);
}
}

void Resize(LIB *lib, int size)
{
BOOK *book;
int k;
if (size != lib->size && size > lib->number)
{
lib->size = size;
book = (BOOK*)malloc(lib->size * sizeof(BOOK));
for (k = 0; k < lib->number; ++k)
{
book[k] = lib->book[k];
}
free(lib->book);
lib->book = book;
}
}

int Search(const LIB *lib, const BOOK *book, int start)
{
int k, r = -1;
for (k = start; k < lib->number; ++k)
{
if (!BookCmp(&lib->book[k], book))
{
r = k;
k = lib->number;
}
}
return r;
}

void Sort(LIB *lib)
{
int i, j;
BOOK book;
for (i = 1; i < lib->number; ++i)
{
if (BookCmp(&lib->book[i], &lib->book[i - 1]) < 0)
{
book = lib->book[i];
for (j = i - 1; j >= 0 && BookCmp(&book, &lib->book[j]) < 0; --j)
{
lib->book[j + 1] = lib->book[j];
}
lib->book[j + 1] = book;
}
}
}

int main()
{
char choice;
LIB lib;
Create(&lib);
do
{
choice = Menu("Append Find Remove Modify Show Quit > ", "AFRMSQ");
switch (choice)
{
case 'A':
Append(&lib);
break;
case 'F':
Find(&lib);
break;
case 'R':
Remove(&lib);
break;
case 'M':
Modify(&lib);
break;
case 'S':
Show(&lib);
break;
case 'Q':
break;
}
}
while (choice != 'Q');
Destroy(&lib);
return 0;
}