学生成绩管理系统
tips : 应该写的注释都写了,适合初学者参考。
某班最多不超过30人(具体人数由键盘输入),考试科目最多不超过6门(具体门数由键盘输入)。
请编写一个程序实现如下菜单驱动的学生成绩管理系统;要求:
(1)录入每个学生 的学号、姓名和各科考试成绩。
(2)计算每门课程的总分和平均分。
(3)计算每个学生的总分和平均分。
(4)按每个学生的总分由高到低排出名次表。
(5)按学号由小到大排出成绩表。
(6)按姓名的字典顺序排出成绩表。
(7)按学号查询学生排名及各科考试成绩。
(8)按姓名查询学生排名及各科考试成绩。
(9)按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,对每门课程分别统计每个类别的人数及所占的百分比。
(10)输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分。
code(可直接运行):

1 #define _CRT_SECURE_NO_WARNINGS
2 #include <stdio.h>
3 #include <string.h>
4 #include<stdlib.h>
5
6 struct Course {
7 int C_score;
8 char C_name[10];
9 };
10 struct Stu {
11 int id;
12 char name[10];
13 struct Course courses[6];
14 int total_score;
15 int ave;
16 int ranking;
17 };
18 struct Stu stu[30];
19 int real_stu;
20 int real_cou;
21 int temp[30] = { 0 };
22 int temp_forFun7[30] = { 0 }; //这里在函数5中修改了 temp ,但是fun7 要用,所以但copy出来一个。
23 int C_total_score[6] = { 0 };
24 int C_ave[6] = { 0 };
25
26 enum level {
27 优秀 = 0, 良好, 中等, 及格, 不及格
28 };
29
30 int getRanking(int id);
31 struct Stu* getStu(int id);
32 char* getLevel(int num);
33
34 int fun0();
35 int fun1();
36 int fun2();
37 int fun3();
38 int fun4();
39 int fun5();
40 int fun6();
41 int fun7();
42 int fun8();
43 int fun9();
44 int fun10();
45 int main()
46 {
47 while (1) {
48 //输入数字几,则运行程序几
49 switch (fun0()) {
50 case 0: exit(0); break;
51 case 1: fun1(); break;
52 case 2: fun2(); break;
53 case 3: fun3(); break;
54 case 4: fun4(); break;
55 case 5: fun5(); break;
56 case 6: fun6(); break;
57 case 7: fun7(); break;
58 case 8: fun8(); break;
59 case 9: fun9(); break;
60 case 10: fun10(); break;
61 case 11:/*fun11()*/; break;
62 }
63 }
64
65 return 0;
66 }
67
68
69 //0菜单
70 int fun0() {
71 //菜单
72 int n;
73 printf("*******************************************************\n");
74 printf("1. 录入每个学生 的学号、姓名和各科考试成绩.\n");
75 printf("2. 计算每门课程的总分和平均分。\n");
76 printf("3. 计算每个学生的总分和平均分。\n");
77 printf("4. 按每个学生的总分由高到低排出名次表。\n");
78 printf("5. 按学号由小到大排出成绩表。\n");
79 printf("6. 按姓名的字典顺序排出成绩表。\n");
80 printf("7. 按学号查询学生排名及各科考试成绩。\n");
81 printf("8. 按姓名查询学生排名及各科考试成绩。\n");
82 printf("9. 按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,对每门课程分别统计每个类别的人数及所占的百分比。\n");
83 printf("10.输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分。\n");
84 printf("0. Exit\n");
85 printf("*******************************************************\n");
86 printf(" Please enter your choice :\n");
87 scanf_s("%d", &n);
88 return n;
89 }
90
91 //1,录入每个学生的学号、姓名和各科考试成绩。
92 int fun1() {
93 //(1)录入每个学生的学号、姓名和各科考试成绩。
94 printf("请输入总人数(不超过30人):");
95 scanf("%d", &real_stu);
96 printf("请输入总课程数(不超过6门):");
97 scanf("%d", &real_cou);
98
99 //(1)录入、每个 学生 的学号、姓名和各科考试成绩。
100 for (int i = 0; i < real_stu; i++) {//录入第 i 个学生信息:学号 姓名。
101 printf("请输入第 %d 个学生的信息 ( 输入格式提示 例如:[学号 姓名] 中括号里面的为一次录入,不用写中括号。)\n", i + 1);
102 scanf("%d %s", &(stu[i].id), stu[i].name);
103
104 printf("请输入需要录入课程的信息 ( 输入格式提示 例如:[课程名字 课程分数] 中括号里面的为一次录入,不用写中括号。)\n");
105 for (int j = 0; j < real_cou; j++) {//录入第 i 个学生课程信息:课程名字 分数。
106 scanf("%s %d", stu[i].courses[j].C_name, &(stu[i].courses[j].C_score));
107 }
108 }
109 return 0;
110 }
111
112 //2,计算每门课程的总分和平均分
113 int fun2() {
114 //(2)计算每门课程的总分和平均分
115
116 for (int i = 0; i < real_stu; i++) {
117 for (int j = 0; j < real_cou; j++) {
118 C_total_score[j] += stu[i].courses[j].C_score;
119 }
120 }
121 for (int i = 0; i < real_stu; i++) {
122 C_ave[i] = C_total_score[i] / real_stu;//第i门课的平均分
123 }
124 //最后不用print
125 for (int i = 0; i < real_cou; i++) {
126 printf("%d %d\n", C_total_score[i], C_ave[i]);
127
128 }
129 return 0;
130 }
131
132 //3,计算每个学生的总分和平均分。
133 int fun3() {
134 //(3)计算每个学生的总分和平均分。
135 for (int i = 0; i < real_stu; i++) {//第几个学生
136 for (int j = 0; j < real_cou; j++) {//第几门课
137 stu[i].total_score += stu[i].courses[j].C_score;
138 }
139 }
140 for (int i = 0; i < real_cou; i++) {
141 stu[i].ave = stu[i].total_score / real_cou;
142 }
143 for (int i = 0; i < real_stu; i++) {
144 printf("第%d个学生的总分为%d\n", i + 1, stu[i].total_score);
145 printf("第%d个学生的平均分为 % d\n", i + 1, stu[i].ave);
146 }
147 return 0;
148 }
149
150 //4,按每个学生的总分由高到低排出名次表。
151 int fun4() {
152 //(4)按每个学生的总分由高到低排出名次表。
153 for (int i = 0; i < real_stu; i++) {
154 temp[i] = stu[i].id; //创建数组,存id,通过id找到信息
155 }
156 int temp_for_swap = 0;
157 //排序
158 for (int i = 0; i < real_stu - 1; i++) {//轮次,n-1次
159 for (int j = 0; j < real_stu - i - 1; j++) {//一次排序
160 if (getStu(temp[j])->total_score < getStu(temp[j + 1])->total_score) {
161 //交换 temp[i] temp[i+1]
162 //t_temp,为中间作为交换的数组,存放id
163 //temp数组为存放id数组
164 temp_for_swap = temp[j];
165 temp[j] = temp[j + 1];
166 temp[j + 1] = temp_for_swap;
167 }
168 }
169 }
170 //输出
171 printf("按每个学生的总分由高到低排出的名次表为\n");
172 for (int i = 0; i < real_stu; i++) {
173 printf("%d %s %d\n", getStu(temp[i])->id, getStu(temp[i])->name, getStu(temp[i])->total_score);
174 }
175 //这里在函数5中修改了 temp ,但是fun7 要用,所以但copy出来一个。
176 memcpy(temp_forFun7,temp,real_stu);
177 return 0;
178
179 }
180
181 //5,按学号由小到大排出成绩表。
182 int fun5() {
183 //(5)按学号由小到大排出成绩表。
184 //排序,学号大小,也就是说,输入不一定按学号从小到大输入
185 int temp_for_swap = 0;
186 //排序
187 for (int i = 0; i < real_stu - 1; i++) {//轮次,n-1次
188 for (int j = 0; j < real_stu - i - 1; j++) {//一次排序
189 if (getStu(temp[j])->id > getStu(temp[j + 1])->id) {
190 //交换 temp[i] temp[i+1]
191 //t_temp,为中间作为交换的数组,存放id
192 //temp数组为存放id数组
193 temp_for_swap = temp[j];
194 temp[j] = temp[j + 1];
195 temp[j + 1] = temp_for_swap;
196 }
197 }
198 }
199 printf("按每个学生的按学号由小到大排出成绩表为\n");
200 for (int i = 0; i < real_stu; i++) {
201 printf("%d %s %d\n", getStu(temp[i])->id, getStu(temp[i])->name, getStu(temp[i])->total_score);
202 }
203 return 0;
204 }
205
206 //6,按姓名的字典顺序排出成绩表。
207 int fun6() {
208 //(6)按姓名的字典顺序排出成绩表。
209 //字典顺序,成绩表
210 //字符串比较
211 int temp_for_swap = 0;
212 for (int i = 0; i < real_stu - 1; i++) {
213 for (int j = 0; j < real_stu - i - 1; j++) {
214 if (strcmp(getStu(temp[j])->name, getStu(temp[j + 1])->name) > 0) {
215 temp_for_swap = temp[j];
216 temp[j] = temp[j + 1];
217 temp[j + 1] = temp_for_swap;
218 }
219 }
220 }
221 //print
222 printf("按学生姓名的字典顺序排出成绩表为\n");
223 for (int i = 0; i < real_stu; i++) {
224 printf("%d %s %d\n", getStu(temp[i])->id, getStu(temp[i])->name, getStu(temp[i])->total_score);
225 }
226 return 0;
227 }
228 //7,按学号查询学生排名及各科考试成绩。
229
230 int fun7() {
231 //按学号查询学生排名及各科考试成绩。
232 for (int i = 0; i < real_stu; i++) {
233 getStu(temp_forFun7[i])->ranking = getRanking(temp_forFun7[i]);//排名,由get函数初始化
234 }
235 int input_id;
236 printf("请输入需要查询成绩的学生的学号:");
237 scanf("%d", &input_id);
238
239 struct Stu* hit_stu = getStu(input_id);//把用getStu函数找到的学生信息,给hit——stu
240
241 printf("学号为%d的学生是%s\n", hit_stu->id, hit_stu->name);
242 printf("排名为%d\n", hit_stu->ranking + 1);
243 printf("其各科成绩为:\n");
244 for (int i = 0; i < real_cou; i++) {
245 printf("%s %d\n", hit_stu->courses[i].C_name, hit_stu->courses[i].C_score);
246 }
247 return 0;
248 }
249 //8,按姓名查询学生排名及各科考试成绩。
250 int fun8() {
251 //(8)按姓名查询学生排名及各科考试成绩。
252 char input_name[10] = { 0 };
253 printf("请输入需要查询成绩的学生的姓名:");
254 scanf("%s", input_name);
255
256 struct Stu* hit_stu = NULL;
257 for (int i = 0; i < real_stu; i++) {
258 if (strcmp(hit_stu->name, input_name) == 0) {
259 hit_stu = &stu[i];
260 }
261 }
262 if (hit_stu == NULL) {
263 printf("姓名为%s的学生不存在\n", input_name);
264 return;
265 }
266
267 printf("学号为%d的学生是%s\n", hit_stu->id, hit_stu->name);
268 printf("排名为%d\n", hit_stu->ranking + 1);
269 printf("其各科成绩为:\n");
270 for (int i = 0; i < real_stu; i++) {
271 printf("%s %d\n", hit_stu->courses[i].C_name, hit_stu->courses[i].C_score);
272 }
273 return 0;
274 }
275 //(9)按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,对每门课程分别统计每个类别的人数及所占的百分比。,switch语句,除以总人数,然后打印
276 int fun9() {
277
278 //(9)按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,
279 //对每门课程分别统计 每个类别的人数及所占的百分比。,除以总人数,然后打印
280 //等价于 int type [][];
281 // type[ 第几门课 ] [ 该课的类别 ]
282 //int** type = (int**)malloc(sizeof(int) * 5 * real_cou);
283 float type[6][5] = {0};
284 for (int i = 0; i < real_stu; i++) {
285 for (int j = 0; j < real_cou; j++) {
286 //type[i][j];
287 int temp_score = stu[i].courses[j].C_score;
288 if (temp_score >= 0 && temp_score < 59) {
289 type[j][不及格] += 1;//bad
290 }
291 else if (temp_score >= 60 && temp_score < 69) {
292 type[j][及格] += 1;//及格
293 }
294 else if (temp_score >= 70 && temp_score < 79) {
295 type[j][中等] += 1;//中等
296 }
297 else if (temp_score >= 80 && temp_score < 89) {
298 type[j][良好] += 1;//良好
299 }
300 else if (temp_score >= 90 && temp_score < 100) {
301 type[j][优秀] += 1;//优秀
302 }
303 }
304 }
305
306 //float** ratio = (float**)malloc(sizeof(float) * 5 * real_cou);
307 float ratio[6][5] = { 0.0 };
308
309 for (int i = 0; i < real_cou; i++)
310 {
311 for (int j = 0; j < 5; j++)
312 {
313 ratio[i][j] = type[i][j] / real_stu;
314 }
315
316 }
317
318 //print
319 for (int i = 0; i < real_cou; i++)
320 {
321 printf("第 %d 课程的各等级占比为:\n", i);
322 for (int j = 0; j < 5; j++)
323 {
324 printf("\t\t %s 占比 %f: \n", getLevel(j), ratio[i][j]);
325 }
326
327 }
328
329 return 0;
330 }
331 //(10)输出每个学生的学号、姓名、各科考试成绩、总分、平均分,以及每门课程的总分和平均分。
332 int fun10() {
333 for (int i = 0; i < real_stu; i++)
334 {
335 printf("姓名为%s\t学号为%d\t学生总分为%d\t平均分为%d\n", getStu(temp[i])->name, getStu(temp[i])->id, getStu(temp[i])->total_score, getStu(temp[i])->ave);
336 for (int j = 0; j < real_cou; j++) {
337 printf("\t科目为:%s \t考试成绩为: %d\t科目总分: %d\t科目平均分 :%d 。\n",
338 getStu(temp[i])->courses[j].C_name, getStu(temp[i])->courses[j].C_score, C_total_score[i], C_ave[i]);
339 }
340 }
341 return 0;
342 }
343
344 int getRanking(int id) {
345 for (int i = 0; i < real_stu; i++) {
346 if (id == temp[i]) {
347 return i;//i即为排名
348 }
349 }
350 }
351 struct Stu* getStu(int id) {
352 for (int i = 0; i < real_stu; i++)
353 {
354 if (stu[i].id == id) {
355 return &stu[i];
356 }
357 }
358 return NULL;
359 }
360
361 char* getLevel(int num) {
362 //enum level {
363 // 优秀 = 0, 良好, 中等, 及格, 不及格
364 //};
365 switch (num)
366 {
367 case 0:
368
369 return "优秀";
370 case 1:
371
372 return "良好";
373 case 2:
374
375 return "中等";
376 case 3:
377
378 return "及格";
379 case 4:
380
381 return "不及格";
382 default:
383 return "unknown";
384 }
385 }
来源:https://www.cnblogs.com/jiujue/p/12391816.html
