将代码写到220行以上
时间: 2024-03-16 07:41:16 浏览: 87
UNIX环境高级编程和源代码
4星 · 用户满意度95%
好的,以下是一个更为复杂的学生选课系统的C++代码,其中所有的自定义标识符都已经加上了_0832lzq:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Course_0832lzq {
private:
string name_0832lzq;
int id_0832lzq;
int credit_0832lzq;
vector<string> prerequisites_0832lzq;
public:
Course_0832lzq(string name, int id, int credit, vector<string> prerequisites = {}) {
name_0832lzq = name;
id_0832lzq = id;
credit_0832lzq = credit;
prerequisites_0832lzq = prerequisites;
}
string get_name_0832lzq() const {
return name_0832lzq;
}
int get_id_0832lzq() const {
return id_0832lzq;
}
int get_credit_0832lzq() const {
return credit_0832lzq;
}
vector<string> get_prerequisites_0832lzq() const {
return prerequisites_0832lzq;
}
};
class Student_0832lzq {
private:
string name_0832lzq;
int id_0832lzq;
vector<Course_0832lzq> courses_0832lzq;
public:
Student_0832lzq(string name, int id) {
name_0832lzq = name;
id_0832lzq = id;
}
string get_name_0832lzq() const {
return name_0832lzq;
}
int get_id_0832lzq() const {
return id_0832lzq;
}
void add_course_0832lzq(const Course_0832lzq& course) {
if (can_take_course_0832lzq(course)) {
courses_0832lzq.push_back(course);
cout << "Student " << name_0832lzq << " has successfully enrolled in " << course.get_name_0832lzq() << endl;
} else {
cout << "Student " << name_0832lzq << " is not eligible to take " << course.get_name_0832lzq() << endl;
}
}
bool can_take_course_0832lzq(const Course_0832lzq& course) const {
if (find(courses_0832lzq.begin(), courses_0832lzq.end(), course) != courses_0832lzq.end()) {
cout << "Student " << name_0832lzq << " has already taken " << course.get_name_0832lzq() << endl;
return false;
}
for (const auto& prerequisite : course.get_prerequisites_0832lzq()) {
if (find_if(courses_0832lzq.begin(), courses_0832lzq.end(), [prerequisite](const Course_0832lzq& c) {return c.get_name_0832lzq() == prerequisite;}) == courses_0832lzq.end()) {
cout << "Student " << name_0832lzq << " has not taken " << prerequisite << " which is a prerequisite for " << course.get_name_0832lzq() << endl;
return false;
}
}
return true;
}
void print_courses_0832lzq() const {
cout << "Student " << name_0832lzq << " has taken the following courses:" << endl;
for (const auto& course : courses_0832lzq) {
cout << course.get_name_0832lzq() << " (ID: " << course.get_id_0832lzq() << ", " << course.get_credit_0832lzq() << " credits)" << endl;
}
}
};
class CourseCatalog_0832lzq {
private:
vector<Course_0832lzq> courses_0832lzq;
public:
void add_course_0832lzq(const Course_0832lzq& course) {
courses_0832lzq.push_back(course);
}
Course_0832lzq find_course_by_name_0832lzq(const string& name) const {
auto it = find_if(courses_0832lzq.begin(), courses_0832lzq.end(), [name](const Course_0832lzq& c) {return c.get_name_0832lzq() == name;});
if (it != courses_0832lzq.end()) {
return *it;
} else {
throw runtime_error("Course not found");
}
}
Course_0832lzq find_course_by_id_0832lzq(int id) const {
auto it = find_if(courses_0832lzq.begin(), courses_0832lzq.end(), [id](const Course_0832lzq& c) {return c.get_id_0832lzq() == id;});
if (it != courses_0832lzq.end()) {
return *it;
} else {
throw runtime_error("Course not found");
}
}
};
int main() {
CourseCatalog_0832lzq catalog;
catalog.add_course_0832lzq(Course_0832lzq("Math", 1, 3));
catalog.add_course_0832lzq(Course_0832lzq("Physics", 2, 4, {"Math"}));
catalog.add_course_0832lzq(Course_0832lzq("Chemistry", 3, 4, {"Math"}));
catalog.add_course_0832lzq(Course_0832lzq("Biology", 4, 3, {"Chemistry", "Physics"}));
Student_0832lzq alice_0832lzq("Alice", 1001);
alice_0832lzq.add_course_0832lzq(catalog.find_course_by_name_0832lzq("Math"));
alice_0832lzq.add_course_0832lzq(catalog.find_course_by_id_0832lzq(2));
alice_0832lzq.add_course_0832lzq(catalog.find_course_by_name_0832lzq("Biology"));
alice_0832lzq.print_courses_0832lzq();
Student_0832lzq bob_0832lzq("Bob", 1002);
bob_0832lzq.add_course_0832lzq(catalog.find_course_by_name_0832lzq("Chemistry"));
bob_0832lzq.add_course_0832lzq(catalog.find_course_by_name_0832lzq("Biology"));
bob_0832lzq.print_courses_0832lzq();
return 0;
}
```
这个程序新增了一个CourseCatalog类,表示一个课程目录,其中包含了所有的课程。CourseCatalog类提供了两个函数:add_course_0832lzq用于添加新课程,find_course_by_name_0832lzq和find_course_by_id_0832lzq用于通过课程名称或课程ID查找课程。
在主函数中,我们首先创建了一个CourseCatalog对象,并添加了四门课程。之后创建了两个学生(Alice和Bob),并让他们选修不同的课程。在选择课程时,学生对象将通过检查课程的先修课程以及学生已经选修的课程来判断是否有资格选修该课程。如果没有资格,则程序会输出相应的提示信息。
这个程序比之前的程序更为复杂,但是仍然只是一个简单的示例。实际的学生选课系统可能需要更多的功能和数据结构来支持更复杂的操作,例如课程时间表、教师信息、成绩管理等。
阅读全文