帮我修改这段代码让它能够删除所有符合要求的学生信息 void Delete_score(Student* header) { char major[20] = { 0 }; printf("输入要删除的学生所在专业\n"); rewind(stdin);//作用用于清楚缓存区 gets_s(major); int subject = 0; int class_ = 0; printf("要删除的班级: "); scanf_s("%d",&class_); while (1) { printf("输入要查找的课程序号\n"); scanf_s("%d", &subject); subject--; if (subject == 0 || subject == 1 || subject == 2) { break; } } printf("输入要求小于的分数段\n"); int score = 0; scanf_s("%d", &score); Student * pre = header; Student * pcurrent = pre->next; Student* tmp = NULL; while(pcurrent != NULL){ if ((pcurrent->class_ == class_) && strcmp(pcurrent->major, major) == 0 && subject == 0 && pcurrent->score[0] < score) { pre->next = pcurrent->next; printf("%s节点被销毁\n ", pcurrent->id); tmp = pcurrent; //释放删除节点 free(tmp); } if ((pcurrent->class_ == class_) && strcmp(pcurrent->major, major) == 0 && subject == 1 && pcurrent->score[1] < score) { pre->next = pcurrent->next; printf("%s节点被销毁\n ", pcurrent->id); //释放删除节点 free(pcurrent); } if ((pcurrent->class_ == class_) && strcmp(pcurrent->major, major) == 0 && subject == 2 && pcurrent->score[2] < score) { pre->next = pcurrent->next; printf("%s节点被销毁\n ", pcurrent->id); //释放删除节点 free(pcurrent); } pre = pcurrent; pcurrent = pcurrent->next; } if (pcurrent == NULL) { return; } }
时间: 2024-04-12 20:27:55 浏览: 91
以下是修改后的代码:
```c
void Delete_score(Student* header) {
char major[20] = { 0 };
printf("输入要删除的学生所在专业\n");
rewind(stdin);//作用用于清楚缓存区
gets_s(major);
int subject = 0;
int class_ = 0;
printf("要删除的班级: ");
scanf_s("%d", &class_);
while (1) {
printf("输入要查找的课程序号\n");
scanf_s("%d", &subject);
subject--;
if (subject == 0 || subject == 1 || subject == 2) {
break;
}
}
printf("输入要求小于的分数段\n");
int score = 0;
scanf_s("%d", &score);
Student* pre = header;
Student* pcurrent = pre->next;
Student* tmp = NULL;
while (pcurrent != NULL) {
if ((pcurrent->class_ == class_) && strcmp(pcurrent->major, major) == 0) {
if (subject == 0 && pcurrent->score[0] < score) {
pre->next = pcurrent->next;
printf("%s节点被销毁\n ", pcurrent->id);
tmp = pcurrent;
//释放删除节点
free(tmp);
}
else if (subject == 1 && pcurrent->score[1] < score) {
pre->next = pcurrent->next;
printf("%s节点被销毁\n ", pcurrent->id);
//释放删除节点
free(pcurrent);
}
else if (subject == 2 && pcurrent->score[2] < score) {
pre->next = pcurrent->next;
printf("%s节点被销毁\n ", pcurrent->id);
//释放删除节点
free(pcurrent);
}
}
pre = pcurrent;
pcurrent = pcurrent->next;
}
if (pcurrent == NULL) {
return;
}
}
```
这个修改后的代码会删除所有符合要求的学生信息。首先,根据用户输入的专业和班级筛选出需要删除的学生。然后,根据用户输入的课程序号和分数段判断是否满足删除条件。如果满足条件,则将对应的学生节点从链表中删除,并释放相关内存空间。最后,返回删除操作是否成功。
阅读全文