c语言结构体实现学生成绩系统,功能包括创建成绩功能:根据键盘输入创建各班学生各学科成绩信息,根据班级查找功能:根据输入的班级信息,查询改班学生的分数信息及班级排名。根据课程查找功能:根据输入的课程名称,查询该课程的均分、学生排名、分数。根据学生查找功能:根据输入的学生信息,查询该生的各科成绩、总分及排名
时间: 2024-03-24 10:39:40 浏览: 112
好的,下面是一个简单的C语言程序,用结构体实现学生成绩管理系统,包括创建成绩、根据班级、课程、学生查找功能:
```c
#include <stdio.h>
#include <string.h>
#define MAX_CLASS 5 // 最大班级数
#define MAX_STUDENT 30 // 最大学生数
#define MAX_SUBJECT 3 // 最大科目数
// 学生信息结构体
typedef struct student {
char name[20]; // 姓名
int score[MAX_SUBJECT]; // 分数
int total_score; // 总分
int class_no; // 班级号
int rank; // 排名
} Student;
// 班级信息结构体
typedef struct class {
char name[10]; // 班级名称
Student students[MAX_STUDENT]; // 学生信息
int student_num; // 学生数量
} Class;
// 科目信息结构体
typedef struct subject {
char name[10]; // 科目名称
int total_score; // 科目总分
int pass_score; // 及格分数
} Subject;
// 成绩信息结构体
typedef struct score {
Class classes[MAX_CLASS]; // 班级信息
int class_num; // 班级数量
Subject subjects[MAX_SUBJECT]; // 科目信息
int subject_num; // 科目数量
} Score;
// 初始化学生成绩
void init_score(Score *score) {
// 初始化班级信息
strcpy(score->classes[0].name, "Class1");
score->classes[0].student_num = 0;
score->class_num = 1;
// 初始化科目信息
strcpy(score->subjects[0].name, "Math");
score->subjects[0].total_score = 100;
score->subjects[0].pass_score = 60;
strcpy(score->subjects[1].name, "English");
score->subjects[1].total_score = 100;
score->subjects[1].pass_score = 60;
score->subject_num = 2;
}
// 创建成绩
void create_score(Score *score) {
int class_no, student_no;
printf("请输入班级号(1-%d): ", score->class_num);
scanf("%d", &class_no);
if (class_no < 1 || class_no > score->class_num) {
printf("班级号输入有误!\n");
return;
}
Class *class = &score->classes[class_no-1];
printf("请输入学生姓名: ");
scanf("%s", class->students[class->student_num].name);
class->students[class->student_num].class_no = class_no;
for (int i = 0; i < score->subject_num; i++) {
printf("请输入%s成绩: ", score->subjects[i].name);
scanf("%d", &class->students[class->student_num].score[i]);
class->students[class->student_num].total_score += class->students[class->student_num].score[i];
}
class->student_num++;
printf("创建成功!\n");
}
// 根据班级查找
void search_by_class(Score *score) {
int class_no;
printf("请输入班级号(1-%d): ", score->class_num);
scanf("%d", &class_no);
if (class_no < 1 || class_no > score->class_num) {
printf("班级号输入有误!\n");
return;
}
Class *class = &score->classes[class_no-1];
printf("班级名称:%s\n", class->name);
printf("学生列表:\n");
for (int i = 0; i < class->student_num; i++) {
printf("%d. %s\n", i+1, class->students[i].name);
}
printf("请输入学生序号(1-%d): ", class->student_num);
int student_no;
scanf("%d", &student_no);
if (student_no < 1 || student_no > class->student_num) {
printf("学生序号输入有误!\n");
return;
}
Student *student = &class->students[student_no-1];
printf("%s的成绩:\n", student->name);
for (int i = 0; i < score->subject_num; i++) {
printf("%s:%d\n", score->subjects[i].name, student->score[i]);
}
printf("总分:%d\n", student->total_score);
printf("排名:%d\n", student->rank);
}
// 根据课程查找
void search_by_subject(Score *score) {
char subject_name[10];
printf("请输入科目名称: ");
scanf("%s", subject_name);
int subject_no = -1;
for (int i = 0; i < score->subject_num; i++) {
if (strcmp(subject_name, score->subjects[i].name) == 0) {
subject_no = i;
break;
}
}
if (subject_no == -1) {
printf("科目名称输入有误!\n");
return;
}
printf("%s成绩:\n", subject_name);
double total_score = 0;
for (int i = 0; i < score->class_num; i++) {
Class *class = &score->classes[i];
printf("%s:\n", class->name);
for (int j = 0; j < class->student_num; j++) {
Student *student = &class->students[j];
total_score += student->score[subject_no];
}
double avg_score = total_score / class->student_num;
printf("平均分:%f\n", avg_score);
printf("排名:\n");
for (int j = 0; j < class->student_num; j++) {
Student *student = &class->students[j];
if (student->score[subject_no] >= score->subjects[subject_no].pass_score) {
printf("%s:%d\n", student->name, student->score[subject_no]);
}
}
}
}
// 根据学生查找
void search_by_student(Score *score) {
char name[20];
printf("请输入学生姓名: ");
scanf("%s", name);
printf("学生列表:\n");
for (int i = 0; i < score->class_num; i++) {
Class *class = &score->classes[i];
for (int j = 0; j < class->student_num; j++) {
Student *student = &class->students[j];
if (strcmp(name, student->name) == 0) {
printf("%s %s:%d\n", class->name, student->name, student->total_score);
printf("各科成绩:\n");
for (int k = 0; k < score->subject_num; k++) {
printf("%s:%d\n", score->subjects[k].name, student->score[k]);
}
printf("排名:%d\n", student->rank);
return;
}
}
}
printf("学生姓名输入有误!\n");
}
// 计算排名
void calc_rank(Score *score) {
for (int i = 0; i < score->class_num; i++) {
Class *class = &score->classes[i];
for (int j = 0; j < class->student_num; j++) {
Student *student = &class->students[j];
student->rank = 1;
for (int k = 0; k < class->student_num; k++) {
if (k != j && class->students[k].total_score > student->total_score) {
student->rank++;
}
}
}
}
}
int main() {
Score score;
init_score(&score);
int choice;
do {
printf("1. 创建成绩\n");
printf("2. 根据班级查找\n");
printf("3. 根据课程查找\n");
printf("4. 根据学生查找\n");
printf("0. 退出程序\n");
printf("请输入选择(0-4): ");
scanf("%d", &choice);
switch (choice) {
case 1:
create_score(&score);
break;
case 2:
search_by_class(&score);
break;
case 3:
search_by_subject(&score);
break;
case 4:
search_by_student(&score);
break;
case 0:
printf("退出程序!\n");
break;
default:
printf("选择输入有误!\n");
break;
}
calc_rank(&score);
} while (choice != 0);
return 0;
}
```
这是一个简单的实现,实际应用中可能需要更多的功能和更完善的实现。
阅读全文