大学生选课系统设计c++代码
时间: 2023-11-13 09:52:58 浏览: 39
对于一个完整的大学生选课系统,需要设计的代码量非常大,这里提供一个简单的选课系统的C++代码供参考:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 学生类
class Student {
public:
Student(string id, string name) : id_(id), name_(name) {}
string getId() const { return id_; }
string getName() const { return name_; }
private:
string id_; // 学生ID
string name_; // 学生姓名
};
// 课程类
class Course {
public:
Course(string id, string name, int capacity) : id_(id), name_(name), capacity_(capacity) {}
string getId() const { return id_; }
string getName() const { return name_; }
int getCapacity() const { return capacity_; }
int getEnrollment() const { return students_.size(); }
bool isFull() const { return getEnrollment() >= capacity_; }
void enroll(Student* student) { students_.push_back(student); }
private:
string id_; // 课程ID
string name_; // 课程名称
int capacity_; // 课程容量
vector<Student*> students_; // 选修该课程的学生列表
};
// 选课系统类
class CourseSelectionSystem {
public:
// 添加学生
void addStudent(string id, string name) {
students_.push_back(new Student(id, name));
}
// 添加课程
void addCourse(string id, string name, int capacity) {
courses_.push_back(new Course(id, name, capacity));
}
// 学生选课
bool enrollCourse(string courseId, string studentId) {
Course* course = findCourseById(courseId);
if (course == nullptr) {
return false;
}
if (course->isFull()) {
return false;
}
Student* student = findStudentById(studentId);
if (student == nullptr) {
return false;
}
course->enroll(student);
return true;
}
// 查询学生选课情况
void queryStudentCourses(string studentId) {
Student* student = findStudentById(studentId);
if (student == nullptr) {
cout << "Student not found!" << endl;
return;
}
cout << student->getName() << " has enrolled in the following courses:" << endl;
for (auto course : courses_) {
for (auto s : course->students_) {
if (s == student) {
cout << course->getName() << endl;
break;
}
}
}
}
// 查询课程选课情况
void queryCourseEnrollment(string courseId) {
Course* course = findCourseById(courseId);
if (course == nullptr) {
cout << "Course not found!" << endl;
return;
}
cout << course->getName() << " has " << course->getEnrollment() << " students enrolled." << endl;
}
private:
// 根据ID查找学生
Student* findStudentById(string id) {
for (auto student : students_) {
if (student->getId() == id) {
return student;
}
}
return nullptr;
}
// 根据ID查找课程
Course* findCourseById(string id) {
for (auto course : courses_) {
if (course->getId() == id) {
return course;
}
}
return nullptr;
}
vector<Student*> students_; // 学生列表
vector<Course*> courses_; // 课程列表
};
int main() {
CourseSelectionSystem css;
css.addStudent("001", "Alice");
css.addStudent("002", "Bob");
css.addCourse("001", "Math", 2);
css.addCourse("002", "English", 1);
css.enrollCourse("001", "001");
css.enrollCourse("002", "001");
css.enrollCourse("001", "002");
css.queryStudentCourses("001");
css.queryStudentCourses("002");
css.queryCourseEnrollment("001");
css.queryCourseEnrollment("002");
return 0;
}
```
在这个简单的选课系统中,包含了学生类、课程类和选课系统类。学生类和课程类分别表示学生和课程的信息,选课系统类提供了添加学生和课程、学生选课、查询学生选课情况、查询课程选课情况等功能。在主函数中,我们通过调用选课系统类的相应函数来模拟选课的过程,并且输出相应的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)