You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
text3/README.md

932 lines
28 KiB

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