用C语言编写一个学生成绩管理系统
时间: 2023-08-22 19:09:35 浏览: 22
好的,下面是一个简单的学生成绩管理系统的实现,主要包括学生信息的录入、查询、修改、删除、统计和排序等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数量
#define MAX_NAME_LEN 20 // 姓名最大长度
// 学生结构体
typedef struct {
char name[MAX_NAME_LEN]; // 姓名
int id; // 学号
int age; // 年龄
char gender; // 性别
int score[3]; // 三门课程成绩
} Student;
// 全局变量,存储所有学生信息
Student students[MAX_STUDENT_NUM];
int student_num = 0; // 学生数量
// 显示菜单
void display_menu() {
printf("========================================\n");
printf("1. 录入学生信息\n");
printf("2. 查询学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生信息\n");
printf("5. 统计学生成绩\n");
printf("6. 排序学生成绩\n");
printf("7. 退出程序\n");
printf("========================================\n");
}
// 录入学生信息
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("已经达到最大学生数量,无法添加新学生!\n");
return;
}
printf("请输入学生姓名:");
scanf("%s", students[student_num].name);
printf("请输入学号:");
scanf("%d", &students[student_num].id);
printf("请输入年龄:");
scanf("%d", &students[student_num].age);
printf("请输入性别(M/F):");
scanf(" %c", &students[student_num].gender);
printf("请输入三门课程成绩:");
scanf("%d %d %d", &students[student_num].score[0], &students[student_num].score[1], &students[student_num].score[2]);
student_num++; // 学生数量加一
printf("学生信息录入成功!\n");
}
// 查询学生信息
void search_student() {
int id;
char name[MAX_NAME_LEN];
printf("请输入要查询的学生信息:\n");
printf("1. 按学号查询\n");
printf("2. 按姓名查询\n");
int option;
scanf("%d", &option);
if (option == 1) {
printf("请输入学号:");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("姓名:%s 学号:%d 年龄:%d 性别:%c 成绩:%d %d %d\n", students[i].name, students[i].id, students[i].age, students[i].gender, students[i].score[0], students[i].score[1], students[i].score[2]);
return;
}
}
printf("未找到学号为 %d 的学生!\n", id);
} else if (option == 2) {
printf("请输入姓名:");
scanf("%s", name);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("姓名:%s 学号:%d 年龄:%d 性别:%c 成绩:%d %d %d\n", students[i].name, students[i].id, students[i].age, students[i].gender, students[i].score[0], students[i].score[1], students[i].score[2]);
return;
}
}
printf("未找到姓名为 %s 的学生!\n", name);
} else {
printf("输入有误!\n");
}
}
// 修改学生信息
void modify_student() {
int id;
printf("请输入要修改的学生学号:");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("该学生原信息为:姓名:%s 学号:%d 年龄:%d 性别:%c 成绩:%d %d %d\n", students[i].name, students[i].id, students[i].age, students[i].gender, students[i].score[0], students[i].score[1], students[i].score[2]);
printf("请输入学生姓名:");
scanf("%s", students[i].name);
printf("请输入学号:");
scanf("%d", &students[i].id);
printf("请输入年龄:");
scanf("%d", &students[i].age);
printf("请输入性别(M/F):");
scanf(" %c", &students[i].gender);
printf("请输入三门课程成绩:");
scanf("%d %d %d", &students[i].score[0], &students[i].score[1], &students[i].score[2]);
printf("学生信息修改成功!\n");
return;
}
}
printf("未找到学号为 %d 的学生!\n", id);
}
// 删除学生信息
void delete_student() {
int id;
printf("请输入要删除的学生学号:");
scanf("%d", &id);
for (int i = 0; i < student_num; i++) {
if (students[i].id == id) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--; // 学生数量减一
printf("学生信息删除成功!\n");
return;
}
}
printf("未找到学号为 %d 的学生!\n", id);
}
// 统计学生成绩
void statistic_student() {
int total_score[MAX_STUDENT_NUM];
int max_score = 0, min_score = 100, total = 0;
float avg_score;
for (int i = 0; i < student_num; i++) {
total_score[i] = students[i].score[0] + students[i].score[1] + students[i].score[2];
total += total_score[i];
if (total_score[i] > max_score) {
max_score = total_score[i];
}
if (total_score[i] < min_score) {
min_score = total_score[i];
}
}
avg_score = (float) total / student_num;
printf("学生总人数:%d\n", student_num);
printf("总分:%d 平均分:%.2f 最高分:%d 最低分:%d\n", total, avg_score, max_score, min_score);
}
// 排序学生成绩
void sort_student() {
int option;
printf("请选择排序方式:\n");
printf("1. 总分排序\n");
printf("2. 单科成绩排序\n");
scanf("%d", &option);
int total_score[MAX_STUDENT_NUM];
for (int i = 0; i < student_num; i++) {
total_score[i] = students[i].score[0] + students[i].score[1] + students[i].score[2];
}
if (option == 1) {
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (total_score[j] < total_score[j + 1]) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
int temp_score = total_score[j];
total_score[j] = total_score[j + 1];
total_score[j + 1] = temp_score;
}
}
}
} else if (option == 2) {
int subject;
printf("请选择要排序的科目(1-3):");
scanf("%d", &subject);
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (students[j].score[subject - 1] < students[j + 1].score[subject - 1]) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
int temp_score = total_score[j];
total_score[j] = total_score[j + 1];
total_score[j + 1] = temp_score;
}
}
}
} else {
printf("输入有误!\n");
}
printf("学生成绩排序完成!\n");
}
int main() {
while (1) {
display_menu();
int option;
printf("请输入要执行的操作序号:");
scanf("%d", &option);
switch (option) {
case 1:
add_student();
break;
case 2:
search_student();
break;
case 3:
modify_student();
break;
case 4:
delete_student();
break;
case 5:
statistic_student();
break;
case 6:
sort_student();
break;
case 7:
printf("程序已退出!\n");
return 0;
default:
printf("输入有误,请重新输入!\n");
break;
}
}
}
```
这个实现比较简单,只是提供一个基本的框架,你可以根据需要进行修改和完善。希望对你有所帮助!
相关推荐













