编译程序通过链表和文件管理,支持录入,查询,修改,删除学生的信息和他的3门课的成绩。
时间: 2024-04-05 15:33:09 浏览: 14
好的,这是一个关于学生信息管理系统的问题。您需要一个能够处理链表和文件的程序,该程序应支持以下功能:
1. 录入学生信息和他的3门课的成绩
2. 查询学生信息和他的3门课的成绩
3. 修改学生信息和他的3门课的成绩
4. 删除学生信息和他的3门课的成绩
您可以使用 C 语言或其他编程语言来实现这个程序。您需要创建一个学生结构体,该结构体包含学生的姓名、学号、3门课程的成绩等信息,同时您还需要创建一个链表来存储这些学生信息。在录入、查询、修改和删除学生信息时,您需要读写文件来保存和读取学生信息。
这是一个比较大的项目,需要您具备一定的编程经验和技能。如果您需要更详细的指导或帮助,请告诉我。
相关问题
通过c语言实现编译程序通过链表和文件管理,支持录入,查询,修改,删除学生的信息和他的3门课的成绩。
好的,下面是一个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门课的成绩。我们还定义了一些函数来实现不同的操作,包括读取文件、写入文件、查找学生信息、录入学生信息、查询学生信息、修改学生信息和删除学生信息。在程序运行时,我们会读取存储学生信息的文件,然后显示一个菜单,让用户选择要执行的操作。
请注意,这只是一个简单的示例程序,您可以根据自己的需求和具体情况进行修改和扩展。同时,在实际开发中,您还需要考虑一些错误处理和边界情况,以确保程序的正确性和稳定性。
用链表管理学生信息。要求遍历链表,把所有学生的信息输出。学生信息包含姓名,学号和3门成绩
好的,这是一个关于链表的问题。下面是一个简单的实现,你可以参考一下:
```python
class Student:
def __init__(self, name, id, score1, score2, score3):
self.name = name
self.id = id
self.score1 = score1
self.score2 = score2
self.score3 = score3
self.next = None
class StudentList:
def __init__(self):
self.head = None
def add_student(self, name, id, score1, score2, score3):
new_student = Student(name, id, score1, score2, score3)
if self.head is None:
self.head = new_student
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_student
def print_students(self):
current = self.head
while current is not None:
print("Name: %s, ID: %s, Scores: %d %d %d" % (current.name, current.id, current.score1, current.score2, current.score3))
current = current.next
```
在这个实现中,我们定义了两个类:`Student` 和 `StudentList`。`Student` 类表示一个学生,包含姓名、学号和三门成绩,以及一个指向下一个节点的指针。`StudentList` 类表示整个链表,包含一个指向链表头的指针。
我们可以使用 `add_student()` 方法向链表中添加学生信息,使用 `print_students()` 方法遍历链表,输出所有学生的信息。
你可以按照以下步骤来测试这个实现:
```python
# 创建一个学生列表
students = StudentList()
# 添加一些学生信息
students.add_student("张三", "1001", 90, 85, 95)
students.add_student("李四", "1002", 80, 75, 85)
students.add_student("王五", "1003", 95, 90, 100)
# 输出所有学生信息
students.print_students()
```
输出结果应该是:
```
Name: 张三, ID: 1001, Scores: 90 85 95
Name: 李四, ID: 1002, Scores: 80 75 85
Name: 王五, ID: 1003, Scores: 95 90 100
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)