用c语言写出对一个有N个学生的班级,每个学生有3门课程,分别为语文,英语,数学。该系统实现对班级成绩的录入、显示、修改、排序、保存等操作的管理。功能要求: (1)本系统采用一个结构体数组,每个数据的结构应当包括:学号、姓名、M门课程名称。 (2)本系统显示这样的菜单: 系统功能选项: a、 数据导入及显示 b、新记录输入 c、 删除记录 d、成绩查询 e、 成绩修改( 要求先输入密码) f、 成绩排序 (可按单科排序或按平均分排序) b、 成绩保存 c、 成绩统计 1)显示每门课程成绩最高的学生的基本信息 2)显示每门课程的平均成绩 3)显示超过某门课程平均成绩的学生人数 g、 退出系统 注意:需保证学号唯一性 (该题目用数组完成)
时间: 2024-01-12 12:04:47 浏览: 65
学生成绩管理系统,c语言写的
好的,以下是用C语言实现的班级成绩管理系统代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
#define MAX_COURSE_NUM 3
typedef struct {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
int score[MAX_COURSE_NUM];
} Student;
int num_students = 0; // 学生总数
Student students[MAX_STUDENTS]; // 存储学生信息的数组
void print_menu();
void load_data();
void input_new_record();
void delete_record();
void query_score();
void modify_score();
int verify_password();
void sort_score();
void save_data();
void statistics();
int main() {
char choice;
while (1) {
print_menu();
scanf(" %c", &choice);
switch (choice) {
case 'a':
load_data();
break;
case 'b':
input_new_record();
break;
case 'c':
delete_record();
break;
case 'd':
query_score();
break;
case 'e':
modify_score();
break;
case 'f':
sort_score();
break;
case 'g':
save_data();
break;
case 'h':
statistics();
break;
case 'i':
return 0;
default:
printf("Invalid choice, please choose again.\n");
}
}
}
void print_menu() {
printf("系统功能选项:\n");
printf("a、 数据导入及显示\n");
printf("b、 新记录输入\n");
printf("c、 删除记录\n");
printf("d、 成绩查询\n");
printf("e、 成绩修改\n");
printf("f、 成绩排序\n");
printf("g、 成绩保存\n");
printf("h、 成绩统计\n");
printf("i、 退出系统\n");
}
void load_data() {
FILE *fp;
char filename[100];
printf("请输入数据文件名:");
scanf("%s", filename);
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("打开文件失败!\n");
return;
}
fread(&num_students, sizeof(num_students), 1, fp);
fread(students, sizeof(Student), num_students, fp);
printf("共读取到%d条记录:\n", num_students);
for (int i = 0; i < num_students; i++) {
printf("%s %s %d %d %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]);
}
fclose(fp);
}
void input_new_record() {
if (num_students >= MAX_STUDENTS) {
printf("学生记录已满,无法录入新记录!\n");
return;
}
printf("请输入学生信息:\n");
printf("学号:");
scanf("%s", students[num_students].id);
// 检查学号是否唯一
for (int i = 0; i < num_students; i++) {
if (strcmp(students[num_students].id, students[i].id) == 0) {
printf("学号已存在,无法录入新记录!\n");
return;
}
}
printf("姓名:");
scanf("%s", students[num_students].name);
for (int i = 0; i < MAX_COURSE_NUM; i++) {
printf("请输入%s成绩:", i == 0 ? "语文" : (i == 1 ? "英语" : "数学"));
scanf("%d", &students[num_students].score[i]);
}
num_students++;
}
void delete_record() {
char id[MAX_ID_LEN];
int found = 0;
printf("请输入要删除的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
found = 1;
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
printf("删除成功!\n");
break;
}
}
if (!found) {
printf("未找到该学生记录!\n");
}
}
void query_score() {
char id[MAX_ID_LEN];
int found = 0;
printf("请输入要查询的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
found = 1;
printf("%s %s %d %d %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]);
break;
}
}
if (!found) {
printf("未找到该学生记录!\n");
}
}
void modify_score() {
char id[MAX_ID_LEN];
int found = 0;
int password;
printf("请输入要修改成绩的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
found = 1;
password = verify_password();
if (password) {
printf("请输入新的成绩:\n");
for (int j = 0; j < MAX_COURSE_NUM; j++) {
printf("%s:", j == 0 ? "语文" : (j == 1 ? "英语" : "数学"));
scanf("%d", &students[i].score[j]);
}
printf("成绩修改成功!\n");
} else {
printf("密码错误,无法修改成绩!\n");
}
break;
}
}
if (!found) {
printf("未找到该学生记录!\n");
}
}
int verify_password() {
int password;
printf("请输入密码:");
scanf("%d", &password);
return password == 123456;
}
void sort_score() {
int choice;
printf("请选择排序方式:\n");
printf("1、按语文成绩排序\n");
printf("2、按英语成绩排序\n");
printf("3、按数学成绩排序\n");
printf("4、按平均分排序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - 1 - i; j++) {
if (students[j].score[0] < students[j + 1].score[0]) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("按语文成绩排序结果:\n");
for (int i = 0; i < num_students; i++) {
printf("%s %s %d %d %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]);
}
break;
case 2:
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - 1 - i; j++) {
if (students[j].score[1] < students[j + 1].score[1]) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("按英语成绩排序结果:\n");
for (int i = 0; i < num_students; i++) {
printf("%s %s %d %d %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]);
}
break;
case 3:
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - 1 - i; j++) {
if (students[j].score[2] < students[j + 1].score[2]) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("按数学成绩排序结果:\n");
for (int i = 0; i < num_students; i++) {
printf("%s %s %d %d %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]);
}
break;
case 4:
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - 1 - i; j++) {
if ((students[j].score[0] + students[j].score[1] + students[j].score[2]) / 3.0 < (students[j + 1].score[0] + students[j + 1].score[1] + students[j + 1].score[2]) / 3.0) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("按平均分排序结果:\n");
for (int i = 0; i < num_students; i++) {
printf("%s %s %d %d %d\n", students[i].id, students[i].name, students[i].score[0], students[i].score[1], students[i].score[2]);
}
break;
default:
printf("无效的排序方式!\n");
}
}
void save_data() {
FILE *fp;
char filename[100];
printf("请输入数据文件名:");
scanf("%s", filename);
fp = fopen(filename, "wb");
if (fp == NULL) {
printf("打开文件失败!\n");
return;
}
fwrite(&num_students, sizeof(num_students), 1, fp);
fwrite(students, sizeof(Student), num_students, fp);
printf("数据保存成功!\n");
fclose(fp);
}
void statistics() {
int max_scores[MAX_COURSE_NUM] = {0};
int total_scores[MAX_COURSE_NUM] = {0};
int num_students_above_avg[MAX_COURSE_NUM] = {0};
double avg_scores[MAX_COURSE_NUM];
// 计算每门课程成绩最高的学生的基本信息
for (int i = 0; i < MAX_COURSE_NUM; i++) {
for (int j = 0; j < num_students; j++) {
if (students[j].score[i] > students[max_scores[i]].score[i]) {
max_scores[i] = j;
}
}
printf("%s成绩最高的学生信息:\n", i == 0 ? "语文" : (i ==
阅读全文