用c++实现宿舍管理系统 要求: (1)能根据学生的学号找到学生的宿舍号 (2)能为宿舍添加学生,达到人数(比如 6 人)之后,提示该宿舍已满。提供空宿 舍编号。 (3)能够提供换宿舍服务。 (4)能够查找某个班级的学生在哪些宿舍 (5)字段信息可包括:学号,姓名,专业,班级,宿舍号等。
时间: 2023-12-19 07:06:15 浏览: 145
好的,以下是一个用C++实现宿舍管理系统的示例代码,其中实现了题目要求的各项功能:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
// 学生类
class Student {
public:
Student(string id, string name, string major, string cls, string dorm)
: m_id(id), m_name(name), m_major(major), m_cls(cls), m_dorm(dorm) {}
string getId() const { return m_id; }
string getName() const { return m_name; }
string getMajor() const { return m_major; }
string getCls() const { return m_cls; }
string getDorm() const { return m_dorm; }
void setDorm(string dorm) { m_dorm = dorm; }
private:
string m_id; // 学号
string m_name; // 姓名
string m_major; // 专业
string m_cls; // 班级
string m_dorm; // 宿舍号
};
// 宿舍类
class Dormitory {
public:
Dormitory(string dorm, int capacity) : m_dorm(dorm), m_capacity(capacity) {}
string getDorm() const { return m_dorm; }
int getCapacity() const { return m_capacity; }
bool isFull() const { return m_students.size() >= m_capacity; }
bool addStudent(Student student) {
if (isFull()) {
return false;
}
m_students.push_back(student);
return true;
}
bool removeStudent(Student student) {
for (auto it = m_students.begin(); it != m_students.end(); ++it) {
if (it->getId() == student.getId()) {
m_students.erase(it);
return true;
}
}
return false;
}
vector<Student> getStudents() const { return m_students; }
private:
string m_dorm; // 宿舍号
int m_capacity; // 容量
vector<Student> m_students; // 住户信息
};
// 班级类
class Class {
public:
Class(string cls) : m_cls(cls) {}
string getCls() const { return m_cls; }
vector<Student> getStudents() const { return m_students; }
void addStudent(Student student) { m_students.push_back(student); }
private:
string m_cls; // 班级编号
vector<Student> m_students; // 学生信息
};
// 宿舍管理系统
class DormitoryManager {
public:
void addStudent(Student student) {
m_students[student.getId()] = student;
// 添加学生后,将其加入对应的班级中
m_classes[student.getCls()].addStudent(student);
}
string getDormByStudentId(string id) const {
if (m_students.find(id) != m_students.end()) {
return m_students.at(id).getDorm();
}
return "";
}
string getAvailableDorm() const {
for (auto it = m_dormitories.begin(); it != m_dormitories.end(); ++it) {
if (!it->second.isFull()) {
return it->second.getDorm();
}
}
return "";
}
bool addStudentToDorm(Student student, string dorm) {
if (m_dormitories.find(dorm) == m_dormitories.end()) {
return false;
}
if (!m_dormitories[dorm].addStudent(student)) {
return false;
}
// 添加学生后,更新其宿舍信息
m_students[student.getId()].setDorm(dorm);
return true;
}
bool removeStudentFromDorm(Student student) {
if (m_dormitories.find(student.getDorm()) == m_dormitories.end()) {
return false;
}
if (!m_dormitories[student.getDorm()].removeStudent(student)) {
return false;
}
// 删除学生后,更新其宿舍信息
m_students[student.getId()].setDorm("");
return true;
}
vector<Dormitory> getDormitoriesByClass(string cls) const {
vector<Dormitory> dorms;
if (m_classes.find(cls) == m_classes.end()) {
return dorms;
}
auto students = m_classes.at(cls).getStudents();
for (auto it = m_dormitories.begin(); it != m_dormitories.end(); ++it) {
for (auto st : students) {
if (it->second.getStudents().size() > 0 && it->second.getStudents().at(0).getCls() == st.getCls()) {
dorms.push_back(it->second);
break;
}
}
}
return dorms;
}
private:
map<string, Student> m_students; // 学生信息
map<string, Dormitory> m_dormitories; // 宿舍信息
map<string, Class> m_classes; // 班级信息
};
int main() {
DormitoryManager manager;
// 添加宿舍信息
manager.addStudent(Student("001", "张三", "计算机科学与技术", "1班", "A101"));
manager.addStudent(Student("002", "李四", "软件工程", "1班", ""));
manager.addStudent(Student("003", "王五", "信息安全", "2班", "A102"));
manager.addStudent(Student("004", "赵六", "物联网工程", "2班", ""));
manager.addStudent(Student("005", "钱七", "人工智能", "3班", "A103"));
manager.addStudent(Student("006", "孙八", "网络工程", "3班", ""));
manager.addStudent(Student("007", "周九", "数字媒体技术", "4班", "A104"));
manager.addStudent(Student("008", "吴十", "软件工程", "4班", ""));
// 查询学生宿舍号
cout << "学号为001的学生住在" << manager.getDormByStudentId("001") << "宿舍" << endl;
// 添加学生到宿舍
string dorm = manager.getAvailableDorm();
if (dorm != "") {
if (manager.addStudentToDorm(Student("009", "郑十一", "信息工程", "4班", ""), dorm)) {
cout << "添加成功,学生住在" << dorm << "宿舍" << endl;
}
else {
cout << "添加失败,宿舍已满" << endl;
}
}
else {
cout << "无可用宿舍" << endl;
}
// 换宿舍
auto students = manager.getDormitoriesByClass("4班").at(0).getStudents();
if (students.size() > 0) {
auto student = students.at(0);
string newDorm = manager.getAvailableDorm();
if (newDorm != "") {
if (manager.removeStudentFromDorm(student) && manager.addStudentToDorm(student, newDorm)) {
cout << "换宿舍成功,学生" << student.getName() << "住在" << newDorm << "宿舍" << endl;
}
else {
cout << "换宿舍失败" << endl;
}
}
else {
cout << "无可用宿舍" << endl;
}
}
return 0;
}
```
在上述代码中,我们定义了`Student`、`Dormitory`和`Class`三个类来分别表示学生、宿舍和班级信息,然后定义了`DormitoryManager`类来管理学生、宿舍和班级信息,并实现了题目要求的各项功能。具体实现细节可以参考代码中的注释。
阅读全文