用c语言链表的方法完成学生管理系统,包括学生基本信息输入、输出、插入、删除、排序、查询、数据保存及打开
时间: 2023-10-20 21:20:10 浏览: 83
C语言链表实现学生管理系统
以下是一个简单的学生管理系统的代码,使用了链表来存储学生信息。具体功能包括学生基本信息输入、输出、插入、删除、排序、查询、数据保存及打开。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
struct student {
char name[50];
int age;
float score;
struct student *next;
};
// 插入学生信息
void insert_student(struct student **head) {
struct student *new_student = (struct student*)malloc(sizeof(struct student));
printf("请输入学生姓名:");
scanf("%s", new_student->name);
printf("请输入学生年龄:");
scanf("%d", &new_student->age);
printf("请输入学生成绩:");
scanf("%f", &new_student->score);
new_student->next = NULL;
if (*head == NULL) {
*head = new_student;
} else {
struct student *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_student;
}
printf("学生信息插入成功!\n\n");
}
// 输出学生信息
void print_student(struct student *head) {
if (head == NULL) {
printf("学生信息为空!\n\n");
} else {
printf("学生姓名\t学生年龄\t学生成绩\n");
struct student *p = head;
while (p != NULL) {
printf("%s\t\t%d\t\t%.2f\n", p->name, p->age, p->score);
p = p->next;
}
printf("\n");
}
}
// 删除学生信息
void delete_student(struct student **head) {
if (*head == NULL) {
printf("学生信息为空!\n\n");
return;
}
char name[50];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
struct student *p = *head;
if (strcmp(p->name, name) == 0) {
*head = p->next;
free(p);
printf("学生信息删除成功!\n\n");
return;
}
while (p->next != NULL && strcmp(p->next->name, name) != 0) {
p = p->next;
}
if (p->next == NULL) {
printf("未找到该学生信息!\n\n");
return;
}
struct student *del = p->next;
p->next = del->next;
free(del);
printf("学生信息删除成功!\n\n");
}
// 排序学生信息
void sort_student(struct student **head) {
if (*head == NULL) {
printf("学生信息为空!\n\n");
return;
}
struct student *p, *q, *tail = NULL;
while (tail != (*head)->next) {
p = *head;
q = p->next;
while (q != tail) {
if (strcmp(p->name, q->name) > 0) {
char name[50];
strcpy(name, p->name);
strcpy(p->name, q->name);
strcpy(q->name, name);
int age = p->age;
p->age = q->age;
q->age = age;
float score = p->score;
p->score = q->score;
q->score = score;
}
p = p->next;
q = q->next;
}
tail = p;
}
printf("学生信息排序成功!\n\n");
}
// 查询学生信息
void search_student(struct student *head) {
if (head == NULL) {
printf("学生信息为空!\n\n");
return;
}
char name[50];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
struct student *p = head;
while (p != NULL && strcmp(p->name, name) != 0) {
p = p->next;
}
if (p == NULL) {
printf("未找到该学生信息!\n\n");
} else {
printf("学生姓名\t学生年龄\t学生成绩\n");
printf("%s\t\t%d\t\t%.2f\n\n", p->name, p->age, p->score);
}
}
// 保存学生信息到文件
void save_student(struct student *head) {
if (head == NULL) {
printf("学生信息为空!\n\n");
return;
}
FILE *fp = fopen("student.txt", "w");
if (fp == NULL) {
printf("文件保存失败!\n\n");
return;
}
struct student *p = head;
while (p != NULL) {
fprintf(fp, "%s %d %.2f\n", p->name, p->age, p->score);
p = p->next;
}
fclose(fp);
printf("学生信息保存成功!\n\n");
}
// 打开文件中的学生信息
void open_student(struct student **head) {
FILE *fp = fopen("student.txt", "r");
if (fp == NULL) {
printf("文件打开失败!\n\n");
return;
}
char name[50];
int age;
float score;
while (fscanf(fp, "%s %d %f\n", name, &age, &score) != EOF) {
struct student *new_student = (struct student*)malloc(sizeof(struct student));
strcpy(new_student->name, name);
new_student->age = age;
new_student->score = score;
new_student->next = NULL;
if (*head == NULL) {
*head = new_student;
} else {
struct student *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_student;
}
}
fclose(fp);
printf("学生信息打开成功!\n\n");
}
int main() {
struct student *head = NULL;
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 插入学生信息\n");
printf("2. 输出学生信息\n");
printf("3. 删除学生信息\n");
printf("4. 排序学生信息\n");
printf("5. 查询学生信息\n");
printf("6. 保存学生信息到文件\n");
printf("7. 打开文件中的学生信息\n");
printf("0. 退出程序\n");
printf("请输入操作编号:");
scanf("%d", &choice);
printf("\n");
switch (choice) {
case 1:
insert_student(&head);
break;
case 2:
print_student(head);
break;
case 3:
delete_student(&head);
break;
case 4:
sort_student(&head);
break;
case 5:
search_student(head);
break;
case 6:
save_student(head);
break;
case 7:
open_student(&head);
break;
case 0:
printf("程序已退出!\n");
exit(0);
default:
printf("输入的操作编号有误!\n\n");
break;
}
}
return 0;
}
```
注意:该代码只是一个简单的示例,实际应用中可能需要更多的功能和异常处理。
阅读全文