void analyzeSingleCourse(CourseListNode* courseNode) {
if (courseNode == NULL) {
printf("未找到该课程的数据。\n");
return;
}
float totalScore = 0.0f;
float maxScore = courseNode->data.chengji;
float minScore = courseNode->data.chengji;
int studentCount = countStudents(head);
// 遍历该课程下的所有学生
CourseListNode* currentCourseNode = courseNode;
while (currentCourseNode != NULL) {
totalScore += currentCourseNode->data.chengji;
if (currentCourseNode->data.chengji > maxScore) {
maxScore = currentCourseNode->data.chengji;
}
if (currentCourseNode->data.chengji < minScore) {
minScore = currentCourseNode->data.chengji;
}
studentCount++;
currentCourseNode = currentCourseNode->next;
}
float averageScore = totalScore / studentCount;
printf("课程名称:%s\n", courseNode->data.name);
printf("总成绩:%.2f\n", totalScore);
printf("平均成绩:%.2f\n", averageScore);
printf("最高分:%.2f\n", maxScore);
printf("最低分:%.2f\n", minScore);
}
// 计算单个学生的总成绩
float calculateTotalScoreForStudent(struct Lstudent* targetStudent) {
float totalScore = 0.0f;
CourseListNode* currentCourse = targetStudent->chead;
while (currentCourse != NULL) {
totalScore += currentCourse->data.chengji;
currentCourse = currentCourse->next;
}
return totalScore;
}
// 计算单个学生的平均成绩
float calculateAverageScoreForStudent(struct Lstudent* targetStudent) {
int courseCount = countCoursesForStudent(targetStudent);
float totalScore = calculateTotalScoreForStudent(targetStudent);
if (courseCount > 0) {
return totalScore / courseCount;
} else {
return 0.0f; // 或者可以根据实际情况设定默认值
}
}
// 计算单个学生的最高分
float calculateMaxScoreForStudent(struct Lstudent* targetStudent) {
float maxScore = targetStudent->chead->data.chengji;
CourseListNode* currentCourse = targetStudent->chead->next;
while (currentCourse != NULL) {
if (currentCourse->data.chengji > maxScore) {
maxScore = currentCourse->data.chengji;
}
currentCourse = currentCourse->next;
}
return maxScore;
}
// 计算单个学生的最低分
float calculateMinScoreForStudent(struct Lstudent* targetStudent) {
float minScore = targetStudent->chead->data.chengji;
CourseListNode* currentCourse = targetStudent->chead->next;
bool hasScore = false;
while (currentCourse != NULL) {
if (currentCourse->data.chengji > 0 && (currentCourse->data.chengji < minScore || !hasScore)) {
minScore = currentCourse->data.chengji;
hasScore = true;
}
currentCourse = currentCourse->next;
}
return hasScore ? minScore : 0.0f; // 如果没有成绩,则返回0或其他默认值
}
// 在main函数中调用这些函数
if (targetStudent != NULL) {
// 输出各项统计数据
printf("\n目标学生共选修了 %d 门课程,总成绩为 %.2f 分。\n", coursesTakenByTarget, totalScoreOfTarget);
printf("平均成绩为: %.2f 分\n", calculateAverageScoreForStudent(targetStudent));
printf("最高分为: %.2f 分\n", calculateMaxScoreForStudent(targetStudent));
printf("最低分为: %.2f 分\n", calculateMinScoreForStudent(targetStudent));
}