c语言用链表、结构体、文件写一个学员管理系统,要求可以添加学员信息(学号、姓名、身份证号(需要用生日进行验证)、手机号、家庭住址),可以查看所有学员信息、完成查找指定姓名、身份证号、学号的信息(实现模糊查询)、可以修改学员信息,可以删除学员信息、可以保存学员信息到指定文件中
时间: 2023-12-02 12:02:43 浏览: 76
好的,我来给你讲解一下如何实现这个程序。
首先,我们需要定义一个学生信息结构体,包含学号、姓名、身份证号、手机号和家庭住址等信息。例如:
```c
typedef struct student {
char id[20]; // 学号
char name[20]; // 姓名
char id_card[20]; // 身份证号
char phone[20]; // 手机号
char address[50]; // 家庭住址
} Student;
```
接着,我们需要定义一个链表,用于存储所有学生信息。每个节点包含一个学生信息结构体和指向下一个节点的指针。例如:
```c
typedef struct node {
Student data; // 学生信息结构体
struct node *next; // 指向下一个节点的指针
} Node;
```
然后,我们可以定义一些函数来实现各种操作,比如添加学生信息、查看所有学生信息、查找指定姓名、身份证号、学号的信息、修改学生信息、删除学生信息和保存学生信息到文件中等操作。以下是这些函数的代码:
```c
// 添加学生信息
void add_student(Node **head) {
Student s;
printf("请输入学号:");
scanf("%s", s.id);
printf("请输入姓名:");
scanf("%s", s.name);
printf("请输入身份证号:");
scanf("%s", s.id_card);
printf("请输入手机号:");
scanf("%s", s.phone);
printf("请输入家庭住址:");
scanf("%s", s.address);
if (!check_id_card(s.id_card)) { // 身份证号验证失败
printf("身份证号格式错误,请重新输入!\n");
return;
}
Node *new_node = (Node *)malloc(sizeof(Node));
new_node->data = s;
new_node->next = NULL;
if (*head == NULL) { // 链表为空
*head = new_node;
} else { // 链表不为空,插入到链表尾部
Node *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
}
printf("添加成功!\n");
}
// 查看所有学生信息
void view_all_students(Node *head) {
if (head == NULL) { // 链表为空
printf("没有学生信息!\n");
return;
}
printf("学号\t姓名\t身份证号\t\t\t手机号\t\t\t家庭住址\n");
Node *p = head;
while (p != NULL) {
printf("%s\t%s\t%s\t%s\t%s\n", p->data.id, p->data.name, p->data.id_card, p->data.phone, p->data.address);
p = p->next;
}
}
// 查找指定姓名、身份证号、学号的信息(实现模糊查询)
void search_student(Node *head) {
if (head == NULL) { // 链表为空
printf("没有学生信息!\n");
return;
}
char keyword[20];
printf("请输入要查找的关键字:");
scanf("%s", keyword);
printf("学号\t姓名\t身份证号\t\t\t手机号\t\t\t家庭住址\n");
Node *p = head;
while (p != NULL) {
if (strstr(p->data.id, keyword) != NULL || strstr(p->data.name, keyword) != NULL || strstr(p->data.id_card, keyword) != NULL) {
printf("%s\t%s\t%s\t%s\t%s\n", p->data.id, p->data.name, p->data.id_card, p->data.phone, p->data.address);
}
p = p->next;
}
}
// 修改学生信息
void modify_student(Node *head) {
if (head == NULL) { // 链表为空
printf("没有学生信息!\n");
return;
}
char id[20];
printf("请输入要修改的学生的学号:");
scanf("%s", id);
Node *p = head;
while (p != NULL && strcmp(p->data.id, id) != 0) {
p = p->next;
}
if (p == NULL) { // 没有找到对应的学生
printf("没有找到对应的学生!\n");
return;
}
printf("请输入新的姓名:");
scanf("%s", p->data.name);
printf("请输入新的身份证号:");
scanf("%s", p->data.id_card);
printf("请输入新的手机号:");
scanf("%s", p->data.phone);
printf("请输入新的家庭住址:");
scanf("%s", p->data.address);
if (!check_id_card(p->data.id_card)) { // 身份证号验证失败
printf("身份证号格式错误,请重新输入!\n");
return;
}
printf("修改成功!\n");
}
// 删除学生信息
void delete_student(Node **head) {
if (*head == NULL) { // 链表为空
printf("没有学生信息!\n");
return;
}
char id[20];
printf("请输入要删除的学生的学号:");
scanf("%s", id);
Node *p = *head;
if (strcmp(p->data.id, id) == 0) { // 删除第一个节点
*head = p->next;
free(p);
} else {
while (p->next != NULL && strcmp(p->next->data.id, id) != 0) {
p = p->next;
}
if (p->next == NULL) { // 没有找到对应的学生
printf("没有找到对应的学生!\n");
return;
}
Node *q = p->next;
p->next = q->next;
free(q);
}
printf("删除成功!\n");
}
// 保存学生信息到文件中
void save_to_file(Node *head) {
if (head == NULL) { // 链表为空
printf("没有学生信息!\n");
return;
}
char filename[50];
printf("请输入要保存的文件名:");
scanf("%s", filename);
FILE *fp = fopen(filename, "w");
if (fp == NULL) { // 打开文件失败
printf("打开文件失败!\n");
return;
}
Node *p = head;
while (p != NULL) {
fprintf(fp, "%s %s %s %s %s\n", p->data.id, p->data.name, p->data.id_card, p->data.phone, p->data.address);
p = p->next;
}
fclose(fp);
printf("保存成功!\n");
}
```
其中,check_id_card函数用于验证身份证号是否符合规定,具体实现可以参考下面的代码:
```c
// 验证身份证号是否符合规定
int check_id_card(char *id_card) {
if (strlen(id_card) != 18) { // 身份证号长度不为18位
return 0;
}
int year = atoi(strncpy((char *)malloc(5), id_card + 6, 4)); // 获取出生年份
int month = atoi(strncpy((char *)malloc(3), id_card + 10, 2)); // 获取出生月份
int day = atoi(strncpy((char *)malloc(3), id_card + 12, 2)); // 获取出生日期
if (year < 1900 || year > 2021 || month < 1 || month > 12 || day < 1 || day > 31) { // 出生年份不在1900-2021之间,出生月份不在1-12之间,出生日期不在1-31之间
return 0;
}
return 1;
}
```
最后,我们可以在主函数中调用这些函数来完成各种操作。例如:
```c
int main() {
Node *head = NULL; // 链表头指针初始化为空
int choice;
do {
printf("1. 添加学生信息\n2. 查看所有学生信息\n3. 查找学生信息\n4. 修改学生信息\n5. 删除学生信息\n6. 保存学生信息到文件中\n0. 退出程序\n");
printf("请输入要执行的操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(&head);
break;
case 2:
view_all_students(head);
break;
case 3:
search_student(head);
break;
case 4:
modify_student(head);
break;
case 5:
delete_student(&head);
break;
case 6:
save_to_file(head);
break;
case 0:
break;
default:
printf("无效的操作编号!\n");
break;
}
} while (choice != 0);
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)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)