帮我写一个(学生信息管理系统)的c语言代码 要求: (1)数据结构:创建学生信息结点类型以及学生信息结构体; (2)存储结构:选择单链表存储学生信息。 (3)实现功能(以函数方式实现):学生信息链表创建、学生信息输入、学生信息 查询、学生信息修改、学生信息删除、学生信息输出(全部或部分输出)或保存到文件,学生相关排序。
时间: 2023-12-20 17:07:26 浏览: 74
好的,这是一个比较复杂的程序,需要一定的时间和精力才能完成,以下是一个基本的框架和部分代码,你可以在此基础上进行修改和完善。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生信息结构体
typedef struct student {
int id; // 学号
char name[20]; // 姓名
int age; // 年龄
char gender[5]; // 性别
char major[20]; // 专业
struct student *next; // 指向下一个学生信息结点的指针
} Student;
// 定义全局变量,表示学生信息链表的头结点
Student *head = NULL;
// 创建学生信息结点
Student *create_node(int id, char *name, int age, char *gender, char *major) {
Student *node = (Student *)malloc(sizeof(Student));
if (node == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
node->id = id;
strcpy(node->name, name);
node->age = age;
strcpy(node->gender, gender);
strcpy(node->major, major);
node->next = NULL;
return node;
}
// 添加学生信息结点到链表中
void add_node(Student *node) {
if (head == NULL) {
head = node;
} else {
Student *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = node;
}
}
// 学生信息输入
void input() {
int id, age;
char name[20], gender[5], major[20];
printf("Please input student id: ");
scanf("%d", &id);
printf("Please input student name: ");
scanf("%s", name);
printf("Please input student age: ");
scanf("%d", &age);
printf("Please input student gender: ");
scanf("%s", gender);
printf("Please input student major: ");
scanf("%s", major);
Student *node = create_node(id, name, age, gender, major);
add_node(node);
printf("Add student information successfully.\n");
}
// 学生信息查询
void search() {
int id;
printf("Please input student id: ");
scanf("%d", &id);
Student *p = head;
while (p != NULL) {
if (p->id == id) {
printf("Student information:\n");
printf("id: %d\n", p->id);
printf("name: %s\n", p->name);
printf("age: %d\n", p->age);
printf("gender: %s\n", p->gender);
printf("major: %s\n", p->major);
return;
}
p = p->next;
}
printf("No student information found.\n");
}
// 学生信息修改
void modify() {
int id;
printf("Please input student id: ");
scanf("%d", &id);
Student *p = head;
while (p != NULL) {
if (p->id == id) {
printf("Please input new student name: ");
scanf("%s", p->name);
printf("Please input new student age: ");
scanf("%d", &p->age);
printf("Please input new student gender: ");
scanf("%s", p->gender);
printf("Please input new student major: ");
scanf("%s", p->major);
printf("Modify student information successfully.\n");
return;
}
p = p->next;
}
printf("No student information found.\n");
}
// 学生信息删除
void delete() {
int id;
printf("Please input student id: ");
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);
printf("Delete student information successfully.\n");
return;
}
prev = p;
p = p->next;
}
printf("No student information found.\n");
}
// 学生信息输出
void output() {
printf("All student information:\n");
Student *p = head;
while (p != NULL) {
printf("id: %d, name: %s, age: %d, gender: %s, major: %s\n", p->id, p->name, p->age, p->gender, p->major);
p = p->next;
}
}
// 学生信息排序
void sort() {
// TODO: 实现学生信息排序
}
int main() {
int option;
while (1) {
printf("Please select an option:\n");
printf("1. Add student information\n");
printf("2. Search student information\n");
printf("3. Modify student information\n");
printf("4. Delete student information\n");
printf("5. Output student information\n");
printf("6. Sort student information\n");
printf("0. Exit\n");
scanf("%d", &option);
switch (option) {
case 1:
input();
break;
case 2:
search();
break;
case 3:
modify();
break;
case 4:
delete();
break;
case 5:
output();
break;
case 6:
sort();
break;
case 0:
printf("Exit.\n");
return 0;
default:
printf("Invalid option.\n");
break;
}
}
return 0;
}
```
阅读全文