帮我用C语言写一个学生成绩管理系统
时间: 2023-12-29 11:16:09 浏览: 54
好的,以下是一个简单的学生成绩管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最大学生数
#define NAME_LENGTH 20 // 学生姓名最大长度
#define COURSE_NUM 3 // 学生科目数
// 学生信息结构体
typedef struct {
char name[NAME_LENGTH];
int scores[COURSE_NUM];
int total_score;
float avg_score;
} Student;
// 学生管理系统结构体
typedef struct {
Student students[MAX_STUDENTS]; // 学生数组
int student_count; // 学生数量
} StudentManager;
// 显示菜单
void show_menu() {
printf("1. 添加学生信息\n");
printf("2. 显示所有学生信息\n");
printf("3. 按姓名查询学生信息\n");
printf("4. 按总分排序并显示学生信息\n");
printf("5. 退出\n");
}
// 添加学生信息
void add_student(StudentManager* manager) {
if (manager->student_count >= MAX_STUDENTS) {
printf("学生数量已达到上限,无法添加新学生!\n");
return;
}
Student new_student;
printf("请输入学生姓名:");
scanf("%s", new_student.name);
printf("请输入学生各科成绩:\n");
for (int i = 0; i < COURSE_NUM; i++) {
printf("科目 %d:", i + 1);
scanf("%d", &new_student.scores[i]);
}
// 计算总分和平均分
new_student.total_score = 0;
for (int i = 0; i < COURSE_NUM; i++) {
new_student.total_score += new_student.scores[i];
}
new_student.avg_score = (float)new_student.total_score / COURSE_NUM;
// 添加学生到管理系统中
manager->students[manager->student_count] = new_student;
manager->student_count++;
printf("成功添加学生信息!\n");
}
// 显示所有学生信息
void show_all_students(StudentManager* manager) {
if (manager->student_count == 0) {
printf("学生数量为0,无法显示学生信息!\n");
return;
}
printf("所有学生信息如下:\n");
printf("姓名\t科目1\t科目2\t科目3\t总分\t平均分\n");
for (int i = 0; i < manager->student_count; i++) {
Student student = manager->students[i];
printf("%s\t", student.name);
for (int j = 0; j < COURSE_NUM; j++) {
printf("%d\t", student.scores[j]);
}
printf("%d\t%.2f\n", student.total_score, student.avg_score);
}
}
// 按姓名查询学生信息
void search_student_by_name(StudentManager* manager) {
if (manager->student_count == 0) {
printf("学生数量为0,无法查询学生信息!\n");
return;
}
char name[NAME_LENGTH];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int found = 0;
printf("查询到的学生信息如下:\n");
printf("姓名\t科目1\t科目2\t科目3\t总分\t平均分\n");
for (int i = 0; i < manager->student_count; i++) {
Student student = manager->students[i];
if (strcmp(student.name, name) == 0) {
printf("%s\t", student.name);
for (int j = 0; j < COURSE_NUM; j++) {
printf("%d\t", student.scores[j]);
}
printf("%d\t%.2f\n", student.total_score, student.avg_score);
found = 1;
}
}
if (!found) {
printf("未查询到学生信息!\n");
}
}
// 比较函数,用于按总分排序学生信息
int compare_students_by_total_score(const void* a, const void* b) {
Student* student_a = (Student*)a;
Student* student_b = (Student*)b;
if (student_a->total_score < student_b->total_score) {
return 1;
} else if (student_a->total_score > student_b->total_score) {
return -1;
} else {
return 0;
}
}
// 按总分排序并显示学生信息
void sort_and_show_students_by_total_score(StudentManager* manager) {
if (manager->student_count == 0) {
printf("学生数量为0,无法排序和显示学生信息!\n");
return;
}
// 按总分排序
qsort(manager->students, manager->student_count, sizeof(Student), compare_students_by_total_score);
// 显示排序后的学生信息
printf("按总分排序后的学生信息如下:\n");
printf("姓名\t科目1\t科目2\t科目3\t总分\t平均分\n");
for (int i = 0; i < manager->student_count; i++) {
Student student = manager->students[i];
printf("%s\t", student.name);
for (int j = 0; j < COURSE_NUM; j++) {
printf("%d\t", student.scores[j]);
}
printf("%d\t%.2f\n", student.total_score, student.avg_score);
}
}
int main() {
StudentManager manager;
manager.student_count = 0;
int choice = 0;
do {
show_menu();
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(&manager);
break;
case 2:
show_all_students(&manager);
break;
case 3:
search_student_by_name(&manager);
break;
case 4:
sort_and_show_students_by_total_score(&manager);
break;
case 5:
printf("谢谢使用!\n");
break;
default:
printf("无效选项,请重新选择!\n");
}
} while (choice != 5);
return 0;
}
```
该学生成绩管理系统实现了添加学生信息、显示所有学生信息、按姓名查询学生信息、按总分排序并显示学生信息等功能。在程序中使用了结构体、函数等C语言特性,可以作为学生们学习C语言的一个小项目。
阅读全文