C++课程设计--学生宿舍管理系统

需求分析


        系统主要使用类链表,将链表的知识得到充分的使用;还是用到了函数重载。

 类定义

        主要包括学生类、宿舍类、报修类以及访客类。由于博主时间问题并没有写到访客类的代码,但是思路是一样的。

学生类

        数据成员:学生学号、姓名、性别、所在宿舍(还未分配时为“未分配”,毕业时为“已毕业”)。

        成员函数:增加学生信息、输出学生信息、增加报修信息、保存学生信息到文件、更换宿舍、从文件读取学生信息到链表。

class Student {
private:
	string student_id;//学号 
	string name;//姓名
	string sex;//性别
	string room_id;//寝室id
	Student* next;
public:
	Student(string i, string n, string s, string rid) :
        student_id(i), name(n), sex(s), room_id(rid), next(NULL) {}//构造函数
	void insertStudent(Student* shead);//增加学生信息
	bool display_student(Student* shead);//输出学生信息
	void sort_Student_by_student_id(Student* shead);//排序
	void add_Repair(string id, string bid, string info, Repair* rehead);//增加保修信息
	void change_room(Student* shead, Room* rhead, string student_id, string new_room_id);//更换寝室
	friend void save_student_to_file(Student* shead);//保存到文件
	friend Student* read_student_the_file();//读取到链表
	//辅助函数
	string get_id() { return student_id; }
	string get_sex() { return sex; }
	string get_room_id() { return room_id; }
	void set_room_id(string id) { room_id = id; }
	bool is_student(Student* shead,string id) {
		Student* temp = shead;
		while (temp != NULL) {
			if (temp->student_id == id && temp->room_id != "已毕业")
				return true;
			temp = temp->next;
		}
		cout << "无法登录" << endl;
		return false;
	}
	Student* get_next() { return next; }
	void set_next(Student* next_node) { next = next_node; }
};

 宿舍类

        数据成员:楼号(可有可无)、宿舍id、类型(男女)、剩余床位、学生信息(学生id)

        成员函数:增加宿舍信息、输出宿舍信息、分配学生宿舍、清空宿舍内学生信息、保存宿舍信息到文件、从文件读取信息到链表。

class Room {
private:
	string build;//楼号
	string id;//寝室号
	string sex;//寝室类别
	int bed_num;//床位剩余数
	string student_info[4];//学生信息
	Room* next;
public:
	Room(string b, string i, string s, int n) :build(b), id(i), sex(s), bed_num(n), next(NULL) {
		for (int i = 0; i < 4; i++)
			student_info[i] = "空";
	}//构造函数
	Room(string b, string i, string s, int n, string stuinfo[]) :build(b), id(i), sex(s), bed_num(n) {
		for (int i = 0; i < 4; i++)
			student_info[i] = stuinfo[i];
	}//构造函数重载,使用在读取
	void insertRoom(Room* rhead);//增加宿舍信息
	bool display_room(Room* rhead);//输出
	void allocation_of_rooms(Room* rhead, Student* shead);//按顺序分配宿舍
	void drop(Room* rhead, Student* shead);//清空寝室内学生信息
	friend void save_room_to_file(Room* rhead);//保存到文件
	friend Room* read_room_the_file();//读取
	//辅助函数
	string get_id() { return id; }
	string get_sex() { return sex; }
	int get_bed_num() { return bed_num; }
	void set_bed_num(int i) { bed_num = i; }
	string get_student_info(int index) { return student_info[index]; }
	void set_student_info(int index, string info) { student_info[index] = info; }
	Room* get_next() { return next; }
	void set_next(Room* next_node) { next = next_node; }
};

 报修类

        数据成员:学号、楼号

        成员函数:输出报修信息、保存报修信息到文件、从文件读取报修信息到链表。

class Repair {//报修
private:
	string sid;//学号
	string bid;//楼号
	string repair_description;//报修内容
	Repair* next;
public:
	Repair(string id, string bi, string info) :sid(id), bid(bi), repair_description(info), next(NULL) {}//构造函数
	bool display_repair(Repair* rehead);//输出报修信息
	friend void save_repair_to_file(Repair* rehead);//保存到文件
	friend Repair* read_repair_the_file();//读取
	//辅助函数
	Repair* get_next() { return next; }
	void set_next(Repair* next_node) { next = next_node; }
};

 实现功能

(1)学校增加学生信息

(2)为学生信息排序

(3)学校增加宿舍信息

(4)学校安排学生宿舍

(5)学校因学生毕业清空宿舍内学生信息(并将学生改为已毕业)

(6)输出宿舍信息

(7)输出学生信息

(8)查看学生提交的报修信息

(9)学生申请更换寝室

(10)学生提交报修信息

(11)保存和读取宿舍、学生和报修信息到文件

(12)应提供一个界面(菜单)来调用各个功能,调用界面和各个功能的操作界面应尽可能清晰美观。

 系统功能模块图


1.功能模块

  1. 1.学生申请更换宿舍功能模块学生提交报修信息功能模块
  2. 2.学校增加学生信息功能模块
  3. 3.学校增加宿舍信息功能模块
  4. 4.学校给学生顺序安排学生宿舍功能模块
  5. 5.学校清空宿舍内学术信息功能模块
  6. 6.输出学生信息、学校信息、报修信息功能模块
  7. 系统功能模块图

 课程报告具体质询QQ2263067368

源代码


#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;

class Room;
class Repair {//报修
private:
	string sid;//学号
	string bid;//楼号
	string repair_description;//报修内容
	Repair* next;
public:
	Repair(string id, string bi, string info) :sid(id), bid(bi), repair_description(info), next(NULL) {}//构造函数
	bool display_repair(Repair* rehead);//输出报修信息
	friend void save_repair_to_file(Repair* rehead);//保存到文件
	friend Repair* read_repair_the_file();//读取
	//辅助函数
	Repair* get_next() { return next; }
	void set_next(Repair* next_node) { next = next_node; }
};
class Student {
private:
	string student_id;//学号 
	string name;//姓名
	string sex;//性别
	string room_id;//寝室id
	Student* next;
public:
	Student(string i, string n, string s, string rid) :student_id(i), name(n), sex(s), room_id(rid), next(NULL) {}//构造函数
	void insertStudent(Student* shead);//增加学生信息
	bool display_student(Student* shead);//输出学生信息
	void sort_Student_by_student_id(Student* shead);//排序
	void add_Repair(string id, string bid, string info, Repair* rehead);//增加保修信息
	void change_room(Student* shead, Room* rhead, string student_id, string new_room_id);//更换寝室
	friend void save_student_to_file(Student* shead);//保存到文件
	friend Student* read_student_the_file();//读取到链表
	//辅助函数
	string get_id() { return student_id; }
	string get_sex() { return sex; }
	string get_room_id() { return room_id; }
	void set_room_id(string id) { room_id = id; }
	bool is_student(Student* shead,string id) {
		Student* temp = shead;
		while (temp != NULL) {
			if (temp->student_id == id && temp->room_id != "已毕业")
				return true;
			temp = temp->next;
		}
		cout << "无法登录" << endl;
		return false;
	}
	Student* get_next() { return next; }
	void set_next(Student* next_node) { next = next_node; }
};
class Room {
private:
	string build;//楼号
	string id;//寝室号
	string sex;//寝室类别
	int bed_num;//床位剩余数
	string student_info[4];//学生信息
	Room* next;
public:
	Room(string b, string i, string s, int n) :build(b), id(i), sex(s), bed_num(n), next(NULL) {
		for (int i = 0; i < 4; i++)
			student_info[i] = "空";
	}//构造函数
	Room(string b, string i, string s, int n, string stuinfo[]) :build(b), id(i), sex(s), bed_num(n) {
		for (int i = 0; i < 4; i++)
			student_info[i] = stuinfo[i];
	}//构造函数重载,使用在读取
	void insertRoom(Room* rhead);//增加宿舍信息
	bool display_room(Room* rhead);//输出
	void allocation_of_rooms(Room* rhead, Student* shead);//按顺序分配宿舍
	void drop(Room* rhead, Student* shead);//清空寝室内学生信息
	friend void save_room_to_file(Room* rhead);//保存到文件
	friend Room* read_room_the_file();//读取
	//辅助函数
	string get_id() { return id; }
	string get_sex() { return sex; }
	int get_bed_num() { return bed_num; }
	void set_bed_num(int i) { bed_num = i; }
	string get_student_info(int index) { return student_info[index]; }
	void set_student_info(int index, string info) { student_info[index] = info; }
	Room* get_next() { return next; }
	void set_next(Room* next_node) { next = next_node; }
};

bool Repair::display_repair(Repair* rehead) {
	Repair* temp = rehead->next;
	if (temp == NULL) { cout << "暂无报修信息" << endl; return false; }
	cout << "学号\t\t" << "寝室\t" << "报修内容" << endl;
	while (temp != NULL) {
		cout << temp->sid << "\t" << temp->bid << "\t" << temp->repair_description << endl;
		temp = temp->next;
	}
	return true;
}
void save_repair_to_file(Repair* rehead) {
	ofstream outfile;
	outfile.open("Repair.txt");
	if (!outfile.is_open()) {
		cout << "无法打开文件" << endl;
		return;
	}
	Repair* temp = rehead->next;
	while (temp != NULL) {
		outfile << temp->sid << "," << temp->bid << "," << temp->repair_description << endl;
		temp = temp->next;
	}
	outfile.close();
}
Repair* read_repair_the_file() {
	ifstream infile;
	infile.open("Repair.txt", ios::app);
	if (!infile.is_open()) {
		cout << "无法打开Repair.txt文件" << endl;
		return NULL;
	}
	Repair* temp, * rehead = new Repair("", "", "");
	string line, field;
	while (getline(infile, line)) {
		istringstream iss(line);
		getline(iss, field, ',');//sid
		string sid = field;
		getline(iss, field, ',');//bid
		string bid = field;
		getline(iss, field, ',');//repair_description
		string repair_description = field;
		Repair* p = new Repair(sid, bid, repair_description);
		temp = rehead;
		while (temp->next != NULL) {
			temp = temp->next;
		}
		temp->next = p;
	}
	infile.close();
	return rehead;
}

void Student::insertStudent(Student* shead) {
	cout << "请输入增加人数" << endl;
	int n;
	string id, name, sex;
	cin >> n;
	for (int i = 0; i < n; i++) {
	folag1:
		cout << "请输入第" << i + 1 << "个人信息\n" << "学号(9位)\t" << "姓名\t" << "性别(男/女)" << endl;
		cin >> id >> name >> sex;
		if (id.length() < 9) {
			cout << "第" << i + 1 << "个学生学号输入错误,请重新输入" << endl;
			goto folag1;
		}
		if (sex == "男" || sex == "女") {
			Student* p = new Student(id, name, sex, "未分配");
			Student* temp = shead;
			bool unique = true;
			while (temp != NULL) {//判断学号是否重复 
				if (temp->student_id == id) {
					unique = false;
					cout << "学号已存在于链表中,不允许添加该学生信息" << endl;
					break;
				}
				temp = temp->next;
			}
			if (unique) {//学号不重复 
				Student* p = new Student(id, name, sex, "未分配");
				temp = shead;
				while (temp->next != NULL) {//找到标尾 
					temp = temp->next;
				}
				temp->next = p;
			}
		}
		else {
			cout << "性别输入错误,请重新输入" << endl;
			goto folag1;
		}
	}
	shead->sort_Student_by_student_id(shead);
	save_student_to_file(shead);
}
bool Student::display_student(Student* shead) {
	Student* p = shead->next;
	if (p == NULL) {
		cout << "学生信息为空" << endl;
		return false;
	}
	cout << "学号\t\t" << "姓名\t" << "性别\t" << "寝室" << endl;
	while (p != NULL) {
		if (p->room_id == "已毕业")p = p->next;
		else {
			cout << p->student_id << "\t" << p->name << "\t" << p->sex << "\t" << p->room_id << endl;
			p = p->next;
		}
	}
	return true;
}
void Student::sort_Student_by_student_id(Student* shead) {
	if (shead->next == NULL || shead->next->next == NULL) {//列表为空或到链表尾 
		return;
	}
	Student* current = shead->next;
	Student* index = NULL;
	string temp_id, temp_name, temp_sex, temp_room_id;
	while (current != NULL) {
		index = current->next;
		while (index != NULL) {
			if (current->student_id > index->student_id) {//交换 
				temp_id = current->student_id;
				temp_name = current->name;
				temp_sex = current->sex;
				temp_room_id = current->room_id;

				current->student_id = index->student_id;
				current->name = index->name;
				current->sex = index->sex;
				current->room_id = index->room_id;

				index->student_id = temp_id;
				index->name = temp_name;
				index->sex = temp_sex;
				index->room_id = temp_room_id;
			}
			index = index->next;
		}
		current = current->next;
	}
}
Room* read_room_the_file() {
	ifstream infile;
	infile.open("Room.txt", ios::app);
	if (!infile.is_open()) {
		cout << "无法打开Room.txt文件" << endl;
		return NULL;
	}
	Room* temp, * rhead = new Room("", "", "", 4);
	string line, field;
	while (getline(infile, line)) {
		istringstream iss(line);
		getline(iss, field, ',');//build
		string build = field;
		getline(iss, field, ',');//id
		string id = field;
		getline(iss, field, ',');//sex
		string sex = field;
		getline(iss, field, ',');//bed_num
		int bed_num = std::stoi(field);
		string student_info[4];
		for (int i = 0; i < 4; i++) {
			getline(iss, field, ','); // student_info
			student_info[i] = field;
		}
		Room* p = new Room(build, id, sex, bed_num, student_info);
		temp = rhead;
		while (temp->next != NULL) {
			temp = temp->next;
		}
		temp->next = p;
	}
	infile.close();
	return rhead;
}
void Student::add_Repair(string sid, string bid, string info, Repair* rehead) {
	//判断寝室号是否存在
	bool room_found = false;
	Room* temp = read_room_the_file();
	while (temp != NULL) {
		if (temp->get_id() == bid) {
			room_found = true;
			break;
		}
		temp = temp->get_next();
	}
	if (!room_found) {
		cout << "寝室号不存在,请重新输入" << endl;
		return;
	}
	Repair* temp1 = rehead;
	Repair* newRepair = new Repair(sid, bid, info);
	while (temp1->get_next() != NULL) {
		temp1 = temp1->get_next();
	}
	temp1->set_next(newRepair);
	save_repair_to_file(rehead);
}
void Student::change_room(Student* shead, Room* rhead, string student_id, string new_room_id) {//邱秀彪
	Student* temp = shead;
	Room* new_room = rhead;
	bool student_found = false;//学号是否存在,并指向该学生 
	while (temp != NULL) {
		if (temp->student_id == student_id) {
			student_found = true;
			break;
		}
		temp = temp->next;
	}
	if (!student_found) {
		cout << "学生不存在,请重新输入" << endl;
		return;
	}
	bool room_found = false;//新宿舍是否存在
	while (new_room != NULL) {
		if (new_room->get_id() == new_room_id) {
			room_found = true;
			break;
		}
		new_room = new_room->get_next();
	}
	if (!room_found) {
		cout << "新寝室号不存在,请重新输入" << endl;
		return;
	}
	if (new_room->get_bed_num() == 0||new_room->get_sex() != temp->sex) {//无剩余床位
		cout << "无法更换" << endl;
		return;
	}
	Room* old_room = rhead;//找到申请学生的宿舍类的位置
	while (old_room != NULL) {
		if (old_room->get_id() == temp->get_room_id()) {
			break;
		}
		old_room = old_room->get_next();
	}
	temp->set_room_id(new_room_id);
	for (int i = 0; i < 4; i++) {//更新旧寝室信息
		if (old_room->get_student_info(i) == student_id) {
			old_room->set_student_info(i, "空");
			break;
		}
	}
	for (int i = 0; i < 4; i++) {//更新新寝室信息
		if (new_room->get_student_info(i) == "空") {
			new_room->set_student_info(i, student_id);
			break;
		}
	}
	old_room->set_bed_num(old_room->get_bed_num() + 1);
	new_room->set_bed_num(new_room->get_bed_num() - 1);
	cout << "成功更换寝室" << endl;
	save_room_to_file(rhead);
	save_student_to_file(shead);
}
void save_student_to_file(Student* shead) {
	ofstream outfile;
	outfile.open("Student.txt");
	if (!outfile.is_open()) {
		cout << "无法打开文件" << endl;
		return;
	}
	Student* temp = shead->next;
	while (temp != NULL) {
		outfile << temp->student_id << "," << temp->name << "," << temp->sex << "," << temp->room_id << endl;
		temp = temp->next;
	}
	outfile.close();
}
Student* read_student_the_file() {
	ifstream infile;
	infile.open("Student.txt", ios::app);
	if (!infile.is_open()) {
		cout << "无法打开Student.text文件" << endl;
		return NULL;
	}
	Student* temp, * shead = new Student("", "", "", "");
	string line, field;
	while (getline(infile, line)) {
		istringstream iss(line);
		getline(iss, field, ',');//student_id
		string student_id = field;
		getline(iss, field, ',');//name
		string name = field;
		getline(iss, field, ',');//sex
		string sex = field;
		getline(iss, field, ',');//room_id
		string room_id = field;
		Student* p = new Student(student_id, name, sex, room_id);
		temp = shead;
		while (temp->next != NULL) {
			temp = temp->next;
		}
		temp->next = p;
	}
	infile.close();
	return shead;
}
void Room::insertRoom(Room* rhead) {
	Room* temp = rhead;
	int n;
	string build, id, sex;
	cout << "请输入增加房间数" << endl;
	cin >> n;
	for (int i = 0; i < n; i++) {
	folag2:
		cout << "请输入第" << i + 1 << "个楼栋信息\n" << "楼号\t" << "寝室号\t" << "性别(男/女)" << endl;
		cin >> build >> id >> sex;
		if (sex == "男" || sex == "女") {
			Room* p = new Room(build, id, sex, 4);
			Room* temp = rhead;
			bool unique = true;
			while (temp != NULL) {
				if (temp->id == id) {
					unique = false;
					cout << "寝室号重复,不允许增加该寝室信息" << endl;
					return;
				}
				temp = temp->next;
			}
			if (unique) {
				Room* p = new Room(build, id, sex, 4);
				temp = rhead;
				while (temp->next != NULL) {//找到表尾 
					temp = temp->next; 
				}
				temp->next = p;
			}
		}
		else {
			cout << "性别输入错误,请重新输入" << endl;
			goto folag2;
		}
	}
	save_room_to_file(rhead);
}
bool Room::display_room(Room* rhead) {
	Room* p = rhead->next;
	if (p == NULL) {
		cout << "寝室信息为空" << endl;
		return false;
	}
	cout << "楼号\t" << "寝室号\t" << "寝室类别\t" << "剩余床位\t" << "已入住学生" << endl;
	while (p != NULL) {
		cout << p->build << "\t" << p->id << "\t" << p->sex << "\t\t" << p->bed_num << "\t\t";
		for (int n = 0; n < 4; n++) {
			if (p->student_info[n] != "空")
				cout << p->student_info[n] << " ";
		}
		cout << endl;
		p = p->next;
	}
	return true;
}
void Room::allocation_of_rooms(Room* rhead, Student* shead) {
	Room* rp = rhead;
	Student* sp = shead;
	if (rhead == NULL) {
		cout << "没有寝室信息,请先输入寝室信息." << endl;
		return;
	}
	//分配寝室信息
	if (sp == NULL) {
		cout << "学生信息为空,请输入学生信息。" << endl;
		return;
	}
	while (sp) {//遍历每个学生 
		if (sp->get_room_id() == "未分配") {//找到没有分配寝室的学生 
			rp = rhead;//每次访问一个学生房间从头开始遍历 
			while (rp && (rp->bed_num == 0 || rp->sex != sp->get_sex())) {//遍历宿舍找到合适的room
				rp = rp->next;
			}
			if (rp == NULL) { cout << sp->get_id() << "无符合的宿舍" << endl; }
			else {//找到合适的 
				sp->set_room_id(rp->id);//设置学生寝室id 
				rp->bed_num--;//寝室床位减少 
				for (int i = 0; i < 4; i++) {
					if (rp->student_info[i] == "空") {
						rp->student_info[i] = sp->get_id();//录入学生信息 
						break;
					}
				}
			}
			sp = sp->get_next();
		}
		else//没找到下一个 
			sp = sp->get_next();
	}
	cout << "分配后学生信息如下" << endl;
	shead->display_student(shead);
	save_student_to_file(shead);
	save_room_to_file(rhead);
}
void save_room_to_file(Room* rhead) {
	ofstream outfile;
	outfile.open("Room.txt");
	if (!outfile.is_open()) {
		cout << "无法打开文件" << endl;
		return;
	}
	Room* temp = rhead->next;
	while (temp != NULL) {
		outfile << temp->build << "," << temp->id << "," << temp->sex << "," << temp->bed_num << ",";
		for (int i = 0; i < 4; i++) {
			outfile << temp->student_info[i];
			if (i < 3) {
				outfile << ",";
			}
		}
		outfile << endl;
		temp = temp->next;
	}
	outfile.close();
}
void Room::drop(Room* rhead, Student* shead) {
	string id; bool room_found = false;
	cout << "现有寝室信息如下:" << endl;
	if (!rhead->display_room(rhead)) return;
	cout << "输入清空寝室id:";
	cin >> id;
	Room* rtemp = rhead; Student* stemp = shead;
	while (rtemp != NULL) {
		if (rtemp->id == id) {
			room_found = true;
			rtemp->bed_num = 4;
			for (int i = 0; i < 4; i++) {
				if (rtemp->student_info[i] != "空")
					rtemp->student_info[i] = "空";
			}
		}
		rtemp = rtemp->next;
	}
	if(!room_found) {
		cout << "未找到指定的寝室,请重新输入" << endl;
		return;
	}
	while (stemp != NULL) {
		if (stemp->get_room_id() == id) {
			stemp->set_room_id("已毕业");
		}
		stemp = stemp->get_next();
	}
	save_room_to_file(rhead);
	save_student_to_file(shead);
}
void menu() {
	system("cls");
	cout << "                      软件工程                   " << endl;
	cout << "**************************************************" << endl;
	cout << "*              学生宿舍管理系统                  *" << endl;
	cout << "**************************************************" << endl;
	cout << "*              请问你想访问的平台                *" << endl;
	cout << "*          1.学生端           2.学校端           *" << endl;
	cout << "*                 其他:退出系统                  *" << endl;
	cout << "**************************************************" << endl;
}
void student_mune(string id) {
	system("cls");
	cout << "                        软件工程                  " << endl;
	cout << "**************************************************" << endl;
	cout << "*                欢迎" << id << "来到学生端         *" << endl;
	cout << "**************************************************" << endl;
	cout << "*                 1.申请更换寝室                 *" << endl;
	cout << "*                 2.申请保修                     *" << endl;
	cout << "*                 其他.返回上一菜单              *" << endl;
	cout << "**************************************************" << endl;
}
void stud() {
	bool end = true;
	string student_id;
	cout << "输入你的学号:";
	cin >> student_id;
	if (!read_student_the_file()->is_student(read_student_the_file(), student_id)) { system("pause"); return; }
	while (end) {
		student_mune(student_id);
		int i;
		cout << "输入操作代码:";
		cin >> i;
		Student* shead = read_student_the_file();
		Room* rhead = read_room_the_file();
		Repair* rehead = read_repair_the_file();
		switch (i) {
		case 1: {
			cout << "现有寝室信息如下:" << endl;
			if (!rhead->display_room(rhead)) {break;}
			string new_room_id;
			cout << "请输入更换寝室号:";
			cin >> new_room_id;
			shead->change_room(shead, rhead, student_id, new_room_id);
			system("pause"); break;
		}
		case 2: {
			string room_id, info;
			cout << "请输入寝室号\t报修信息:" << endl;
			cin >> room_id >> info;
			shead->add_Repair(student_id, room_id, info, rehead);
			system("pause"); break;
		}
		default: end = false; break;
		}
	}
}
void school_menu() {
	system("cls");
	cout << "                        软件工程                  " << endl;
	cout << "**************************************************" << endl;
	cout << "*                 欢迎来到学校端                 * " << endl;
	cout << "**************************************************" << endl;
	cout << "*                 1.增加寝室信息                 *" << endl;
	cout << "*                 2.增加学生信息                 *" << endl;
	cout << "*                 3.输出寝室信息                 *" << endl;
	cout << "*                 4.输出学生信息                 *" << endl;
	cout << "*                 5.分配学生寝室                 *" << endl;
	cout << "*                 6.查看保修信息                 *" << endl;
	cout << "*                 7.清空寝室                     *" << endl;
	cout << "*                 其他.返回上一菜单              *" << endl;
	cout << "**************************************************" << endl;
}
void school() {
	int n; bool end = true;
	while (end)
	{
		school_menu();
		Student* shead = read_student_the_file();
		Room* rhead = read_room_the_file();
		Repair* rehead = read_repair_the_file();
		cout << "请输入操作代码:";
		cin >> n;
		switch (n) {
		case 1:rhead->insertRoom(rhead); system("pause"); break;
		case 2:shead->insertStudent(shead); system("pause"); break;
		case 3:rhead->display_room(rhead); system("pause"); break;
		case 4:shead->display_student(shead); system("pause"); break;
		case 5:rhead->allocation_of_rooms(rhead, shead); system("pause"); break;
		case 6:rehead->display_repair(rehead); system("pause"); break;
		case 7:rhead->drop(rhead, shead);  system("pause"); break;
		default:end = false; break;
		}
	}
}
int main()
{
	int n = 0;
	while (true)
	{
		menu();
		cout << "输入操作代码:";
		cin >> n;
		switch (n) {
		case 1:stud(); break;
		case 2:school(); break;
		default:
			exit(0);
		}
	}
	return 0;
}

  • 23
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我不能为您提供完整的代码。但是,我可以为您提供一个简单的宿舍管理系统的框架,您可以根据自己的需求进行修改和完善。 ```c++ #include <iostream> #include <string> #include <vector> using namespace std; // 学生类 class Student { private: string name; int id; string dormitory; public: Student(string n, int i, string d) { name = n; id = i; dormitory = d; } string getName() { return name; } int getId() { return id; } string getDormitory() { return dormitory; } }; // 宿舍类 class Dormitory { private: string name; int capacity; vector<Student*> students; // 存储学生的指针 public: Dormitory(string n, int c) { name = n; capacity = c; } string getName() { return name; } int getCapacity() { return capacity; } int getOccupied() { return students.size(); } bool addStudent(Student* s) { if (students.size() < capacity) { students.push_back(s); return true; } return false; } void removeStudent(Student* s) { for (int i = 0; i < students.size(); i++) { if (students[i]->getId() == s->getId()) { students.erase(students.begin() + i); break; } } } void printStudents() { for (int i = 0; i < students.size(); i++) { cout << students[i]->getName() << " " << students[i]->getId() << endl; } } }; // 宿舍楼类 class DormitoryBuilding { private: vector<Dormitory*> dormitories; // 存储宿舍的指针 public: bool addDormitory(Dormitory* d) { dormitories.push_back(d); return true; } Dormitory* findDormitory(string name) { for (int i = 0; i < dormitories.size(); i++) { if (dormitories[i]->getName() == name) { return dormitories[i]; } } return NULL; } }; int main() { // 创建宿舍楼 DormitoryBuilding* dormitoryBuilding = new DormitoryBuilding(); // 创建宿舍 Dormitory* dormitory1 = new Dormitory("Dormitory 1", 4); dormitoryBuilding->addDormitory(dormitory1); Dormitory* dormitory2 = new Dormitory("Dormitory 2", 3); dormitoryBuilding->addDormitory(dormitory2); Dormitory* dormitory3 = new Dormitory("Dormitory 3", 2); dormitoryBuilding->addDormitory(dormitory3); // 创建学生 Student* student1 = new Student("Tom", 1001, ""); dormitory1->addStudent(student1); Student* student2 = new Student("Jerry", 1002, ""); dormitory1->addStudent(student2); Student* student3 = new Student("Mary", 1003, ""); dormitory1->addStudent(student3); Student* student4 = new Student("John", 1004, ""); dormitory1->addStudent(student4); Student* student5 = new Student("Bob", 1005, ""); dormitory2->addStudent(student5); Student* student6 = new Student("Alice", 1006, ""); dormitory2->addStudent(student6); Student* student7 = new Student("David", 1007, ""); dormitory2->addStudent(student7); Student* student8 = new Student("Lily", 1008, ""); dormitory3->addStudent(student8); Student* student9 = new Student("Kate", 1009, ""); dormitory3->addStudent(student9); // 输出宿舍信息 Dormitory* dormitory = dormitoryBuilding->findDormitory("Dormitory 1"); cout << dormitory->getName() << " capacity: " << dormitory->getCapacity() << " occupied: " << dormitory->getOccupied() << endl; dormitory->printStudents(); return 0; } ``` 在这个简单的框架中,我们定义了三个类:学生类、宿舍类和宿舍楼类。学生类包含学生的姓名、学号和宿舍信息;宿舍类包含宿舍的名称、容量、学生列表等信息;宿舍楼类包含宿舍列表等信息。我们在 `main()` 函数中创建了宿舍楼、宿舍和学生,并将学生添加到相应的宿舍中,最后输出宿舍信息。 注意,这只是一个简单的框架,您需要根据自己的需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值