功能4:该函数实现的是对已经存在于系统种的同学的信息进行删除操作,具体想法是学生人数num--,后面所有人的信息前移一个位置,实现信息的覆盖*/ void Delete() { int i, j,flag = -1; char op[2],information[maxsize]; setInfo("请输入:<1>按学号删除<2>按姓名删除", op); if (op[0] == '1') { setInfo("请输入学号", information); for (i = 0; i < num; i++) { if (strcmp(Students[i].Student_ID, information) == 0) { flag = 1; for (j = i + 1; j < num; j++) { copy(&Students[i], &Students[j]); } } } } else { setInfo("请输入姓名", information); for (i = 0; i < num; i++) { if (strcmp(Students[i].Name, information) == 0) { flag = 1; for (j = i + 1; j < num; j++) { copy(&Students[i], &Students[j]); } } } } if (flag == -1) { printf("查无此信息!按回车键返回\n"); wait_for_Enter(); return ; } num--;//数据减少,容量相应减少 printf("删除完成!按回车键返回\n"); wait_for_Enter(); } 请完善此删除功能,其查找学生姓名准备进行删除功能时,可以查找重名的情况,如果重名,则输入学号进一步确认,又因为每一位同学的数据不唯一,请精确删除范围,使其能删除同学某一天的考勤信息
时间: 2024-03-02 18:48:57 浏览: 23
好的,我来为您完善这个删除功能。首先,在查找到同名学生时,我们需要输出所有同名学生的信息,让用户选择要删除哪个学生。其次,在删除某个学生时,我们需要把这个学生的考勤信息也一并删除。以下是完善后的代码:
```
void Delete() {
int i, j, flag = -1;
char op[2], information[maxsize];
setInfo("请输入:<1>按学号删除<2>按姓名删除", op);
if (op[0] == '1') {
setInfo("请输入学号", information);
for (i = 0; i < num; i++) {
if (strcmp(Students[i].Student_ID, information) == 0) {
flag = 1;
for (j = i + 1; j < num; j++) {
copy(&Students[i], &Students[j]);
}
}
}
} else {
setInfo("请输入姓名", information);
for (i = 0; i < num; i++) {
if (strcmp(Students[i].Name, information) == 0) {
flag = 1;
int cnt = 0;
int indices[maxsize];
for (j = 0; j < num; j++) {
if (strcmp(Students[j].Name, information) == 0) {
printf("第%d位同名学生:\n", cnt + 1);
print(&Students[j]);
indices[cnt] = j;
cnt++;
}
}
if (cnt == 0) {
printf("查无此信息!按回车键返回\n");
wait_for_Enter();
return ;
}
setInfo("请选择要删除的同名学生(输入编号)", information);
int index = atoi(information);
if (index < 1 || index > cnt) {
printf("输入有误!按回车键返回\n");
wait_for_Enter();
return ;
}
i = indices[index - 1];
for (j = i + 1; j < num; j++) {
copy(&Students[i], &Students[j]);
}
}
}
}
if (flag == -1) {
printf("查无此信息!按回车键返回\n");
wait_for_Enter();
return ;
}
num--; // 数据减少,容量相应减少
// 删除该学生的考勤信息
for (i = 0; i < attendance_num; i++) {
if (strcmp(Attendance[i].Student_ID, Students[num].Student_ID) == 0) {
for (j = i + 1; j < attendance_num; j++) {
copy_attendance(&Attendance[i], &Attendance[j]);
}
attendance_num--;
}
}
printf("删除完成!按回车键返回\n");
wait_for_Enter();
}
```
在删除同名学生时,我们首先统计同名学生的数量,并输出所有同名学生的信息。然后,我们让用户输入要删除的同名学生的编号,再找到该学生的位置进行删除。在删除同学时,我们还需要遍历考勤信息表,把该学生的考勤信息也一并删除。