-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experiment7.c
483 lines (463 loc) · 14.4 KB
/
Experiment7.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
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
#include "stdio.h"
#include "string.h"
//程序末尾附测试用例
/// @brief 学生人数
int Number_ = 0;
/// @brief 课程数目
int Course_ = 0;
struct students
{
unsigned long long int id;
char name[21];
double score[6];
double total;
double average;
};
static struct students student[30];
void InputRecord(); //1 输入所有学生信息
void CaculateEveryCourses(); //2 计算每门课程的总分和平均分
void CaculateEveryStudent(); //3 计算每个学生的总分和平均分
void SortByTotal(); //4 按学生总分从高到低进行排序
void SortByNumber(); //5 按学生学号从高到低排序
void SortByDictionary(); //6 按首字母ASCII排序(词典写着太复杂了)
void SearchByNumber(); //7 按学号查找学生信息
void SearchByName(); //8 按姓名查找学生信息
void CourseStatistic(); //9 对每门课程进行成绩统计
void RecordList(); //10 输出所有学生记录信息
int main()
{
int choice = 0;
printf("1. Input record\n"
"2. Caculate total and average score of every course\n"
"3. Caculate total and average score of every student\n"
"4. Sort in descending order by total score of every student\n"
"5. Sort in ascending order by number\n"
"6. Sort in dictionary order by name\n"
"7. Search by number\n"
"8. Search by name\n"
"9. Statistic analysis for every course\n"
"10. List record \n"
"0. Exit\n");
do
{
printf("Please input your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input record\n");
InputRecord();
break;
case 2:
printf("Caculate total and average score of every course\n");
CaculateEveryCourses();
break;
case 3:
printf("Caculate total and average score of every student\n");
CaculateEveryStudent();
break;
case 4:
printf("Sort in descending order by total score of every student\n");
SortByTotal();
break;
case 5:
printf("Sort in ascending order by number\n");
SortByNumber();
break;
case 6:
printf("Sort in dictionary order by name\n");
SortByDictionary();
break;
case 7:
printf("Search by number\n");
SearchByNumber();
break;
case 8:
printf("Search by name\n");
SearchByName();
break;
case 9:
printf("Statistic analysis for every course\n");
CourseStatistic();
break;
case 10:
printf("List record \n");
RecordList();
break;
case 0:return 0;
break;
}
}while(choice != 0);
return 0;
}
/// @brief 输入所有学生信息
void InputRecord() //1
{
int i = 0,j = 0;
unsigned int temp = 0;
printf("Please input the number of students.\n");
scanf("%d",&Number_);
printf("Please input the number of coures.\n");
scanf("%d",&Course_);
for(i = 0; i < Number_; i++)
{
printf("\nNo.%d student.\n",i+1);
printf("Please input the student's id\n");
scanf("%lld",&student[i].id);//学号
//这里也可以用字符串,最开始就是用的字符串但是当时还没用过strcmp()函数(后面匹配姓名没办法了才知道这个函数),就改成比对数字是否相同了,用了一个unsigned longlongint 233,不过这样好像是简单一些(就是内存用的有些奢侈)
printf("Please input the student's name\n");
scanf("%20s",student[i].name);//不超过20字符的姓名
for(j = 0; j < Course_; j++)
{
printf("Please input No.%d coures score\n",j+1);
scanf("%lf",student[i].score+j);
// "指针+偏移量"访问数组元素,即student[i].score + j表示数组中第j个元素的地址。
// 又因为student[i].score是一个数组,所以它本身就是一个指针,可以直接进行"指针+偏移量"来访问数组的某个元素。
}
}
printf("\n");return;
}
/// @brief 计算每门课程的总分和平均分
void CaculateEveryCourses() //2
{
int i,j;
double total,average;
for(j = 0; j < Course_; j++)
{
total = 0;
average = 0;
for(i=0; i < Number_; i++)
{
total += student[i].score[j];
}
average = total / (double)Number_;
printf("\n");
printf("No.%d course:\n",j+1);
printf("\tTotal score: %.2lf.\n",total);
printf("\tAverage score: %.2lf.\n",average);
}
printf("\n");return;
}
/// @brief 计算每个学生的总分和平均分
void CaculateEveryStudent() //3
{
int i,j;
double total,average;
for(i = 0; i < Number_; i++)
{
total = 0;
average = 0;
for(j = 0; j < Course_; j++)
{
total += student[i].score[j];
}
average = total / (double)Course_;
student[i].average = average;
student[i].total = total;
printf("\n");
printf("No.%d student:\n",i+1);
printf("%lld %s\n",student[i].id,student[i].name);
for(j = 0; j < Course_; j++)
{
printf("\tScore of No.%d coures score is %lf.\n",
j+1, student[i].score[j]);
}
printf("\tTotal score: %.2lf.\n",total);
printf("\tAverage score: %.2lf.\n",average);
}
printf("\n");return;
}
/// @brief 按学生总分从高到低进行排序
void SortByTotal() //4
{
int i = 0, j = 0, maxIndex = 0, temp = 0;
int arr[30]={0}; // 存放学生结构体在排序后的序列
// 初始化arr数组为0 ~ Number_-1
for(i = 0; i < Number_; i++)
{
arr[i] = i;
}
// 高到低排斥
for(i = 0; i < Number_; i++)
{
maxIndex = i;
for(j = i + 1; j < Number_; j++)
{
// 逐个找出最大值的序号
if(student[j].total > student[maxIndex].total)
{
maxIndex = j;
}
}
// 将当前未排序序列的最大值交换到已排序的末尾
temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
printf("Sort by total score in descending order:\n"); // 输出按总分排序后的结果
for(i = 0; i < Number_; i++)
{
printf("%lld\t%s\n\ttotal score: %.2lf\n",
student[arr[i]].id, student[arr[i]].name,
student[arr[i]].total);
}
printf("\n");return;
}
/// @brief 按学生学号从高到低排序
void SortByNumber() //5
{
int i = 0, j = 0, temp = 0;
int arr[30]={0};
//初始化arr数组为0 ~ Number_-1
for(i = 0; i < Number_; i++)
{
arr[i] = i;
}
//对arr数组进行排序 //经过排序后arr[i]的值是对应结构体第i个元素的排名 //后续遍历时只需输出结构体中的第arr[i++]个元素就是正确的序了
for(i = 0; i < Number_; i++)
{
for(j = i+1; j < Number_; j++)
{
if(student[i].id < student[j].id)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Sort by number in descending order:\n");
//遍历
for(i = 0; i < Number_; i++)
{
printf("%lld\t%s\n",
student[arr[i]].id, student[arr[i]].name);
for(j = 0; j < Course_; j++)
{
printf("\tScore of No.%d coures score is %lf.\n",
j+1, student[arr[i]].score[j]);
}
printf("\ttotal score: %.2lf\n\taverage score: %.2lf\n",
student[arr[i]].total, student[arr[i]].average);
}
printf("\n");return;
}
/// @brief 按首字母ASCII排序(词典写着太复杂了)
void SortByDictionary() //6
{
int i = 0, j = 0;
char arr[30]={0};// 存放学生结构体在排序后的序列
char temp = 0;
//初始化arr数组为0 ~ Number_-1
for(i = 0; i < Number_; i++)
{
arr[i] = i;
}
//对arr数组进行排序 //经过排序后arr[i]的值是对应结构体第i个元素的排名 //后续遍历时只需输出结构体中的第arr[i++]个元素就是正确的序了
for(i = 0; i < Number_; i++)
{
for(j = i+1; j < Number_; j++)
{
if(student[i].name[0] < student[j].name[0])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Sort by number in descending order:\n");
//遍历
for(i = 0; i < Number_; i++)
{
printf("%s\t%lld\n",
student[arr[i]].name, student[arr[i]].id);
for(j = 0; j < Course_; j++)
{
printf("\tScore of No.%d coures score is %lf.\n",
j+1, student[arr[i]].score[j]);
}
printf("\ttotal score: %.2lf\n\taverage score: %.2lf\n",
student[arr[i]].total, student[arr[i]].average);
}
printf("\n");return;
}
/// @brief 按学号查找学生信息
void SearchByNumber() //7
{
int i = 0, j = 0;
unsigned long long int ID;
printf("Please input the number you want to search: ");
scanf("%lld",&ID);
for(i = 0; i < Number_; i++)
{
if(ID == student[i].id)
{
printf("%lld %s",
student[i].id, student[i].name);
for(j = 0; j < Course_; j++)
{
printf(" Score of No.%d coures score is %lf.\n",j+1,student[i].score[j]);
}
printf(" \ntotal score: %.2lf average score: %.2lf\n",
student[i].total, student[i].average);
return;
}
else
{
printf("ERROR\n");
printf("The number of the search was not found in the record.\n");
}
}
return;
}
/// @brief 按姓名查找学生信息
void SearchByName() //8
{
int i = 0, j = 0;
char name[21];
printf("Please input the name you want to search: ");
scanf("%20s",name);//输入不超过20字符的姓名
for(i = 0; i < Number_; i++)
{
if(strcmp(name,student[i].name) == 0)
{
break;
}
}
if(strcmp(name,student[i].name) != 0)
{
printf("ERROR\n");
printf("The name of the search was not found in the record.\n");
}
else
{
printf("Name: %s\n", student[i].name);
for(j = 0; j < Course_; j++)
{
printf(" Score of No.%d coures score is %lf.\n",j+1,student[i].score[j]);
}
printf(" \ntotal score: %.2lf average score: %.2lf\n",
student[i].total, student[i].average);
}
}
/// @brief 对每门课程进行成绩统计
void CourseStatistic() //9
{
int i = 0, j = 0;
double score[30][6] = {0};
double (*p)[6] = score; // 定义指向数组 score 的指针
for(i = 0; i < Number_; i++)
{
for(j = 0; j < Course_; j++)
{
*(p[i]+j) = student[i].score[j]; // 使用指针访问二维数组 score,并将其赋值
}
}
for(j = 0; j < Course_; j++)// 遍历每门课程
{
int excellent = 0, good = 0, medium = 0, pass = 0, fail = 0;// 存储各个类别的人数
double total = Number_;
for(i = 0; i < Number_; i++)// 遍历每个学生的某一门课程成绩
{
if(score[i][j] >= 90)
{
excellent++;
}
else if(score[i][j] >= 80 && score[i][j] < 90)
{
good++;
}
else if(score[i][j] >= 70 && score[i][j] < 80)
{
medium++;
}
else if(score[i][j] >= 60 && score[i][j] < 70)
{
pass++;
}
else
{
fail++;
}
}
printf("For course%d, there are %d students (accounting for %.2lf%%) with excellent score,\n", j+1, excellent, excellent/total*100);
printf("%d students (accounting for %.2lf%%) with good score,\n", good, good/total*100);
printf("%d students (accounting for %.2lf%%) with medium score,\n", medium, medium/total*100);
printf("%d students (accounting for %.2lf%%) with pass score,\n", pass, pass/total*100);
printf("%d students (accounting for %.2lf%%) with fail score.\n\n", fail, fail/total*100);
}
}
/*
void RecordList() //直接用结构体访问的版本
{
int i = 0, j = 0;
for(i = 0; i < Number_; i++)
{
printf("No.%d student:\n",i+1);
printf("%lld %s",
student[i].id, student[i].name);
for(j = 0; j < Course_; j++)
{
printf(" Score of No.%d coures score is %lf.\n",j+1,student[i].score[j]);
}
printf(" \ntotal score: %.2lf average score: %.2lf\n",
student[i].total, student[i].average);
}
}
*/
/// @brief 输出所有学生记录信息
void RecordList() //10 //使用指针访问结构体
{
int i = 0, j = 0;
struct students *p = student;
for(i = 0; i < Number_; i++, p++)
{
printf("No.%d student:\n",i+1);
printf("%lld %s\n",
p->id, p->name);
for(j = 0; j < Course_; j++)
{
printf(" Score of No.%d coures score is %lf.\n",j+1,p->score[j]);
}
printf(" \ntotal score: %.2lf average score: %.2lf\n",
p->total, p->average);
}
}
/*测试用例
1
4
3
202232182319
Aname
100
60
80
202032182319
Cname
60
100
55.6
202332182319
Bname
90
67
86
202132182319
Dname
90
75
84
2
3
4
5
6
7
202232182319
8
Bname
9
10
0
*/