设计一个成绩处理程序,处理m个学生、n门课程(至少5门课)的成绩。用结构体来存储数据,结构体的成员包括:学号、姓名、性别、n门课成绩。先输入这些学号、姓名、性别、n门课成绩,然后显示功能菜单,调用功能函数,完成包括以下功能: (1)求每个学生n门课程成绩的总分、平均分 (2)求每门课程成绩的平均分 (3)按输入成绩的顺序,显示指定第i个人的总分和平均分 (4)按输入成绩的顺序,显示指定第i个人的第j门课成绩,并显示改成绩是高 于课程平均分还是低于课程平均分。 (5)求每个学生按平均分的排名 (6)数字0,则退出程序(结束运行)
时间: 2024-01-06 17:03:51 浏览: 29
好的,这是一个比较完整的程序,我会尽力为您解答。以下是程序代码,您可以参考学习:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_SUBJECT_NUM 5
#define MAX_STUDENT_NUM 100
typedef struct {
char id[20];
char name[MAX_NAME_LENGTH];
char gender[5];
float score[MAX_SUBJECT_NUM];
float sum_score;
float avg_score;
int rank;
} student;
student students[MAX_STUDENT_NUM];
int student_num;
int subject_num;
void input_students() {
printf("请输入学生数:");
scanf("%d", &student_num);
printf("请输入课程数:");
scanf("%d", &subject_num);
for (int i = 0; i < student_num; i++) {
student *s = &students[i];
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%s", s->id);
printf("姓名:");
scanf("%s", s->name);
printf("性别:");
scanf("%s", s->gender);
printf("成绩:");
for (int j = 0; j < subject_num; j++) {
scanf("%f", &s->score[j]);
s->sum_score += s->score[j];
}
s->avg_score = s->sum_score / subject_num;
}
}
void show_menu() {
printf("\n请选择要执行的操作:\n");
printf("1、求每个学生n门课程成绩的总分、平均分\n");
printf("2、求每门课程成绩的平均分\n");
printf("3、按输入成绩的顺序,显示指定第i个人的总分和平均分\n");
printf("4、按输入成绩的顺序,显示指定第i个人的第j门课成绩,并显示该成绩是高于课程平均分还是低于课程平均分\n");
printf("5、求每个学生按平均分的排名\n");
printf("0、退出程序\n");
}
void show_student_score() {
printf("学号\t\t姓名\t\t总分\t\t平均分\n");
for (int i = 0; i < student_num; i++) {
student *s = &students[i];
printf("%s\t\t%s\t\t%.2f\t\t%.2f\n", s->id, s->name, s->sum_score, s->avg_score);
}
}
void show_subject_score() {
printf("课程\t\t平均分\n");
for (int i = 0; i < subject_num; i++) {
float sum_score = 0;
for (int j = 0; j < student_num; j++) {
student *s = &students[j];
sum_score += s->score[i];
}
float avg_score = sum_score / student_num;
printf("课程%d\t\t%.2f\n", i + 1, avg_score);
}
}
void show_one_student_score() {
int i;
printf("请输入要查询的学生序号:");
scanf("%d", &i);
if (i > 0 && i <= student_num) {
student *s = &students[i - 1];
printf("学号:%s\n", s->id);
printf("姓名:%s\n", s->name);
printf("总分:%.2f\n", s->sum_score);
printf("平均分:%.2f\n", s->avg_score);
} else {
printf("查询失败,学生序号不存在!\n");
}
}
void show_one_student_subject_score() {
int i, j;
printf("请输入要查询的学生序号和课程序号,用空格隔开:");
scanf("%d %d", &i, &j);
if (i > 0 && i <= student_num && j > 0 && j <= subject_num) {
student *s = &students[i - 1];
float score = s->score[j - 1];
float avg_score = 0;
for (int k = 0; k < student_num; k++) {
student *t = &students[k];
avg_score += t->score[j - 1];
}
avg_score /= student_num;
printf("学号:%s\n", s->id);
printf("姓名:%s\n", s->name);
printf("成绩:%.2f\n", score);
if (score > avg_score) {
printf("该成绩高于课程平均分!\n");
} else if (score < avg_score) {
printf("该成绩低于课程平均分!\n");
} else {
printf("该成绩等于课程平均分!\n");
}
} else {
printf("查询失败,学生序号或课程序号不存在!\n");
}
}
void sort_students() {
for (int i = 0; i < student_num; i++) {
int r = 1;
student *s = &students[i];
for (int j = 0; j < student_num; j++) {
student *t = &students[j];
if (s->avg_score < t->avg_score) {
r++;
}
}
s->rank = r;
}
}
void show_student_rank() {
printf("学号\t\t姓名\t\t平均分\t\t排名\n");
sort_students();
for (int i = 0; i < student_num; i++) {
student *s = &students[i];
printf("%s\t\t%s\t\t%.2f\t\t%d\n", s->id, s->name, s->avg_score, s->rank);
}
}
void run() {
int choice = -1;
while (choice != 0) {
show_menu();
printf("请选择要执行的操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
show_student_score();
break;
case 2:
show_subject_score();
break;
case 3:
show_one_student_score();
break;
case 4:
show_one_student_subject_score();
break;
case 5:
show_student_rank();
break;
case 0:
printf("程序已退出!\n");
break;
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
}
int main() {
input_students();
run();
return 0;
}
```
这个程序比较长,但是它实现了题目中要求的所有功能。如果有任何不理解的地方,可以随时问我。
阅读全文