把上面两种代码功能结合后,能不能给出一个完整的代码
时间: 2024-03-01 20:54:33 浏览: 26
当然可以,下面是一份结合了两种功能的完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_DATE_LEN 10
#define MAX_ATTENDANCE_DAYS 31
// 定义学生结构体
struct student {
char name[MAX_NAME_LEN]; // 姓名
char attendance[MAX_ATTENDANCE_DAYS]; // 考勤信息,0表示缺勤,1表示出席
struct student *next; // 下一个学生节点
};
struct student *head = NULL; // 链表头指针
// 添加学生函数
void add_student(char *name) {
struct student *p = (struct student*) malloc(sizeof(struct student));
if (p == NULL) {
printf("内存分配失败!\n");
return;
}
strcpy(p->name, name);
memset(p->attendance, 0, sizeof(p->attendance));
p->next = head;
head = p;
printf("添加学生 %s 成功!\n", name);
}
// 删除学生函数
void delete_student(char *name, char *date) {
struct student *p, *prev;
prev = NULL;
for (p = head; p != NULL; prev = p, p = p->next) {
if (strcmp(p->name, name) == 0) {
int day = atoi(date) - 1;
if (day < 0 || day >= MAX_ATTENDANCE_DAYS) {
printf("日期格式错误!\n");
return;
}
if (p->attendance[day] == 0) {
printf("学生 %s 在 %s 缺勤,无需删除!\n", name, date);
return;
}
p->attendance[day] = 0;
printf("删除学生 %s 在 %s 的考勤记录成功!\n", name, date);
return;
}
}
printf("无法找到姓名为 %s 的学生记录!\n", name);
}
// 显示学生信息函数
void show_students() {
struct student *p;
if (head == NULL) {
printf("学生信息为空!\n");
return;
}
printf("学生姓名\t考勤信息\n");
for (p = head; p != NULL; p = p->next) {
printf("%s\t\t", p->name);
for (int i = 0; i < MAX_ATTENDANCE_DAYS; i++) {
printf("%d ", p->attendance[i]);
}
printf("\n");
}
}
int main() {
add_student("张三");
add_student("李四");
add_student("王五");
strcpy(head->attendance, "1111100000111110000011111000000");
strcpy(head->next->attendance, "1111111111000000000011111100000");
strcpy(head->next->next->attendance, "1110001111000000111100011110001");
show_students();
delete_student("张三", "15");
delete_student("李四", "31");
delete_student("王五", "50");
show_students();
return 0;
}
```
上面的代码中,我们结合了添加学生、删除学生和显示学生信息三个功能,实现了一个简单的考勤系统。在删除学生信息的函数中,可以根据日期精确删除某一个学生的某一天的考勤记录。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)