-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq3_2.c
75 lines (60 loc) · 1.31 KB
/
q3_2.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct record {
int no;
char name[10];
int point;
struct record *next;
};
void insert_list(int no, char *name, int x);
struct record *head = NULL;
int main(int argc, char *argv[])
{
FILE *fp;
int no, x;
char name[10], buf[256];
struct record *p;
if (argc != 2) {
printf("missing file argument\n");
return 1;
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("can't open %s\n", argv[1]);
return 1;
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
sscanf(buf, "%d %s %d", &no, name, &x);
insert_list(no, name, x);
}
fclose(fp);
for (p = head; p != NULL; p = p->next)
printf("%d %s %d\n", p->no, p->name, p->point);
return 0;
}
void insert_list(int no, char *name, int x)
{
struct record *p, *q, *t;
if (x < 60)
return;
t = (struct record *)malloc(sizeof(struct record));
if (t == NULL) {
printf("Out of memory\n");
exit(1);
}
t->no = no;
strcpy(t->name, name);
t->point = x;
q = NULL;
for (p = head; p != NULL; p = p->next) {
if (p->point < x)
break;
q = p;
}
if (q != NULL)
q->next = t;
else
head = t;
t->next = p;
}