// 统计学生不及格次数
int countFailedCoursesForStudent(struct Lstudent* targetStudent) {
int failedCount = 0;
CourseListNode* currentCourse = targetStudent->chead;
while (currentCourse != NULL) {
if (currentCourse->data.chengji < 60) { // 假设60分为及格线
failedCount++;
}
currentCourse = currentCourse->next;
}
return failedCount;
}
// 计算单个学生的总成绩
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;
int hasScore = 0;
while (currentCourse != NULL) {
if (currentCourse->data.chengji > 0 && (currentCourse->data.chengji < minScore || !hasScore)) {
minScore = currentCourse->data.chengji;
hasScore = 1;
}
currentCourse = currentCourse->next;
}
return hasScore ? minScore : 0.0f; // 如果没有成绩,则返回0或其他默认值
}
// 计算单个学生的不及格率(百分比形式)
float calculateFailureRateForStudent(struct Lstudent* targetStudent) {
int totalCourses = countCoursesForStudent(targetStudent);
int failedCourses = countFailedCoursesForStudent(targetStudent);
if (totalCourses > 0) {
return ((float)failedCourses / totalCourses) * 100.0f;
} else {
// 若学生没有参加任何课程,则不及格率为0%
return 0.0f;
}
}
int main() {
// 初始化链表
struct Lstudent* head = NULL; // 初始化为空指针
load(&head,"信息.txt");
KGPA(head);
ZGPA(head);
if (head != NULL) {
// 打印链表信息
printStudentListAndCourses(head);
// 其他对链表的操作...
} else {
printf("没有读取到任何学生信息。\n");
}
// 示例:分析某个学生的信息
int targetXuehao;
printf("请输入要分析的学生学号:\n");
scanf("%d", &targetXuehao);
struct Lstudent* targetStudent = findStudentByXuehao(head, targetXuehao);
if (targetStudent == NULL) {
printf("未找到对应学号的学生信息。\n");
} else {
printf("\n学生学号:%d 的成绩分析:\n", targetStudent->data.xuehao);
printf("总成绩:%.2f\n", calculateTotalScoreForStudent(targetStudent));
printf("平均成绩:%.2f\n", calculateAverageScoreForStudent(targetStudent));
printf("最高分:%.2f\n", calculateMaxScoreForStudent(targetStudent));
printf("最低分:%.2f\n", calculateMinScoreForStudent(targetStudent));
printf("不及格课程数:%d\n", countFailedCoursesForStudent(targetStudent));
int failCount = countFailedCoursesForStudent(targetStudent);
float failureRate = calculateFailureRateForStudent(targetStudent);
printf("不及格率:%.2f%%\n", failureRate);
}