int countEnrollment(int courseId) { int count = 0; for (int i = 0; i < enrollments.size(); ++i) { if (enrollments[i].courseId == courseId) { count++; } } return count; } void queryEnrollmentCount() { int courseId; cout << "请输入要查询的课程编号:"; cin >> courseId; Course* course = NULL; for (int i = 0; i < courses.size(); ++i) { if (courses[i].id == courseId) { course = &courses[i]; break; } } if (course == NULL) { cout << "课程不存在!" << endl; return; } int count = countEnrollment(courseId); cout << "课程 " << course->name << " 的选课人数为:" << count << endl; } int calculateCredits(int studentId, int year) { int totalCredits = 0; for (int i = 0; i < enrollments.size(); ++i) { if (enrollments[i].studentId == studentId && enrollments[i].year == year && enrollments[i].grade >= 60) { Course* course = NULL; for (int j = 0; j < courses.size(); ++j) { if (courses[j].id == enrollments[i].courseId) { course = &courses[j]; break; } } if (course != NULL) { totalCredits += course->credit; } } } return totalCredits; } bool hasMetCreditRequirements(int studentId) { for (int year = 1; year <= 4; ++year) { int requiredCredits = (year == 1) ? 40 : 40; int earnedCredits = calculateCredits(studentId, year); if (earnedCredits < requiredCredits) { return false; } } return true; }
时间: 2024-02-14 18:26:17 浏览: 54
基于ideaI-2018-J2EE企业级应用开发课程资源.zip
这段代码是一个关于课程选课和学分计算的程序。它包含了以下几个函数:
1. `int countEnrollment(int courseId)`:这个函数用于统计选修某门课程的人数。它通过遍历`enrollments`列表,找到与指定课程编号相匹配的记录,并统计数量。
2. `void queryEnrollmentCount()`:这个函数用于查询某门课程的选课人数。它首先要求用户输入要查询的课程编号,然后通过遍历`courses`列表,找到与输入课程编号相匹配的课程记录。如果找到了对应的课程,就调用`countEnrollment`函数统计选课人数,并输出结果。如果没有找到对应的课程,就输出"课程不存在!"。
3. `int calculateCredits(int studentId, int year)`:这个函数用于计算某个学生某一年度的学分总数。它通过遍历`enrollments`列表,找到与指定学生和年份相匹配的记录,并判断该门课程的成绩是否及格。如果成绩及格,就查找对应的课程记录,计算该门课程的学分,并累加到总学分上。
4. `bool hasMetCreditRequirements(int studentId)`:这个函数用于判断某个学生是否满足学分要求。它通过调用`calculateCredits`函数,计算学生在每个年度的学分总数,并与要求的学分进行比较。如果某一年度的学分总数小于要求的学分,就返回`false`,表示学分未满足要求。如果所有年度都满足学分要求,就返回`true`。
阅读全文