某专业的学生选课情况如下: 学生信息包括:学号、姓名,专业,入学年份等;课程信息包括:课程编号,课程姓名,课程类型,学分,选课人数等,其中课程类型包括必修和选修,选课人数表示本门课程允许选课的最大人数;学生选课信息包括:学号,课程编号,学年成绩等,学生每学年要求必修课学分至少达到30学分,选修课至少达到10学分。 试设计一个小型的选课管理系统,使之能提供以下功能: (1)系统以菜单方式工作 (2)学生、课程和选课信息的增删改查基本操作,其中选课信息增删改时,要考虑本门课程的选课人数不能超过设置; (3)查询某门课程的选课人数; (4)查询某学生每个学年的学分,课程成绩如果不及格就不能算学分,并给出学生是否达到每学年必修选修学分的要求。 (5)统计每个学生的总学分,按从高到底排序,输出排序结果(需输出学号和总学分) 用cpp格式实现
时间: 2023-12-15 15:04:18 浏览: 88
以下是一个简单实现选课管理系统的示例代码:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
int id;
string name;
string major;
int enrollmentYear;
};
struct Course {
int id;
string name;
string type;
int credit;
int maxStudents;
int enrolledStudents = 0;
};
struct Enrollment {
int studentId;
int courseId;
int year;
int score;
};
vector<Student> students;
vector<Course> courses;
vector<Enrollment> enrollments;
// 添加学生
void addStudent() {
Student student;
cout << "请输入学生信息:" << endl;
cout << "学号:";
cin >> student.id;
cout << "姓名:";
cin >> student.name;
cout << "专业:";
cin >> student.major;
cout << "入学年份:";
cin >> student.enrollmentYear;
students.push_back(student);
}
// 添加课程
void addCourse() {
Course course;
cout << "请输入课程信息:" << endl;
cout << "课程编号:";
cin >> course.id;
cout << "课程名称:";
cin >> course.name;
cout << "课程类型(必修/选修):";
cin >> course.type;
cout << "学分:";
cin >> course.credit;
cout << "选课人数上限:";
cin >> course.maxStudents;
courses.push_back(course);
}
// 学生选课
void enrollCourse() {
int studentId, courseId, year, score;
cout << "请输入选课信息:" << endl;
cout << "学号:";
cin >> studentId;
cout << "课程编号:";
cin >> courseId;
cout << "学年:";
cin >> year;
cout << "成绩:";
cin >> score;
// 检查课程是否存在
auto courseIt = find_if(courses.begin(), courses.end(), [&](const Course& c) {
return c.id == courseId;
});
if (courseIt == courses.end()) {
cout << "课程不存在!" << endl;
return;
}
// 检查选课人数是否已满
if (courseIt->enrolledStudents >= courseIt->maxStudents) {
cout << "选课人数已满!" << endl;
return;
}
// 更新选课信息
Enrollment enrollment;
enrollment.studentId = studentId;
enrollment.courseId = courseId;
enrollment.year = year;
enrollment.score = score;
enrollments.push_back(enrollment);
courseIt->enrolledStudents++;
}
// 查询某门课程的选课人数
void queryCourseEnrollment() {
int courseId;
cout << "请输入课程编号:";
cin >> courseId;
// 检查课程是否存在
auto courseIt = find_if(courses.begin(), courses.end(), [&](const Course& c) {
return c.id == courseId;
});
if (courseIt == courses.end()) {
cout << "课程不存在!" << endl;
return;
}
cout << "课程" << courseIt->name << "的选课人数为:" << courseIt->enrolledStudents << endl;
}
// 查询某学生每个学年的学分及是否达到要求
void queryStudentCredits() {
int studentId;
cout << "请输入学号:";
cin >> studentId;
// 检查学生是否存在
auto studentIt = find_if(students.begin(), students.end(), [&](const Student& s) {
return s.id == studentId;
});
if (studentIt == students.end()) {
cout << "学生不存在!" << endl;
return;
}
cout << "学生" << studentIt->name << "每个学年的学分情况:" << endl;
for (int year = studentIt->enrollmentYear; year <= 2022; year++) {
int requiredCredits = 30;
int electiveCredits = 10;
int totalCredits = 0;
for (const auto& enrollment : enrollments) {
if (enrollment.studentId == studentId && enrollment.year == year) {
auto courseIt = find_if(courses.begin(), courses.end(), [&](const Course& c) {
return c.id == enrollment.courseId;
});
if (courseIt != courses.end()) {
totalCredits += courseIt->credit;
}
}
}
string result = totalCredits >= requiredCredits ? "达到要求" : "未达到要求";
cout << year << "年:" << totalCredits << "学分," << result << endl;
}
}
// 统计每个学生的总学分并排序输出
void sortStudentsByTotalCredits() {
vector<pair<int, int>> studentTotalCredits; // pair<studentId, totalCredits>
for (const auto& student : students) {
int totalCredits = 0;
for (const auto& enrollment : enrollments) {
if (enrollment.studentId == student.id) {
auto courseIt = find_if(courses.begin(), courses.end(), [&](const Course& c) {
return c.id == enrollment.courseId;
});
if (courseIt != courses.end()) {
totalCredits += courseIt->credit;
}
}
}
studentTotalCredits.push_back(make_pair(student.id, totalCredits));
}
// 按总学分排序
sort(studentTotalCredits.begin(), studentTotalCredits.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second;
});
cout << "每个学生的总学分排序结果:" << endl;
for (const auto& studentTotalCredit : studentTotalCredits) {
cout << "学号:" << studentTotalCredit.first << ",总学分:" << studentTotalCredit.second << endl;
}
}
int main() {
int choice;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 添加课程" << endl;
cout << "3. 学生选课" << endl;
cout << "4. 查询某门课程的选课人数" << endl;
cout << "5. 查询某学生每个学年的学分" << endl;
cout << "6. 统计每个学生的总学分并排序输出" << endl;
cout << "0. 退出" << endl;
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
addCourse();
break;
case 3:
enrollCourse();
break;
case 4:
queryCourseEnrollment();
break;
case 5:
queryStudentCredits();
break;
case 6:
sortStudentsByTotalCredits();
break;
case 0:
return 0;
default:
cout << "无效选择!" << endl;
}
cout << endl;
}
}
```
这只是一个简单的示例,实际情况中可能需要更多的功能和错误处理。你可以根据实际需求进行扩展和改进。
阅读全文