通过c语言实现编译程序通过链表和文件管理,支持录入,查询,修改,删除学生的信息和他的3门课的成绩。
时间: 2024-03-19 20:44:03 浏览: 18
好的,下面是一个C语言实现学生信息管理系统的示例程序,该程序通过链表和文件管理,支持录入、查询、修改、删除学生信息和他的3门课的成绩。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "student.txt" // 定义存储学生信息的文件名
// 定义学生结构体
typedef struct student {
char name[20];
int id;
float score[3];
struct student *next;
} Student;
// 读取文件中的学生信息,并返回一个链表头指针
Student *read_students_from_file() {
FILE *fp = fopen(FILE_NAME, "r");
if (fp == NULL) {
printf("文件不存在!\n");
return NULL;
}
Student *head = NULL;
Student *p = NULL;
while (!feof(fp)) {
Student *s = (Student *) malloc(sizeof(Student));
if (fscanf(fp, "%s %d %f %f %f\n", s->name, &s->id, &s->score[0], &s->score[1], &s->score[2]) == 5) {
s->next = NULL;
if (head == NULL) {
head = s;
} else {
p->next = s;
}
p = s;
}
}
fclose(fp);
return head;
}
// 将学生信息写入文件
void write_student_to_file(Student *head) {
FILE *fp = fopen(FILE_NAME, "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
Student *p = head;
while (p != NULL) {
fprintf(fp, "%s %d %.2f %.2f %.2f\n", p->name, p->id, p->score[0], p->score[1], p->score[2]);
p = p->next;
}
fclose(fp);
}
// 在链表中查找学生信息,返回该学生的指针
Student *find_student(Student *head, int id) {
Student *p = head;
while (p != NULL) {
if (p->id == id) {
return p;
}
p = p->next;
}
return NULL;
}
// 录入学生信息和他的3门课的成绩
void add_student(Student **head) {
Student *s = (Student *) malloc(sizeof(Student));
printf("请输入学生姓名:\n");
scanf("%s", s->name);
printf("请输入学生编号:\n");
scanf("%d", &s->id);
printf("请输入学生的3门课成绩:\n");
scanf("%f %f %f", &s->score[0], &s->score[1], &s->score[2]);
s->next = NULL;
if (*head == NULL) {
*head = s;
} else {
Student *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = s;
}
write_student_to_file(*head);
printf("学生信息已录入成功!\n");
}
// 查询学生信息和他的3门课的成绩
void query_student(Student *head) {
int id;
printf("请输入要查询的学生编号:\n");
scanf("%d", &id);
Student *s = find_student(head, id);
if (s != NULL) {
printf("姓名:%s\n", s->name);
printf("编号:%d\n", s->id);
printf("成绩:%.2f %.2f %.2f\n", s->score[0], s->score[1], s->score[2]);
} else {
printf("没有找到该学生!\n");
}
}
// 修改学生信息和他的3门课的成绩
void modify_student(Student *head) {
int id;
printf("请输入要修改的学生编号:\n");
scanf("%d", &id);
Student *s = find_student(head, id);
if (s != NULL) {
printf("请输入学生姓名:\n");
scanf("%s", s->name);
printf("请输入学生的3门课成绩:\n");
scanf("%f %f %f", &s->score[0], &s->score[1], &s->score[2]);
write_student_to_file(head);
printf("学生信息已修改成功!\n");
} else {
printf("没有找到该学生!\n");
}
}
// 删除学生信息和他的3门课的成绩
void delete_student(Student **head) {
int id;
printf("请输入要删除的学生编号:\n");
scanf("%d", &id);
Student *p = *head;
Student *prev = NULL;
while (p != NULL) {
if (p->id == id) {
if (prev == NULL) {
*head = p->next;
} else {
prev->next = p->next;
}
free(p);
write_student_to_file(*head);
printf("学生信息已删除成功!\n");
return;
}
prev = p;
p = p->next;
}
printf("没有找到该学生!\n");
}
// 打印菜单
void print_menu() {
printf("-----------------------------\n");
printf("1. 录入学生信息\n");
printf("2. 查询学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生信息\n");
printf("5. 退出程序\n");
printf("-----------------------------\n");
}
int main() {
Student *head = read_students_from_file();
int choice;
while (1) {
print_menu();
printf("请输入要执行的操作:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(&head);
break;
case 2:
query_student(head);
break;
case 3:
modify_student(head);
break;
case 4:
delete_student(&head);
break;
case 5:
printf("程序已退出!\n");
return 0;
default:
printf("无效的操作!\n");
break;
}
}
return 0;
}
```
在这个程序中,我们定义了一个 `Student` 结构体来存储学生信息,包括学生姓名、学号和3门课的成绩。我们还定义了一些函数来实现不同的操作,包括读取文件、写入文件、查找学生信息、录入学生信息、查询学生信息、修改学生信息和删除学生信息。在程序运行时,我们会读取存储学生信息的文件,然后显示一个菜单,让用户选择要执行的操作。
请注意,这只是一个简单的示例程序,您可以根据自己的需求和具体情况进行修改和扩展。同时,在实际开发中,您还需要考虑一些错误处理和边界情况,以确保程序的正确性和稳定性。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)