1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5
6 #define N 80
7 #define PICK 5
8 #define LEN 128
9
10 typedef struct {
11 long id;
12 char name[32];
13 char class_name[64];
14 } Student;
15
16 int read_students(Student st[], int maxn);
17 void make_date_filename(char filename[], int size);
18 void pick_students(int selected[], int n, int count);
19 void sort_by_id(Student st[], int n);
20 void output_students(FILE *fp, Student st[], int n);
21
22 int main(int argc, char *argv[]) {
23 Student students[N], winners[PICK];
24 int selected[PICK];
25 int n, i;
26 char filename[LEN];
27 FILE *fout;
28
29 n = read_students(students, N);
30 if (n < PICK) {
31 printf("学生人数不足,无法抽取%d位中奖名单\n", PICK);
32 return 1;
33 }
34
35 if (argc > 1) {
36 strncpy(filename, argv[1], LEN - 1);
37 filename[LEN - 1] = '\0';
38 } else {
39 printf("请输入保存中奖名单的文件名(直接回车则使用系统日期): ");
40 if (fgets(filename, LEN, stdin) == NULL)
41 filename[0] = '\0';
42
43 filename[strcspn(filename, "\r\n")] = '\0';
44 if (filename[0] == '\0')
45 make_date_filename(filename, LEN);
46 }
47
48 srand((unsigned int)time(NULL) ^ (unsigned int)clock());
49 pick_students(selected, n, PICK);
50
51 for (i = 0; i < PICK; i++)
52 winners[i] = students[selected[i]];
53
54 sort_by_id(winners, PICK);
55
56 fout = fopen(filename, "w");
57 if (fout == NULL) {
58 printf("fail to open file\n");
59 return 1;
60 }
61
62 printf("中奖名单如下:\n");
63 output_students(stdout, winners, PICK);
64
65 output_students(fout, winners, PICK);
66 fclose(fout);
67
68 printf("中奖名单已写入文件: %s\n", filename);
69
70 return 0;
71 }
72
73 int read_students(Student st[], int maxn) {
74 int n = 0;
75 FILE *fin;
76
77 fin = fopen("list.txt", "r");
78 if (fin == NULL) {
79 printf("fail to open file\n");
80 return 0;
81 }
82
83 while (n < maxn &&
84 fscanf(fin, "%ld %31s %63s", &st[n].id, st[n].name,
85 st[n].class_name) == 3) {
86 ++n;
87 }
88
89 fclose(fin);
90 return n;
91 }
92
93 void make_date_filename(char filename[], int size) {
94 time_t now;
95 struct tm *local;
96
97 now = time(NULL);
98 local = localtime(&now);
99 if (local == NULL || strftime(filename, (size_t)size, "%Y%m%d.txt", local) == 0)
100 strncpy(filename, "winners.txt", (size_t)size);
101
102 filename[size - 1] = '\0';
103 }
104
105 void pick_students(int selected[], int n, int count) {
106 int flag[N] = {0};
107 int i = 0;
108 int index;
109
110 while (i < count) {
111 index = rand() % n;
112 if (!flag[index]) {
113 flag[index] = 1;
114 selected[i] = index;
115 ++i;
116 }
117 }
118 }
119
120 void sort_by_id(Student st[], int n) {
121 int i, j;
122 Student temp;
123
124 for (i = 0; i < n - 1; i++) {
125 for (j = 0; j < n - 1 - i; j++) {
126 if (st[j].id > st[j + 1].id) {
127 temp = st[j];
128 st[j] = st[j + 1];
129 st[j + 1] = temp;
130 }
131 }
132 }
133 }
134
135 void output_students(FILE *fp, Student st[], int n) {
136 int i;
137
138 fprintf(fp, "学号\t\t姓名\t班级\n");
139 for (i = 0; i < n; i++)
140 fprintf(fp, "%ld\t%s\t%s\n", st[i].id, st[i].name, st[i].class_name);
141 }