memmove(&students[i], &students[i + 1], sizeof(Student) * (studentCount - i - 1)); studentCount--;这串代码的逻辑
时间: 2024-04-04 09:32:02 浏览: 10
这段代码的作用是将学生数组中第 i 个学生之后的所有学生向前移一位,覆盖掉第 i 个学生,相当于删除第 i 个学生。同时,将学生总数 studentCount 减一。其中,memmove() 函数用于将一段内存区域的数据移动到另一个内存区域,它会自动处理内存区域重叠的情况。具体来说,&students[i] 表示从第 i 个学生开始的内存地址,&students[i + 1] 表示从第 i + 1 个学生开始的内存地址,sizeof(Student) * (studentCount - i - 1) 表示需要移动的字节数,即从第 i + 1 个学生开始,到学生数组末尾的所有学生所占用的字节数。
相关问题
void deleteStudentInfo() { int id, i; printf("Please enter the student ID to delete: "); scanf("%d", &id); for (i = 0; i < studentCount; ++i) { if (students[i].id == id) { memmove(&students[i], &students[i+1], sizeof(Student) * (studentCount - i - 1)); studentCount--; printf("Deletion successful!\n"); return; } } printf("Student with ID %d not found!\n", id); }该代码的详细运行逻辑,并使用了那些函数
这段代码是一个删除学生信息的函数。函数首先要求用户输入要删除的学生的 ID,然后在学生数组中查找该 ID 对应的学生。如果找到了该学生,则使用 memmove 函数将该学生结构体之后的所有结构体向前移动一位,覆盖掉该学生结构体,然后将学生数量减少 1,最后输出“Deletion successful!”,函数结束。如果在学生数组中找不到该 ID 对应的学生,则输出“Student with ID %d not found!”,函数结束。
该函数使用了 scanf 函数获取用户输入的 ID,使用了 memmove 函数实现结构体数组元素的移动。
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char name[20]; double averageScore; double moralityScore; double finalScore; } Student; int cmp(const void* a, const void* b) { Student* studentA = (Student*)a; Student* studentB = (Student*)b; return studentB->finalScore - studentA->finalScore; } int main() { FILE* inFile = fopen("rawscore.txt", "r"); if (!inFile) { fprintf(stderr, "无法打开文件\n"); return 1; } int studentCount, subjectCount; fscanf(inFile, "%d %d", &studentCount, &subjectCount); Student* students = (Student*)malloc(studentCount * sizeof(Student)); for (int i = 0; i < studentCount; i++) { fscanf(inFile, "%s", students[i].name); double sum = 0; for (int j = 0; j < subjectCount; j++) { double score; fscanf(inFile, "%lf", &score); sum += score; } students[i].averageScore = sum / subjectCount; fscanf(inFile, "%lf", &students[i].moralityScore); } double academicWeight, moralityWeight; printf("请输入专业课成绩与德育成绩之间的比例(如0.7):"); scanf("%lf", &academicWeight); moralityWeight = 1 - academicWeight; for (int i = 0; i < studentCount; i++) { students[i].finalScore = students[i].averageScore * academicWeight + students[i].moralityScore * moralityWeight; } qsort(students, studentCount, sizeof(Student), cmp); int scholarshipLevelCount; printf("请输入奖学金等级数:"); scanf("%d", &scholarshipLevelCount); for (int i = 1; i <= scholarshipLevelCount; i++) { int count; printf("请输入%d等奖的获奖人数:", i); scanf("%d", &count); printf("%d等奖获得者:\n", i); for (int j = 0; j < count; j++) { printf("%s %.2lf\n", students[j].name, students[j].finalScore); } memmove(students, students + count, (studentCount - count) * sizeof(Student)); studentCount -= count; } free(students); return 0; }
这段代码是用来计算学生的最终成绩并根据成绩给出奖学金等级的。程序首先读取一个文本文件,该文件包含了每个学生的姓名、各门课程的成绩以及德育成绩。然后程序通过输入专业课成绩与德育成绩之间的比例来计算每个学生的最终成绩,最终成绩由专业课成绩和德育成绩加权得出。程序使用快速排序算法对学生按照最终成绩进行排序,然后根据输入的奖学金等级数依次给出各个等级的获奖人数和获奖名单。最后释放动态分配的内存并退出程序。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)