用c语言写一个学生管理系统实现以下功能:1)使用结构体、链表完成学生信息管理系统的学生信息结构体及链表创建,并使用文件输入输出流对系统中的信息进行保存、读取显示等; (2)实现录入学生信息功能函数; (3)实现查找学生信息功能函数; (4)实现删除学生信息功能函数; (5)实现修改学生信息功能函数; (6)实现插入学生信息功能函数; (7)实现学生成绩排名功能函数; (8)实现学生人数统计功能函数;(9)实现显示学生信息功能函数; (10)在主函数添加菜单主界面信息,并通过Switch-case语句来调用不同的功能函数实现函数调用;
时间: 2023-12-24 11:06:14 浏览: 86
以下是一个简单的学生管理系统的实现,包括上述所有功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
// 学生信息结构体
typedef struct student {
int id; // 学号
char name[MAX_NAME_LEN]; // 姓名
int score; // 分数
struct student *next; // 指向下一个节点的指针
} Student;
// 学生链表结构体
typedef struct student_list {
int count; // 学生数量
Student *head; // 指向链表头节点的指针
} StudentList;
// 显示菜单界面
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("8. 显示学生信息\n");
printf("0. 退出系统\n");
printf("----------------------\n");
}
// 创建一个学生结构体
Student *create_student(int id, char *name, int score) {
Student *stu = (Student *)malloc(sizeof(Student));
stu->id = id;
strcpy(stu->name, name);
stu->score = score;
stu->next = NULL;
return stu;
}
// 销毁一个学生结构体
void destroy_student(Student *stu) {
if (stu) {
free(stu);
}
}
// 初始化学生链表
void init_student_list(StudentList *list) {
list->count = 0;
list->head = NULL;
}
// 销毁学生链表
void destroy_student_list(StudentList *list) {
Student *p = list->head, *tmp;
while (p) {
tmp = p->next;
destroy_student(p);
p = tmp;
}
list->count = 0;
list->head = NULL;
}
// 向学生链表中插入一个节点
void insert_student(StudentList *list, Student *stu) {
if (!list->head) {
list->head = stu;
} else {
Student *p = list->head;
while (p->next) {
p = p->next;
}
p->next = stu;
}
list->count++;
}
// 从学生链表中删除一个节点
void delete_student(StudentList *list, Student *stu) {
if (!list->head) {
return;
}
if (list->head == stu) {
list->head = list->head->next;
} else {
Student *p = list->head;
while (p && p->next != stu) {
p = p->next;
}
if (p) {
p->next = stu->next;
}
}
destroy_student(stu);
list->count--;
}
// 查找学生信息
Student *find_student(StudentList *list, int id) {
Student *p = list->head;
while (p) {
if (p->id == id) {
return p;
}
p = p->next;
}
return NULL;
}
// 显示学生信息
void display_student_info(Student *stu) {
printf("学号:%d,姓名:%s,分数:%d\n", stu->id, stu->name, stu->score);
}
// 显示所有学生信息
void display_all_students(StudentList *list) {
printf("学生信息如下:\n");
Student *p = list->head;
while (p) {
display_student_info(p);
p = p->next;
}
}
// 录入学生信息
void input_student_info(StudentList *list) {
int id, score;
char name[MAX_NAME_LEN];
printf("请输入学号:");
scanf("%d", &id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入分数:");
scanf("%d", &score);
Student *stu = create_student(id, name, score);
insert_student(list, stu);
printf("录入成功!\n");
}
// 删除学生信息
void delete_student_info(StudentList *list) {
int id;
printf("请输入要删除的学生的学号:");
scanf("%d", &id);
Student *stu = find_student(list, id);
if (stu) {
delete_student(list, stu);
printf("删除成功!\n");
} else {
printf("未找到该学生!\n");
}
}
// 修改学生信息
void modify_student_info(StudentList *list) {
int id, score;
char name[MAX_NAME_LEN];
printf("请输入要修改的学生的学号:");
scanf("%d", &id);
Student *stu = find_student(list, id);
if (stu) {
printf("请输入新的姓名:");
scanf("%s", name);
printf("请输入新的分数:");
scanf("%d", &score);
strcpy(stu->name, name);
stu->score = score;
printf("修改成功!\n");
} else {
printf("未找到该学生!\n");
}
}
// 插入学生信息
void insert_student_info(StudentList *list) {
int id, score;
char name[MAX_NAME_LEN];
int pos;
printf("请输入插入位置(从0开始):");
scanf("%d", &pos);
if (pos < 0 || pos > list->count) {
printf("插入位置不合法!\n");
return;
}
printf("请输入学号:");
scanf("%d", &id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入分数:");
scanf("%d", &score);
Student *stu = create_student(id, name, score);
if (pos == 0) {
stu->next = list->head;
list->head = stu;
} else {
Student *p = list->head;
for (int i = 0; i < pos - 1; i++) {
p = p->next;
}
stu->next = p->next;
p->next = stu;
}
list->count++;
printf("插入成功!\n");
}
// 学生成绩排名
void rank_students_by_score(StudentList *list) {
if (!list->head) {
printf("没有学生信息!\n");
return;
}
Student *p, *q, *prev;
int swapped;
do {
swapped = 0;
prev = NULL;
p = list->head;
while (p->next) {
q = p->next;
if (p->score < q->score) {
swapped = 1;
if (prev) {
prev->next = q;
} else {
list->head = q;
}
p->next = q->next;
q->next = p;
prev = q;
} else {
prev = p;
p = p->next;
}
}
} while (swapped);
printf("学生成绩排名如下:\n");
display_all_students(list);
}
// 学生人数统计
void count_students(StudentList *list) {
printf("学生总人数为:%d\n", list->count);
}
// 主函数
int main() {
StudentList list;
init_student_list(&list);
// 从文件读取学生信息
FILE *fp;
if ((fp = fopen("students.dat", "rb")) != NULL) {
int count;
fread(&count, sizeof(int), 1, fp);
for (int i = 0; i < count; i++) {
Student stu;
fread(&stu, sizeof(Student), 1, fp);
insert_student(&list, create_student(stu.id, stu.name, stu.score));
}
fclose(fp);
printf("从文件中读取学生信息成功!\n");
} else {
printf("文件不存在或读取失败!\n");
}
int choice;
do {
display_menu();
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("退出系统!\n");
break;
case 1:
input_student_info(&list);
break;
case 2: {
int id;
printf("请输入要查找的学生的学号:");
scanf("%d", &id);
Student *stu = find_student(&list, id);
if (stu) {
display_student_info(stu);
} else {
printf("未找到该学生!\n");
}
break;
}
case 3:
delete_student_info(&list);
break;
case 4:
modify_student_info(&list);
break;
case 5:
insert_student_info(&list);
break;
case 6:
rank_students_by_score(&list);
break;
case 7:
count_students(&list);
break;
case 8:
display_all_students(&list);
break;
default:
printf("输入有误,请重新输入!\n");
break;
}
} while (choice != 0);
// 将学生信息保存到文件中
if ((fp = fopen("students.dat", "wb")) != NULL) {
fwrite(&list.count, sizeof(int), 1, fp);
Student *p = list.head;
while (p) {
fwrite(p, sizeof(Student), 1, fp);
p = p->next;
}
fclose(fp);
printf("学生信息保存到文件成功!\n");
} else {
printf("保存文件失败!\n");
}
destroy_student_list(&list); // 销毁链表
return 0;
}
```
该程序使用了结构体和链表来存储学生信息,并通过文件输入输出流将学生信息保存到文件中。菜单主界面通过 `switch-case` 语句来调用不同的功能函数。
阅读全文