用c语言写一个学生学籍管理系统 主要功能: 欢迎及菜单页面 通过文本文件读入学生信息 新增学生(学号不可重复) 录入成绩 删除学生 展示所有学生信息 计算最高最低平均成绩 统计ABCD每个分段的人数 按成绩从高到低排序 按学号排序 退出前将所有学生信息回写进文本文档
时间: 2023-06-27 11:00:57 浏览: 117
c语言学生学籍管理系统,功能强大
这是一个比较全面的学生学籍管理系统,需要一定的编程基础才能完成。下面是一个简单的参考代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 1000
typedef struct {
char id[20];
char name[20];
int score;
} Student;
Student students[MAX_STUDENT_NUM];
int student_num = 0;
void read_students_from_file() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("无法打开文件!\n");
return;
}
char line[100];
while (fgets(line, 100, fp) != NULL) {
sscanf(line, "%s %s %d", students[student_num].id, students[student_num].name, &students[student_num].score);
student_num++;
}
fclose(fp);
}
void save_students_to_file() {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("无法打开文件!\n");
return;
}
for (int i = 0; i < student_num; i++) {
fprintf(fp, "%s %s %d\n", students[i].id, students[i].name, students[i].score);
}
fclose(fp);
}
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生人数已达上限!\n");
return;
}
Student new_student;
printf("请输入学生学号:");
scanf("%s", new_student.id);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, new_student.id) == 0) {
printf("学号已存在!\n");
return;
}
}
printf("请输入学生姓名:");
scanf("%s", new_student.name);
printf("请输入学生成绩:");
scanf("%d", &new_student.score);
students[student_num++] = new_student;
printf("添加成功!\n");
}
void delete_student() {
char id[20];
printf("请输入要删除的学生学号:");
scanf("%s", id);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("删除成功!\n");
return;
}
}
printf("学号不存在!\n");
}
void show_all_students() {
printf("学号\t姓名\t成绩\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t%s\t%d\n", students[i].id, students[i].name, students[i].score);
}
}
void sort_by_score() {
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("排序成功!\n");
}
void sort_by_id() {
for (int i = 0; i < student_num - 1; i++) {
for (int j = 0; j < student_num - i - 1; j++) {
if (strcmp(students[j].id, students[j + 1].id) > 0) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("排序成功!\n");
}
void calculate_scores() {
int max_score = 0, min_score = 100, sum_score = 0;
int count_a = 0, count_b = 0, count_c = 0, count_d = 0;
for (int i = 0; i < student_num; i++) {
max_score = students[i].score > max_score ? students[i].score : max_score;
min_score = students[i].score < min_score ? students[i].score : min_score;
sum_score += students[i].score;
if (students[i].score >= 90) {
count_a++;
} else if (students[i].score >= 80) {
count_b++;
} else if (students[i].score >= 70) {
count_c++;
} else {
count_d++;
}
}
printf("最高分:%d\n", max_score);
printf("最低分:%d\n", min_score);
printf("平均分:%.2f\n", (float)sum_score / student_num);
printf("A段人数:%d\n", count_a);
printf("B段人数:%d\n", count_b);
printf("C段人数:%d\n", count_c);
printf("D段人数:%d\n", count_d);
}
int main() {
read_students_from_file();
while (1) {
printf("\n");
printf("学生学籍管理系统\n");
printf("1. 新增学生\n");
printf("2. 录入成绩\n");
printf("3. 删除学生\n");
printf("4. 展示所有学生信息\n");
printf("5. 计算最高最低平均成绩\n");
printf("6. 统计ABCD每个分段的人数\n");
printf("7. 按成绩从高到低排序\n");
printf("8. 按学号排序\n");
printf("9. 退出并保存\n");
printf("请选择操作:");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
printf("请输入要录入成绩的学生学号:");
char id[20];
scanf("%s", id);
for (int i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("请输入成绩:");
scanf("%d", &students[i].score);
printf("成绩录入成功!\n");
break;
}
if (i == student_num - 1) {
printf("学号不存在!\n");
}
}
break;
case 3:
delete_student();
break;
case 4:
show_all_students();
break;
case 5:
calculate_scores();
break;
case 6:
calculate_scores();
break;
case 7:
sort_by_score();
break;
case 8:
sort_by_id();
break;
case 9:
save_students_to_file();
printf("已保存并退出!\n");
return 0;
default:
printf("无效的操作!\n");
break;
}
}
}
```
需要注意的是,上述代码中使用了一个结构体 `Student` 来存储学生信息,其中包括学号、姓名和成绩。同时,我们将所有的学生信息存储在一个全局数组 `students` 中,最大长度为 `MAX_STUDENT_NUM`。在主函数中,我们通过 `read_students_from_file` 函数从文件中读取学生信息,并通过 `save_students_to_file` 函数将所有学生信息写回文件。对于其他的功能,我们分别定义了相应的函数来实现。
阅读全文