需求分析
系统主要使用类链表,将链表的知识得到充分的使用;还是用到了函数重载。
类定义
主要包括学生类、宿舍类、报修类以及访客类。由于博主时间问题并没有写到访客类的代码,但是思路是一样的。
学生类
数据成员:学生学号、姓名、性别、所在宿舍(还未分配时为“未分配”,毕业时为“已毕业”)。
成员函数:增加学生信息、输出学生信息、增加报修信息、保存学生信息到文件、更换宿舍、从文件读取学生信息到链表。
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.学生申请更换宿舍功能模块学生提交报修信息功能模块
- 2.学校增加学生信息功能模块
- 3.学校增加宿舍信息功能模块
- 4.学校给学生顺序安排学生宿舍功能模块
- 5.学校清空宿舍内学术信息功能模块
- 6.输出学生信息、学校信息、报修信息功能模块
课程报告具体质询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;
}
标签:学生宿舍,课程设计,room,temp,--,next,student,id,string
From: https://blog.csdn.net/xiubiao/article/details/140110200