|
|
# 学习成绩管理系统
|
|
|
|
|
|
2023年4月17日-5月1日
|
|
|
|
|
|
李雨佳 徐子贵 许子林 张宇
|
|
|
|
|
|
## 项目简介
|
|
|
|
|
|
本成绩管理系统是为教师提供管理班级成绩的工具,该系统可以实现的对学生的成绩进行增、删、改、查、信息保存和信息导入等基本信息,以及对学生成绩排序和分段的扩展功能,有利于对学生成绩管理。项目采用C语言编程实现,在VS Code 集成开发环境(IDE)中使用GCC进行编译。系统采用模块化设计,程序结构清晰,采用菜单驱动的命令行界面,操作便捷,能够用CSV格式读取和保存数据,通用性强,能够用图表展示数据,直观清楚。
|
|
|
|
|
|
下载地址:https://bdgit.educoder.net/pvfj5ugro/text1.git
|
|
|
|
|
|
项目开发过程中采用 Kanban(看板)进行任务管理和分工协作,并使用 Git 对程序代码和文档进行版本管理。任务分工情况如下:
|
|
|
|
|
|
| 任务 | 设计 | 开发 | 测试 | 文档 |
|
|
|
| ------ | ----- | ----- | -- | ----- |
|
|
|
| C1-C3 菜单驱动的用户界面 | 许子林 | 许子林 | 李雨佳 张宇 | 徐子贵 |
|
|
|
| C4 添加学生信息 | 张宇 | 张宇 | 徐子贵 许子林 | 李雨佳 |
|
|
|
| C5-C6 求和求平均 | 徐子贵 | 徐子贵 | 李雨佳 许子林 | 张宇 |
|
|
|
| C7-C9 排序 | 李雨佳 | 李雨佳 | 张宇 徐子贵 | 许子林 |
|
|
|
| C10 学生信息查询 | 张宇 | 张宇 | 李雨佳 徐子贵 | 许子林 |
|
|
|
| C11 统计学生信息 | 徐子贵 | 徐子贵 | 许子林 张宇 | 李雨佳 |
|
|
|
| C12 打印学生信息 | 许子林 | 许子林 | 李雨佳 张宇 | 徐子贵 |
|
|
|
| C13 将学生信息读入文件 | 李雨佳 | 李雨佳 | 许子林 徐子贵 | 张宇 |
|
|
|
| C14 从文件中读取学生信息 | 李雨佳 | 李雨佳 | 张宇 徐子贵 | 许子林 |
|
|
|
|
|
|
每个成员的工作量(百分比):
|
|
|
|
|
|
| 李雨佳 | 徐子贵 | 许子林 | 张宇 |
|
|
|
| ------ | ----- | ------ | --- |
|
|
|
|30 | 30 | 20 | 20 |
|
|
|
|
|
|
## 关于学生成绩管理系统
|
|
|
|
|
|
设计一个学生成绩管理系统,要求采用行菜单界面进行交互,具备读取、保存、打印、查询、插入和排序等基本功能,能够以表格和图表形式展示数据,采用CSV格式保存数据。
|
|
|
|
|
|
系统的功能性需求:
|
|
|
|
|
|
- 数据的读取、保存、打印、查询、插入、排序和图表展示。
|
|
|
|
|
|
系统的非功能性需求:
|
|
|
|
|
|
- 菜单驱动的命令行交互界面
|
|
|
|
|
|
## 需求分析
|
|
|
|
|
|
分析系统的功能需求和界面需求,编制用户手册如下。
|
|
|
|
|
|
### C1: 启动程序
|
|
|
|
|
|
命令行中执行命令./app,系统启动,显示提示信息,然后显示功能菜单,等待用户输入命令。
|
|
|
|
|
|
```
|
|
|
Management for Students'scores
|
|
|
|
|
|
1 Input record
|
|
|
2 Calculate total and average score of every course
|
|
|
3 Calculate total and average score of every student
|
|
|
4 Sort in descending order by score
|
|
|
5 Sort in ascending order by score
|
|
|
6 Sort in ascending order by number
|
|
|
7 Sort in dictionary order by name
|
|
|
8 Search by number
|
|
|
9 Search by name
|
|
|
10 Statistic analysis
|
|
|
11 List record
|
|
|
12 Write to a file
|
|
|
13 Read from a file
|
|
|
0 Exit
|
|
|
|
|
|
Please Input your choice:
|
|
|
|
|
|
```
|
|
|
|
|
|
### C2: 显示命令菜单
|
|
|
|
|
|
调用 Menu() 函数显示命令菜单,用户输入选项后,将结果返回主函数
|
|
|
|
|
|
```
|
|
|
Management for Students'scores
|
|
|
|
|
|
1 Input record
|
|
|
2 Calculate total and average score of every course
|
|
|
3 Calculate total and average score of every student
|
|
|
4 Sort in descending order by score
|
|
|
5 Sort in ascending order by score
|
|
|
6 Sort in ascending order by number
|
|
|
7 Sort in dictionary order by name
|
|
|
8 Search by number
|
|
|
9 Search by name
|
|
|
10 Statistic analysis
|
|
|
11 List record
|
|
|
12 Write to a file
|
|
|
13 Read from a file
|
|
|
0 Exit
|
|
|
|
|
|
Please Input your choice:
|
|
|
|
|
|
```
|
|
|
|
|
|
### C3: 退出程序
|
|
|
|
|
|
选择菜单命令 0 ,再输入 y 确认,则退出程序。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 0
|
|
|
Are you sure you want to exit?(Y/N): y
|
|
|
End of program!
|
|
|
|
|
|
```
|
|
|
|
|
|
### C4: 添加学生信息
|
|
|
|
|
|
选择菜单命令 1 ,假设n=4,m=3,提示输入学生的学号,姓名和成绩,然后利用循环逐个输入学生的学号和姓名以及各科成绩。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 1
|
|
|
Input student's ID, name and score:
|
|
|
2214111006 XuZilin 100 95 86
|
|
|
2214111009 XuZigui 77 89 99
|
|
|
2214111011 ZhangYu 85 91 98
|
|
|
2214111025 LiYujia 75 89 100
|
|
|
|
|
|
```
|
|
|
|
|
|
### C5: 计算学生各门课程总分和平均分
|
|
|
|
|
|
选择菜单命令 2 ,计算学生各门课程总分和平均分,然后利用循环逐个输出学生的顺序,总成绩和平均分,结果都取整数。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 2
|
|
|
student 1: sum = 281, aver = 93
|
|
|
student 2: sum = 265, aver = 88
|
|
|
student 3: sum = 274, aver = 91
|
|
|
student 4: sum = 264, aver = 88
|
|
|
|
|
|
```
|
|
|
|
|
|
### C6: 计算每门课程的总分和平均分
|
|
|
|
|
|
选择菜单命令 3 ,计算每门课程的总分和平均分,然后利用循环逐个输出每门课程的总分和平均分,结果都取整数。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 3
|
|
|
course 1: sum = 337, aver = 84
|
|
|
course 2: sum = 364, aver = 91
|
|
|
course 3: sum = 383, aver = 95
|
|
|
|
|
|
```
|
|
|
|
|
|
### C7: 学生总分排序
|
|
|
|
|
|
如果a < b,则按升序排列。选择菜单命令 4 ,按选择法将数组 sum 的元素值按升序进行排序,显示提示信息,最后打印出学生信息。
|
|
|
如果a > b,则按降序排序。选择菜单命令 5 ,按选择法将数组 sum 的元素值按降序进行排序,显示提示信息,最后打印出学生成绩。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 4
|
|
|
Sort in descending order by score:
|
|
|
2214111006 XuZilin 100 95 86 281 93
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
2214111009 XuZigui 77 89 99 265 88
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
|
|
|
Please Input your choice: 5
|
|
|
Sort in ascending order by score:
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
2214111009 XuZigui 77 89 99 265 88
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
2214111006 XuZilin 100 95 86 281 93
|
|
|
|
|
|
```
|
|
|
|
|
|
### C8: 学号顺序排序
|
|
|
|
|
|
选择菜单命令 6 ,按学号从小到大进行排序,显示提示信息,最后打印学生信息。
|
|
|
```
|
|
|
Please Input your choice: 6
|
|
|
Sort in ascending order by number:
|
|
|
2214111006 XuZilin 100 95 86 281 93
|
|
|
2214111009 XuZigui 77 89 99 265 88
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
|
|
|
```
|
|
|
|
|
|
### C9: 姓名顺序排序
|
|
|
|
|
|
选择菜单命令 7 ,按姓名的字典顺序进行排序,显示提示信息,最后打印学生信息。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 7
|
|
|
Sort in dictionary order by name:
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
2214111009 XuZigui 77 89 99 265 88
|
|
|
2214111006 XuZilin 100 95 86 281 93
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
|
|
|
```
|
|
|
|
|
|
### C10: 学生信息查询
|
|
|
|
|
|
- 选择菜单命令 8 ,提示输入学生学号,若该学号存在,则输出学生信息;否则提示没有找到并结束。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 8
|
|
|
Input the number you want to search:2214111011
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
|
|
|
1 Input record
|
|
|
2 Calculate total and average score of every course
|
|
|
3 Calculate total and average score of every student
|
|
|
4 Sort in descending order by score
|
|
|
5 Sort in ascending order by score
|
|
|
6 Sort in ascending order by number
|
|
|
7 Sort in dictionary order by name
|
|
|
8 Search by number
|
|
|
9 Search by name
|
|
|
10 Statistic analysis
|
|
|
11 List record
|
|
|
12 Write to a file
|
|
|
13 Read from a file
|
|
|
0 Exit
|
|
|
|
|
|
Please Input your choice: 8
|
|
|
Input the number you want to search:2214111001
|
|
|
Not found!
|
|
|
|
|
|
```
|
|
|
|
|
|
- 选择菜单命令 9 ,提示输入学生姓名,若该姓名存在,则输出学生信息;否则提示没有找到并结束。
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 9
|
|
|
Input the name you want to search:LiYujia
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
|
|
|
1 Input record
|
|
|
2 Calculate total and average score of every course
|
|
|
3 Calculate total and average score of every student
|
|
|
4 Sort in descending order by score
|
|
|
5 Sort in ascending order by score
|
|
|
6 Sort in ascending order by number
|
|
|
7 Sort in dictionary order by name
|
|
|
8 Search by number
|
|
|
9 Search by name
|
|
|
10 Statistic analysis
|
|
|
11 List record
|
|
|
12 Write to a file
|
|
|
13 Read from a file
|
|
|
0 Exit
|
|
|
|
|
|
Please Input your choice: 9
|
|
|
Input the name you want to search:SunYizhe
|
|
|
Not found!
|
|
|
|
|
|
```
|
|
|
|
|
|
### C11: 统计各分数段学生人数及所占百分比
|
|
|
|
|
|
选择菜单命令 10,提示输入学生成绩,通过循环得出各分数段学生人数及所占百分比
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 10
|
|
|
For course 1:
|
|
|
<60 0 0.00%
|
|
|
60-70 0 0.00%
|
|
|
70-80 2 50.00%
|
|
|
80-90 1 25.00%
|
|
|
90-100 0 0.00%
|
|
|
100 1 25.00%
|
|
|
For course 2:
|
|
|
<60 0 0.00%
|
|
|
60-70 0 0.00%
|
|
|
70-80 0 00.00%
|
|
|
80-90 2 50.00%
|
|
|
90-100 2 50.00%
|
|
|
100 0 0.00%
|
|
|
For course 3:
|
|
|
<60 0 0.00%
|
|
|
60-70 0 0.00%
|
|
|
70-80 0 0.00%
|
|
|
80-90 1 25.00%
|
|
|
90-100 2 50.00%
|
|
|
100 1 25.00%
|
|
|
|
|
|
```
|
|
|
### C12: 打印学生信息
|
|
|
|
|
|
选择菜单命令11,打印
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 11
|
|
|
2214111006 XuZilin 100 95 86 281 93
|
|
|
2214111009 XuZigui 77 89 99 265 88
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
|
|
|
```
|
|
|
|
|
|
### C13: 将学生信息保存在文件中
|
|
|
|
|
|
选择菜单命令 12 ,将学生信息保存在文件 student.txt 中。如果文件不存在,则给出错误信息并退出程序,否则逐个读入
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 12
|
|
|
Failure to open score.txt!
|
|
|
|
|
|
1 Input record
|
|
|
2 Calculate total and average score of every course
|
|
|
3 Calculate total and average score of every student
|
|
|
4 Sort in descending order by score
|
|
|
5 Sort in ascending order by score
|
|
|
6 Sort in ascending order by number
|
|
|
7 Sort in dictionary order by name
|
|
|
8 Search by number
|
|
|
9 Search by name
|
|
|
10 Statistic analysis
|
|
|
11 List record
|
|
|
12 Write to a file
|
|
|
13 Read from a file
|
|
|
0 Exit
|
|
|
|
|
|
Please Input your choice: 12
|
|
|
2214111006 XuZilin 100 95 86 281 93
|
|
|
2214111009 XuZigui 77 89 99 265 88
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
|
|
|
```
|
|
|
|
|
|
### C14:从文件中读取学生信息
|
|
|
|
|
|
选择菜单命令 13 ,从文件中读取学生的学号、姓名及成绩等信息写入到结构体数组 stu 中。如果文件为空,则给出错误信息并退出程序,反之,逐个读入
|
|
|
|
|
|
```
|
|
|
Please Input your choice: 13
|
|
|
Failure to open score.txt!
|
|
|
|
|
|
1 Input record
|
|
|
2 Calculate total and average score of every course
|
|
|
3 Calculate total and average score of every student
|
|
|
4 Sort in descending order by score
|
|
|
5 Sort in ascending order by score
|
|
|
6 Sort in ascending order by number
|
|
|
7 Sort in dictionary order by name
|
|
|
8 Search by number
|
|
|
9 Search by name
|
|
|
10 Statistic analysis
|
|
|
11 List record
|
|
|
12 Write to a file
|
|
|
13 Read from a file
|
|
|
0 Exit
|
|
|
|
|
|
Please Input your choice: 13
|
|
|
2214111006 XuZilin 100 95 86 281 93
|
|
|
2214111009 XuZigui 77 89 99 265 88
|
|
|
2214111011 ZhangYu 85 91 98 274 91
|
|
|
2214111025 LiYujia 75 89 100 264 88
|
|
|
|
|
|
```
|
|
|
|
|
|
## 概要设计
|
|
|
|
|
|
系统主要分为用户界面和数据处理两大模块。
|
|
|
用户界面模块包括系统初始化(init),显示菜单(display_menu),选择菜单命令(make_choice)、确认(confirm)、退出(quit)等子模块。
|
|
|
数据处理模块包括读取数据(read_data)、保存数据(save_data)、打印数据(print_data)、查询数据(query_data)、添加数据(add_data)、更新数据(update_data)、删除数据(delete_data)、数据排序(sort_data)和生成图表(make_chart)等子模块。其中查询、添加、更
|
|
|
新和删除数据还会用到根据编号查询数据的方法(find)。
|
|
|
|
|
|
上述各模块通过主程序(main)进行调用,系统模块图如下。
|
|
|
|
|
|
![系统模块图](main.drawio.svg)
|
|
|
|
|
|
各模块的主要功能如下:
|
|
|
|
|
|
#### main
|
|
|
|
|
|
系统主函数模块,显示菜单,根据用户选择的菜单命令,执行相关操作。
|
|
|
|
|
|
#### init
|
|
|
|
|
|
系统初始化
|
|
|
|
|
|
#### menu
|
|
|
|
|
|
显示菜单命令
|
|
|
|
|
|
## 详细设计
|
|
|
|
|
|
针对概要设计
|
|
|
|
|
|
#### main
|
|
|
|
|
|
Step 1:初始化
|
|
|
Step 2:根据用户选择的命令执行相应的操作
|
|
|
Step 2.1:显示菜单
|
|
|
Step 2.2:选择菜单命令 c
|
|
|
Step 2.3:if c == 1 then ReadScore
|
|
|
Step 2.4:if c == 2 then AverSumofEveryStudent
|
|
|
Step 2.5:if c == 3 then AverSumofEveryCourse
|
|
|
Step 2.6:if c == 4 then SortbyScore(Descending)
|
|
|
Step 2.7:if c == 5 then SortbyScore(Ascending)
|
|
|
Step 2.8:if c == 6 then AsSortbyNum
|
|
|
Step 2.9:if c == 7 then SortbyName
|
|
|
Step 2.10:if c == 8 then SearchbyNum
|
|
|
Step 2.11:if c == 9 then SearchbyName
|
|
|
Step 2.12:if c == 10 then StatisticAnalysis
|
|
|
Step 2.13:if c == 11 then PrintScore
|
|
|
Step 2.14:if c == 12 then WritetoFile
|
|
|
Step 2.15:if c == 13 then ReadfromFile
|
|
|
Step 2.16:if c == 0 then exit
|
|
|
|
|
|
![系统模块图](main函数详细实现.drawio.svg)
|
|
|
|
|
|
#### ReadScore
|
|
|
|
|
|
Step 1:显示提示信息,要求输入学生的学号,姓名和成绩
|
|
|
Step 2:利用循环,输入学生的学号,成绩和各科成绩
|
|
|
|
|
|
![学生信息输入](ReadScore.svg)
|
|
|
|
|
|
#### AverSumofEveryStudent
|
|
|
|
|
|
Step 1:利用for循环给 sum 赋初值为 0
|
|
|
Step 2:利用第二重循环重新计算每个学生的总分
|
|
|
Step 3:计算每个学生的平均分
|
|
|
Step 4:将结果按照舒徐,总分和平均分的顺序进行输出
|
|
|
|
|
|
![计算每个学生各门课程的总分和平均分](AverSumofEveryStudent.svg)
|
|
|
|
|
|
#### AverSumofEveryCourse
|
|
|
|
|
|
step 1:利用for循环给 sum 赋初值为 0
|
|
|
Step 2:利用第二重循环重新计算每门课程的总分
|
|
|
Step 3:计算每门课程的平均分
|
|
|
tep 4:将结果按照顺序,总分和平均分的顺序进行输出
|
|
|
|
|
|
![计算每门课程的总分和平均分](AverSumofEveryCourse.svg)
|
|
|
|
|
|
#### SortbyScore
|
|
|
|
|
|
step 1:利用循环将i赋值给k
|
|
|
step 2:用循环从i+1到n-1比较,将较小的赋给k
|
|
|
step 3:如果k与i不相等,每一课的成绩交换
|
|
|
step 4:总分,平均分,学号,姓名依次交换
|
|
|
|
|
|
![按选择法将数组 sum 的元素值排序](SortbyScore.svg)
|
|
|
|
|
|
#### AsSortbyNum
|
|
|
|
|
|
step 1:利用循环将i赋值给k
|
|
|
step 2:用循环从i+1到n-1比较,将较小的学号赋给k
|
|
|
step 3:如果k与i不相等,每一课的成绩交换
|
|
|
step 4:总分,平均分,学号,姓名依次交换
|
|
|
|
|
|
![按选择法将数组 num 的元素从低到高排序](AsSortbyNum.svg)
|
|
|
|
|
|
#### SortbyName
|
|
|
|
|
|
#### SearchbyNum
|
|
|
|
|
|
#### SearchbyName
|
|
|
|
|
|
#### StatisticAnalysis
|
|
|
|
|
|
Step 1:将数组 t 的全部元素初始化为0
|
|
|
Step 2:利用循环和if条件统计各分数段学生人数
|
|
|
Step 3:利用循环和if条件统计各分数段学生所占比结果
|
|
|
|
|
|
![统计各分数段的学生人数及所占的百分比](StatisticAnalysis.svg)
|
|
|
|
|
|
#### PrintScore
|
|
|
|
|
|
Step 1:利用for循环输出学生的学号和姓名
|
|
|
Step 2:利用二次循环输出每位学生的成绩
|
|
|
Step 3:输出学生的总分数和平均分
|
|
|
|
|
|
![打印学生成绩](PrintScore.svg)
|
|
|
|
|
|
#### WritetoFile
|
|
|
|
|
|
Step 1:读取文件输出学生人数和课程门数
|
|
|
Step 2:利用循环for依次输出学生学号和姓名
|
|
|
Step 3:利用二次循环输出学生成绩
|
|
|
Step 4:输出总成绩和平均分
|
|
|
|
|
|
![输出n个学生的学号,姓名以及m门课程的成绩](-WritetoFile.svg)
|
|
|
|
|
|
#### ReadfromFile
|
|
|
Step 1:从文件中读取学生人数和课门数
|
|
|
Step 2:利用循环for输入学生学号和姓名
|
|
|
Step 3:利用二次循环输入学生成绩
|
|
|
Step 4:输入学生总成绩和平均分
|
|
|
|
|
|
![从文件中读取学生的学号,姓名及成绩等信息写入结构体](ReadfromFile.svg)
|
|
|
#### exit
|
|
|
|
|
|
## 代码实现
|
|
|
|
|
|
// 头文件的使用
|
|
|
|
|
|
```
|
|
|
#include<stdio.h>
|
|
|
#include<stdlib.h>
|
|
|
#include<string.h>
|
|
|
```
|
|
|
|
|
|
// define定义的全局变量
|
|
|
|
|
|
```
|
|
|
#define MAX_LEN 10 //字符串最大长度
|
|
|
#define STU_NUM 30 //最多的考试人数
|
|
|
#define COURSE_NUM 6 //最多的考试科目
|
|
|
```
|
|
|
|
|
|
// 结构体的定义
|
|
|
|
|
|
```
|
|
|
typedef struct student{
|
|
|
long num; //每个学生的学号
|
|
|
char name[MAX_LEN]; //每个学生的姓名
|
|
|
float score[COURSE_NUM]; //每个学生COURSE_NUM门功课的成绩
|
|
|
float sum; //每个学生的总成绩
|
|
|
float aver; //每个学生的平均成绩
|
|
|
}STU;
|
|
|
```
|
|
|
|
|
|
// 函数声明
|
|
|
|
|
|
```
|
|
|
int Menu(void); //菜单显示、用户输入函数
|
|
|
void ReadScore(STU stu[],int n,int m); //学生成绩录入函数
|
|
|
void AverSumofEveryStudent(STU stu[],int n,int m); //求每个学生总分和平均分的函数
|
|
|
void AverSumofEveryCourse(STU stu[],int n,int m); //求每门课程总分和平均分的函数
|
|
|
void SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b)); //选择法将数组 sum 的元素排序的函数
|
|
|
int Ascending(float a,float b); //使数据按升序排序的函数
|
|
|
int Descending(float a,float b); //使数据按降序排序的函数
|
|
|
void SwapFloat(float *x,float *y); //用于交换两个单精度浮点数的函数
|
|
|
void SwapLong(long *x,long *y); //用于交换两个长整型数据的函数
|
|
|
void SwapChar(char x[],char y[]); //用于交换两个字符串的函数
|
|
|
void AsSortbyNum(STU stu[],int n,int m); //选择法将数组 num 的元素值从低到高排序的函数
|
|
|
void SortbyName(STU stu[],int n,int m); //交换法实现字符串按字典顺序排序的函数
|
|
|
void SearchbyNum(STU stu[],int n,int m); //按学号查找学生成绩并显示查找结果的函数
|
|
|
void SearchbyName(STU stu[],int n,int m); //按姓名的字典顺序排出成绩表的函数
|
|
|
void StatisticAnalysis(STU stu[],int n,int m); //统计各分数段的学生人数及所占的百分比的函数
|
|
|
void PrintScore(STU stu[],int n,int m); //打印学生成绩的函数
|
|
|
void WritetoFile(STU record[],int n,int m); //输出学生信息到文件中的函数
|
|
|
void ReadfromFile(STU record[],int *n,int *m); //从文件中读取学生信息的函数
|
|
|
```
|
|
|
|
|
|
// 主函数
|
|
|
```
|
|
|
int main(void)
|
|
|
{
|
|
|
char ch;
|
|
|
int n = 0,m = 0;
|
|
|
STU stu[STU_NUM];
|
|
|
printf("Input student number(n<%d):",STU_NUM);
|
|
|
scnaf("%d",&n);
|
|
|
printf("Input course number(m<=%d):",COURSE_NUM);
|
|
|
scanf("%d",&m);
|
|
|
while(1)
|
|
|
{
|
|
|
ch = Menu(); //显示菜单,并读取用户输入
|
|
|
switch(ch)
|
|
|
{
|
|
|
case 1:ReadScore(stu,n,m);
|
|
|
break;
|
|
|
case 2:AverSumofEveryCourse(stu,n,m);
|
|
|
break;
|
|
|
case 3:AverSumofEveryStudent(stu,n,m);
|
|
|
break;
|
|
|
case 4:SortbyScore(stu,n,m,Descending);
|
|
|
printf("\nSort in descending order by score:\n");
|
|
|
PrintScore(stu,n,m);
|
|
|
break;
|
|
|
case 5:SortbyScore(stu,n,m,Ascending);
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
PrintScore(stu,n,m);
|
|
|
break;
|
|
|
case 6:AsSortbyNum(stu,n,m);
|
|
|
printf("\nSort in ascending order by number:\n");
|
|
|
PrintScore(stu,n,m);
|
|
|
break;
|
|
|
case 7:SortbyName(stu,n,m);
|
|
|
printf("\nSort in dictionary order by name:\n");
|
|
|
PrintScore(stu,n,m);
|
|
|
break;
|
|
|
case 8:SearchbyNum(stu,n,m);
|
|
|
break;
|
|
|
case 9:SearchbyName(stu,n,m);
|
|
|
break;
|
|
|
case 10:StatisticAnalysis(stu,n,m);
|
|
|
break;
|
|
|
case 11:PrintScore(stu,n,m);
|
|
|
break;
|
|
|
case 12:WritetoFile(stu,n,m);
|
|
|
break;
|
|
|
case 13:ReadfromFile(stu,&n,&m);
|
|
|
break;
|
|
|
case 0: printf("Are you sure you want to exit?(Y/N):");
|
|
|
printf("End of program!);
|
|
|
exit(0);
|
|
|
default:printf("Input error!");
|
|
|
}
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
// 函数功能:显示菜单并获得用户键盘输入的选项
|
|
|
```
|
|
|
int Menu(void)
|
|
|
{
|
|
|
int itemSelected;
|
|
|
printf("Management for Students' scores\n");
|
|
|
printf("1 Input record\n");
|
|
|
printf("2 Calculate total and average score of every course\n");
|
|
|
printf("3 Calculate total and average score of every student\n");
|
|
|
printf("4 Sort in descending order by score\n");
|
|
|
printf("5 Sort in ascending order by score\n");
|
|
|
printf("6 Sort in ascending order by number\n");
|
|
|
printf("7 Sort in dictionary order by name\n");
|
|
|
printf("8 Search by number\n");
|
|
|
printf("9 Search by name\n");
|
|
|
printf("10 Statistic analysis\n");
|
|
|
printf("11 List record\n");
|
|
|
printf("12 Write to a file\n");
|
|
|
printf("13 Read from a file\n");
|
|
|
printf("0 Exit\n");
|
|
|
printf("Please Input your choice:");
|
|
|
scanf("%d",&itemSelected); //读入用户输入
|
|
|
return itemSelected;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
// 函数功能:输入 n 个学生的 m 门课成绩
|
|
|
```
|
|
|
void ReadScore(STU stu[],int n,int m)
|
|
|
{
|
|
|
int i,j;
|
|
|
printf("Input student's ID ,name and score:\n");
|
|
|
for(i=0,i<n;i++) //输入n个同学
|
|
|
{
|
|
|
scanf("%ld%s",&stu[i].num,stu[i].name);
|
|
|
for(j=0;j<m;j++) //输入m门成绩
|
|
|
scanf("%f",&stu[i].score[j]);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:计算每个学生各门课程的总分和平均分
|
|
|
```
|
|
|
void AverSumofEveryStudent(STU stu[],int n,int m)
|
|
|
{
|
|
|
int i,j;
|
|
|
for(i=0;i<n;i++)
|
|
|
{
|
|
|
stu[i].sum = 0; //初始化每个学生总分为0
|
|
|
for(j=0;j<m;j++)
|
|
|
{
|
|
|
stu[i].sum = stu[i].sum + stu[i].score[j]; //累加分数
|
|
|
}
|
|
|
stu[i].aver = m>0 ? stu[i].sum /m : -1;
|
|
|
printf("student %d: sum = %.0f,aver = %.0f\n",i+1,stu[i].sum,stu[i].aver);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:计算每门课程的总分和平均分
|
|
|
```
|
|
|
void AverSumofEveryCourse(STU stu[],int n,int m)
|
|
|
{
|
|
|
int i,j;
|
|
|
float sum[COURSE_NUM],aver[COURSE_NUM];
|
|
|
for(j=0;j<m;j++)
|
|
|
{
|
|
|
sum[j] = 0;
|
|
|
for(i=0;i<n;i++)
|
|
|
{
|
|
|
sum[j]+= stu[i].score[j]; //计算n个同学每门课程的总分
|
|
|
|
|
|
}
|
|
|
aver[j] = sum[j]/i;
|
|
|
printf("course %d: sum = %.0f,aver = %.0f\n",j+1, sum[j], aver[j]);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:按选择法将数组 sum 的元素值排序
|
|
|
```
|
|
|
voidSortbyScore(STU stu[],int n,int m,int(*compare)(float a,float b))
|
|
|
{
|
|
|
int i, j, k, t;
|
|
|
for (i=0; i<n-1; i++)
|
|
|
{
|
|
|
k = i;
|
|
|
for (j=i+1; j<n; j++)
|
|
|
{
|
|
|
if ((*compare)(stu[j].sum, stu[k].sum)) k = j;
|
|
|
}
|
|
|
if (k != i)
|
|
|
{
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
{
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
}
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); //交换总分
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); //交换平均分
|
|
|
SwapLong(&stu[k].num, &stu[i].num); //交换学号
|
|
|
SwapChar(stu[k].name, stu[i].name); //交换姓名
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//使数据按升序排序
|
|
|
```
|
|
|
int Ascending(float a,float b)
|
|
|
{
|
|
|
return a < b; //这样比较决定了按升序排序,如果a<b,则交换
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//使数据按降序排序
|
|
|
```
|
|
|
int Descending(float a,float b)
|
|
|
{
|
|
|
return a > b; // 这样比较决定了按降序排序,如果 a>b,则交换
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//交换两个单精度浮点型数据
|
|
|
```
|
|
|
void SwapFloat(float *x,float *y)
|
|
|
{
|
|
|
float temp;
|
|
|
temp = *x;
|
|
|
*x = *y;
|
|
|
*y = temp;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//交换两个长整型数据
|
|
|
```
|
|
|
void SwapLong(long *x,long *y)
|
|
|
{
|
|
|
long temp;
|
|
|
temp = *x;
|
|
|
*x = *y;
|
|
|
*y = temp;
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//交换两个字符串
|
|
|
```
|
|
|
void SwapChar(char x[],chary[])
|
|
|
{
|
|
|
char temp[MAX_LEN];
|
|
|
strcpy(temp, x);
|
|
|
strcpy(x, y);
|
|
|
strcpy(y, temp);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:按选择法将数组 num 的元素从低到高排序
|
|
|
```
|
|
|
void AsSortbyNum(STU stu[],int n,int m)
|
|
|
{
|
|
|
int i, j, k, t;
|
|
|
for (i=0; i<n-1; i++)
|
|
|
{
|
|
|
int i, j, k, t;
|
|
|
k = i;
|
|
|
for (j=i+1; j<n; j++)
|
|
|
{
|
|
|
if (stu[j].num < stu[k].num) k = j;
|
|
|
}
|
|
|
if (k != i)
|
|
|
{
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
{
|
|
|
SwapFloat(&stu[k].score[t], &stu[i].score[t]);
|
|
|
}
|
|
|
SwapFloat(&stu[k].sum, &stu[i].sum); //交换总分
|
|
|
SwapFloat(&stu[k].aver, &stu[i].aver); //交换平均分
|
|
|
SwapLong(&stu[k].num, &stu[i].num); //交换学号
|
|
|
SwapChar(stu[k].name, stu[i].name); //交换姓名
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:交换法实现字符串按字典顺序排序
|
|
|
```
|
|
|
void SortbyName(STU stu[],int n,int m)
|
|
|
{
|
|
|
int i, j, t;
|
|
|
for (i=0; i<n-1; i++)
|
|
|
{
|
|
|
for (j = i+1; j<n; j++)
|
|
|
{
|
|
|
if (strcmp(stu[j].name, stu[i].name) < 0)
|
|
|
{
|
|
|
for (t=0; t<m; t++) //交换m门课程的成绩
|
|
|
{
|
|
|
SwapFloat(&stu[i].score[t], &stu[j].score[t]);
|
|
|
}
|
|
|
SwapFloat(&stu[i].sum, &stu[j].sum); //交换总分
|
|
|
SwapFloat(&stu[i].aver, &stu[j].aver); //交换平均分
|
|
|
SwapLong(&stu[i].num, &stu[j].num); //交换学号
|
|
|
SwapChar(stu[i].name, stu[j].name); //交换姓名
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:按学号查找学生成绩并显示查找结果
|
|
|
```
|
|
|
void SearchbyNum(STU stu[],int n,int m)
|
|
|
{
|
|
|
long number;
|
|
|
int i,j;
|
|
|
printf("Input the number you want to search:");
|
|
|
scanf("%ld",&number);
|
|
|
for(i=0;i<n;i++)
|
|
|
if(stu[i].num==number)
|
|
|
{
|
|
|
printf("\n");
|
|
|
for(j=0;j<m;j++)
|
|
|
printf("%d\t",stu[i].score[j]);
|
|
|
printf("\n");
|
|
|
break;
|
|
|
}
|
|
|
if(i==n)
|
|
|
printf("\nNot found!\n");
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:按姓名查找学生成绩并显示查找结果
|
|
|
```
|
|
|
void SearchbyName(STU stu[],int n,int m)
|
|
|
{
|
|
|
char x[MAX_LEN];
|
|
|
int i,j;
|
|
|
printf("Input the name you want to search:");
|
|
|
scanf("%s", x);
|
|
|
for(i=0;i<n;i++)
|
|
|
{
|
|
|
if(strcmp(stu[i].name,x)==0)
|
|
|
{
|
|
|
printf("%ld\t%s\t",stu[i].num,stu[i].name);
|
|
|
for(j=0;j<m;j++)
|
|
|
{
|
|
|
printf("%.0f\t",stu[i].score[j]);
|
|
|
}
|
|
|
printf(".0f\t%.0f\n",stu[i].sum,stu[i].aver);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
printf("\nNot found!\n);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//函数功能:统计各分数段的学生人数及所占的百分比
|
|
|
```
|
|
|
void StatisticAnalysis(STU stu[],int n,int m)
|
|
|
{
|
|
|
int i,j,t[6];
|
|
|
for(j=0;j<m;j++)
|
|
|
{
|
|
|
printf("For course %d:\n",j+1);
|
|
|
memset(t,0,sizeof(t)); //将数组 t 的全部元素初始化为0
|
|
|
for(i=0;i<n;i++)
|
|
|
{
|
|
|
if(stu[i].score[j]>=0&&stu[i].score[j]<60) t[0]++;
|
|
|
else if(stu[i].score[j]<70) t[1]++;
|
|
|
else if(stu[i].score[j]<80) t[2]++;
|
|
|
else if(stu[i].score[j]<90) t[3]++;
|
|
|
else if(stu[i].score[j]<100) t[4]++;
|
|
|
else if(stu[i].score[j]==100) t[5]++;
|
|
|
}
|
|
|
for(i=0;i<=5;i++)
|
|
|
{
|
|
|
if(i==0) printf("<60\t%d\t%.2f%%\n",t[i],(float)t[i]/n*100);
|
|
|
else if(i==5) printf("%d\t%d\t%.2f%%\n",(i+5)*10,t[i],(float)t[i]/n*100);
|
|
|
else printf("%d-%d\t%d\t%.2f%%\n",(i+5)*10,(i+5)*10+9,t[i],(float)t[i]/n*100);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
//函数功能:打印学生成绩
|
|
|
```
|
|
|
void PrintScore(STU stu[],int n,int m)
|
|
|
{
|
|
|
int i,j;
|
|
|
for (i=0; i<n; i++)
|
|
|
{
|
|
|
printf("%ld\t%s\t", stu[i].num, stu[i].name);
|
|
|
for (j=0; j<m; j++)
|
|
|
{
|
|
|
printf("%.0f\t", stu[i].score[j]);
|
|
|
}
|
|
|
printf("%.0f\t%.0f\n", stu[i].sum, stu[i].aver);
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//输出 n 个学生的学号、姓名及 m 门课程的成绩到文件 student.txt 中
|
|
|
```
|
|
|
void WritetoFile(STU stu[],int n,int m)
|
|
|
{
|
|
|
FILE *fp;
|
|
|
int i, j;
|
|
|
|
|
|
if((fp = fopen("student.txt","w")) == NULL)
|
|
|
{
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
fprintf(fp, "%d\t%d\n", n, m); //将学生人数和课程门数写入文件
|
|
|
for (i=0; i<n; i++)
|
|
|
{
|
|
|
fprintf(fp, "%10ld%10s", stu[i].num, stu[i].name);
|
|
|
for (j=0; j<m; j++)
|
|
|
{
|
|
|
fprintf(fp, "%10.0f", stu[i].score[j]);
|
|
|
}
|
|
|
fprintf(fp, "%10.0f%10.0f\n", stu[i].sum, stu[i].aver);
|
|
|
}
|
|
|
fclose(fp);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
//从文件中读取学生的学号、姓名及成绩等信息写入到结构体 stu 中
|
|
|
```
|
|
|
void ReadfromFile(STU stu[],int *n,int *m)
|
|
|
{
|
|
|
FILE *fp;
|
|
|
int i, j;
|
|
|
if ((fp = fopen("student.txt","r")) == NULL)
|
|
|
{
|
|
|
printf("Failure to open score.txt!\n");
|
|
|
exit(0);
|
|
|
}
|
|
|
fscanf(fp, "%d\t%d", n, m); //从文件中读出学生人数,课程门数
|
|
|
for (i=0; i<*n; i++) //学生人数保存在n指向的储存单元
|
|
|
{
|
|
|
fscanf(fp, "%10ld", &stu[i].num);
|
|
|
fscanf(fp, "%10s", stu[i].name);
|
|
|
for (j=0; j<*m; j++) //课程门数保存在m指向的储存单元
|
|
|
{
|
|
|
fscanf(fp, "%10f", &stu[i].score[j]); //不能用%10.0f
|
|
|
}
|
|
|
fscanf(fp, "%10f%10f", &stu[i].sum, &stu[i].aver); //不能用%10.0f
|
|
|
}
|
|
|
fclose(fp);
|
|
|
}
|
|
|
```
|
|
|
|
|
|
[def]: AsScortNum.svg |