c语言学生信息管理表
时间: 2024-02-19 07:55:07 浏览: 44
以下是一个简单的C语言学生信息管理系统的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct {
int id;
char name[50];
int age;
} Student;
// 定义链表节点结构体
typedef struct Node {
Student data;
struct Node* next;
} Node;
// 全局变量,指向链表头节点
Node* head = NULL;
// 添加学生信息
void addStudent() {
Node* newNode = (Node*)malloc(sizeof(Node));
printf("请输入学生的学号:");
scanf("%d", &(newNode->data.id));
printf("请输入学生的姓名:");
scanf("%s", newNode->data.name);
printf("请输入学生的年龄:");
scanf("%d", &(newNode->data.age));
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
printf("学生信息添加成功!\n");
}
// 删除学生信息
void deleteStudent() {
if (head == NULL) {
printf("学生信息为空,无法删除!\n");
return;
}
int id;
printf("请输入要删除的学生学号:");
scanf("%d", &id);
Node* temp = head;
Node* prev = NULL;
while (temp != NULL && temp->data.id != id) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("未找到该学生学号,删除失败!\n");
return;
}
if (prev == NULL) {
head = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
printf("学生信息删除成功!\n");
}
// 修改学生信息
void modifyStudent() {
if (head == NULL) {
printf("学生信息为空,无法修改!\n");
return;
}
int id;
printf("请输入要修改的学生学号:");
scanf("%d", &id);
Node* temp = head;
while (temp != NULL && temp->data.id != id) {
temp = temp->next;
}
if (temp == NULL) {
printf("未找到该学生学号,修改失败!\n");
return;
}
printf("请输入学生的新姓名:");
scanf("%s", temp->data.name);
printf("请输入学生的新年龄:");
scanf("%d", &(temp->data.age));
printf("学生信息修改成功!\n");
}
// 查询学生信息
void queryStudent() {
if (head == NULL) {
printf("学生信息为空,无法查询!\n");
return;
}
int id;
printf("请输入要查询的学生学号:");
scanf("%d", &id);
Node* temp = head;
while (temp != NULL && temp->data.id != id) {
temp = temp->next;
}
if (temp == NULL) {
printf("未找到该学生学号,查询失败!\n");
return;
}
printf("学生学号:%d\n", temp->data.id);
printf("学生姓名:%s\n", temp->data.name); printf("学生年龄:%d\n", temp->data.age);
}
// 保存学生信息到文件
void saveToFile() {
if (head == NULL) {
printf("学生信息为空,无法保存!\n");
return;
}
FILE* file = fopen("students.txt", "w");
if (file == NULL) {
printf("文件打开失败,保存失败!\n");
return;
}
Node* temp = head;
while (temp != NULL) {
fprintf(file, "%d %s %d\n", temp->data.id, temp->data.name, temp->data.age);
temp = temp->next;
}
fclose(file);
printf("学生信息保存成功!\n");
}
// 从文件加载学生信息
void loadFromFile() {
FILE* file = fopen("students.txt", "r");
if (file == NULL) {
printf("文件打开失败,加载失败!\n");
return;
}
head = NULL;
while (!feof(file)) {
Node* newNode = (Node*)malloc(sizeof(Node));
fscanf(file, "%d %s %d\n", &(newNode->data.id), newNode->data.name, &(newNode->data.age));
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
fclose(file);
printf("学生信息加载成功!\n");
}
// 释放链表内存
void freeList() {
Node* temp = head;
while (temp != NULL) {
Node* next = temp->next;
free(temp);
temp = next;
}
head = NULL;
}
int main() {
int choice;
while (1) {
printf("\n学生信息管理系统\n");
printf("1. 添加学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 查询学生信息\n");
printf("5. 保存学生信息到文件\n");
printf("6. 从文件加载学生信息\n");
printf("7. 退出\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
case 5:
saveToFile();
break;
case 6:
loadFromFile();
break;
case 7:
freeList();
printf("感谢使用学生信息管理系统,再见!\n");
return 0;
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
return 0;
}
```
相关推荐
![](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)