#include <stdio.h> #include <string.h> #define MAX_STUDENT_NUM 30 //最大学生个数 #define MAX_COURSE_NUM 6 //最大课程个数 #define NAME_LEN 20 //最大名字个数 typedef struct Student { char id[10];//学生学号 char name[NAME_LEN];//姓名 int scores[MAX_COURSE_NUM];//各科分数 int total_score;//总分 float avg_score;//平均分 } XUESHENG;//将结构体名称命名为XUESHENG void sort_by_total_score(XUESHENG students[], int num);//按每个学生的总分由高到低排出并输出名次表 void sort_by_number(XUESHENG students[], int num);//按学号由小到大排出并输出成绩表 void sort_by_name(XUESHENG students[], int num);//按姓名的字典顺序排出并输出成绩表 请补充各个函数
时间: 2024-01-16 22:02:02 浏览: 66
毕业论文设计--学生成绩管理系统千行代码c语言课程设计正文.doc
函数1:按每个学生的总分由高到低排出并输出名次表
```
void sort_by_total_score(XUESHENG students[], int num){
int i, j, rank = 1;
XUESHENG temp;
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
if(students[i].total_score < students[j].total_score){
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("名次\t学号\t姓名\t总分\t平均分\n");
for(i = 0; i < num; i++){
printf("%d\t%s\t%s\t%d\t%.2f\n", rank++, students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
```
函数2:按学号由小到大排出并输出成绩表
```
void sort_by_number(XUESHENG students[], int num){
int i, j;
XUESHENG temp;
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
if(strcmp(students[i].id, students[j].id) > 0){
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("学号\t姓名\t");
for(i = 0; i < MAX_COURSE_NUM; i++){
printf("科目%d\t", i+1);
}
printf("总分\t平均分\n");
for(i = 0; i < num; i++){
printf("%s\t%s\t", students[i].id, students[i].name);
for(j = 0; j < MAX_COURSE_NUM; j++){
printf("%d\t", students[i].scores[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].avg_score);
}
}
```
函数3:按姓名的字典顺序排出并输出成绩表
```
void sort_by_name(XUESHENG students[], int num){
int i, j;
XUESHENG temp;
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
if(strcmp(students[i].name, students[j].name) > 0){
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("学号\t姓名\t");
for(i = 0; i < MAX_COURSE_NUM; i++){
printf("科目%d\t", i+1);
}
printf("总分\t平均分\n");
for(i = 0; i < num; i++){
printf("%s\t%s\t", students[i].id, students[i].name);
for(j = 0; j < MAX_COURSE_NUM; j++){
printf("%d\t", students[i].scores[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].avg_score);
}
}
```
阅读全文