用C语言来实行学生信息管理命令行界面链表保存
时间: 2023-08-10 18:44:02 浏览: 160
好的,以下是使用C语言实现学生信息管理命令行界面链表保存的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_ID_LENGTH 10
typedef struct student {
char name[MAX_NAME_LENGTH];
char id[MAX_ID_LENGTH];
int age;
struct student *next;
} Student;
Student *head = NULL;
void add_student() {
Student *new_student = (Student *) malloc(sizeof(Student));
printf("Enter student name: ");
scanf("%s", new_student->name);
printf("Enter student ID: ");
scanf("%s", new_student->id);
printf("Enter student age: ");
scanf("%d", &new_student->age);
new_student->next = head;
head = new_student;
printf("Student added successfully!\n");
}
void delete_student() {
char id[MAX_ID_LENGTH];
printf("Enter ID of the student to be deleted: ");
scanf("%s", id);
Student *current = head, *previous = NULL;
while (current != NULL) {
if (strcmp(current->id, id) == 0) {
if (previous == NULL) {
head = current->next;
} else {
previous->next = current->next;
}
free(current);
printf("Student deleted successfully!\n");
return;
}
previous = current;
current = current->next;
}
printf("Student with ID \"%s\" not found!\n", id);
}
void update_student() {
char id[MAX_ID_LENGTH];
printf("Enter ID of the student to be updated: ");
scanf("%s", id);
Student *current = head;
while (current != NULL) {
if (strcmp(current->id, id) == 0) {
printf("Enter updated student name: ");
scanf("%s", current->name);
printf("Enter updated student age: ");
scanf("%d", ¤t->age);
printf("Student updated successfully!\n");
return;
}
current = current->next;
}
printf("Student with ID \"%s\" not found!\n", id);
}
void print_student(Student *student) {
printf("%s\t%s\t%d\n", student->name, student->id, student->age);
}
void print_students() {
Student *current = head;
printf("Name\tID\tAge\n");
while (current != NULL) {
print_student(current);
current = current->next;
}
}
void save_students() {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return;
}
Student *current = head;
while (current != NULL) {
fprintf(file, "%s\t%s\t%d\n", current->name, current->id, current->age);
current = current->next;
}
fclose(file);
printf("Students saved to file successfully!\n");
}
void load_students() {
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return;
}
char name[MAX_NAME_LENGTH], id[MAX_ID_LENGTH];
int age;
while (fscanf(file, "%s\t%s\t%d\n", name, id, &age) == 3) {
Student *new_student = (Student *) malloc(sizeof(Student));
strcpy(new_student->name, name);
strcpy(new_student->id, id);
new_student->age = age;
new_student->next = head;
head = new_student;
}
fclose(file);
printf("Students loaded from file successfully!\n");
}
int main() {
int choice;
do {
printf("\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Update student\n");
printf("4. Print all students\n");
printf("5. Save students to file\n");
printf("6. Load students from file\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
update_student();
break;
case 4:
print_students();
break;
case 5:
save_students();
break;
case 6:
load_students();
break;
case 0:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 0);
Student *current = head;
while (current != NULL) {
Student *next = current->next;
free(current);
current = next;
}
head = NULL;
return 0;
}
```
在代码中,我们使用链表来存储学生信息,并提供了添加、删除、更新和打印学生信息的功能。我们还添加了将学生信息保存到文件和从文件中加载学生信息的功能,以便在程序退出后可以将学生信息保存到磁盘上,并在程序启动时可以从磁盘上加载之前保存的学生信息。
阅读全文