用Dev C++写一个学生宿舍管理系统APP
时间: 2023-12-27 17:23:20 浏览: 79
好的,下面是一个简单的学生宿舍管理系统APP的代码,使用Dev C++编写:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
private:
string name;
int age;
string major;
string dormitory;
public:
Student(string n, int a, string m, string d) {
name = n;
age = a;
major = m;
dormitory = d;
}
void display() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "专业:" << major << endl;
cout << "宿舍:" << dormitory << endl;
}
string getName() {
return name;
}
};
class Dormitory {
private:
string id;
int capacity;
vector<Student*> students;
public:
Dormitory(string i, int c) {
id = i;
capacity = c;
}
void addStudent(Student* s) {
if (students.size() < capacity) {
students.push_back(s);
cout << s->getName() << "成功入住" << id << "宿舍" << endl;
}
else {
cout << id << "宿舍已满,无法入住" << endl;
}
}
void displayStudents() {
cout << id << "宿舍的居民:" << endl;
for (int i = 0; i < students.size(); i++) {
students[i]->display();
cout << endl;
}
}
};
class DormitoryManager {
private:
vector<Dormitory*> dormitories;
public:
void addDormitory(Dormitory* d) {
dormitories.push_back(d);
cout << "成功添加宿舍:" << d->id << endl;
}
void addStudentToDormitory(Student* s, Dormitory* d) {
for (int i = 0; i < dormitories.size(); i++) {
if (dormitories[i] == d) {
dormitories[i]->addStudent(s);
return;
}
}
cout << "找不到该宿舍" << endl;
}
void displayAllDormitories() {
cout << "所有宿舍信息:" << endl;
for (int i = 0; i < dormitories.size(); i++) {
cout << "宿舍号:" << dormitories[i]->id << ",容纳人数:" << dormitories[i]->capacity << endl;
dormitories[i]->displayStudents();
cout << endl;
}
}
};
int main() {
DormitoryManager dm;
Dormitory* d1 = new Dormitory("A101", 3);
Dormitory* d2 = new Dormitory("B102", 2);
dm.addDormitory(d1);
dm.addDormitory(d2);
Student* s1 = new Student("张三", 18, "计算机科学与技术", "A101");
Student* s2 = new Student("李四", 19, "软件工程", "A101");
Student* s3 = new Student("王五", 20, "信息安全", "A101");
Student* s4 = new Student("赵六", 21, "网络工程", "B102");
dm.addStudentToDormitory(s1, d1);
dm.addStudentToDormitory(s2, d1);
dm.addStudentToDormitory(s3, d1);
dm.addStudentToDormitory(s4, d2);
dm.displayAllDormitories();
return 0;
}
```
这个系统包含了三个类:`Student`,`Dormitory`和`DormitoryManager`。`Student`表示一个学生的信息,包括姓名,年龄,专业和宿舍号;`Dormitory`表示一个宿舍的信息,包括宿舍号,容纳人数和居民列表;`DormitoryManager`是一个管理器类,负责管理所有的宿舍和学生,提供添加宿舍、添加学生到宿舍和显示所有宿舍信息等功能。在主函数中,我们创建了两个宿舍对象,四个学生对象,并将学生分配到宿舍中,最后显示所有宿舍的信息。
阅读全文