-
Notifications
You must be signed in to change notification settings - Fork 0
/
219.c
76 lines (67 loc) · 1.64 KB
/
219.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
// http://www.bjfuacm.com/problem/219/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OVERFLOW 11
struct Books;
typedef struct Books Book;
typedef struct Books* PtrToBook;
typedef PtrToBook List;
typedef PtrToBook Position;
struct Books {
char no[20];
char name[50];
float price;
struct Books* next;
};
void InitList(List* L) {
*L = (PtrToBook)calloc(1, sizeof(Book));
if (!*L) exit(OVERFLOW);
(*L)->next = NULL;
return;
}
int main() {
List L;
InitList(&L);
int count;
Position post, pre;
scanf("%d", &count);
for (int i = 0; i < count; i++) {
post = (PtrToBook)calloc(1, sizeof(Book));
if (!post) exit(OVERFLOW);
scanf("%s %s %f", post->no, post->name, &post->price);
if (i == 0) {
L->next = post;
} else {
pre->next = post;
}
pre = post;
}
pre->next = NULL;
int cnt;
int query;
scanf("%d", &query);
while (query--) {
cnt = 0;
Position p = L->next;
PtrToBook* ans = (PtrToBook*)calloc(count, sizeof(PtrToBook));
char test[50];
scanf("%s", test);
while (p) {
if (!strcmp(p->name, test)) {
ans[cnt] = p;
cnt += 1;
}
p = p->next;
}
if (cnt == 0) {
printf("%s\n", "Sorry,there is no your favourite!");
} else {
printf("%d\n", cnt);
for (int i = 0; i < cnt; i++) {
printf("%s %s %.2f\n", (*(ans + i))->no, (*(ans + i))->name, (*(ans + i))->price);
}
}
free(ans);
}
}