统计综合排序

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));
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值