请用visual stdio 2022编译器写一段c++代码 完成以下功能:题目:学生宿舍管理系统设计 功能:实现简单的学生宿舍基本信息管理,宿舍的基本信息包括楼号、房间号、面积、所容纳人数、已入住人数等基本要求:1设计简单的菜单,能够进行系统功能选择。2实现信息的录入功能。3在已有信息的基础上添加新的记录。4删除指定房间号的记录。5修改指定房间号的记录 6实现信息的浏览功能 7按房间号查询功能 8按已入住人数排序功能
时间: 2024-03-25 19:37:36 浏览: 73
很高兴为您提供这段代码,以下是实现宿舍管理系统的C++代码:
```
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 学生宿舍基本信息结构体
struct Dormitory {
int building; // 楼号
int room; // 房间号
double area; // 面积
int capacity; // 所容纳人数
int occupied; // 已入住人数
};
// 学生宿舍管理系统类
class DormitoryManagementSystem {
public:
DormitoryManagementSystem() {
// 初始化系统菜单
menu.push_back("1. 录入信息");
menu.push_back("2. 添加记录");
menu.push_back("3. 删除记录");
menu.push_back("4. 修改记录");
menu.push_back("5. 浏览信息");
menu.push_back("6. 按房间号查询");
menu.push_back("7. 按已入住人数排序");
menu.push_back("0. 退出系统");
}
// 显示菜单
void showMenu() {
cout << "欢迎使用学生宿舍管理系统!" << endl;
for (auto item : menu) {
cout << item << endl;
}
}
// 录入信息
void addDormitory() {
Dormitory dormitory;
cout << "请输入楼号: ";
cin >> dormitory.building;
cout << "请输入房间号: ";
cin >> dormitory.room;
cout << "请输入面积: ";
cin >> dormitory.area;
cout << "请输入所容纳人数: ";
cin >> dormitory.capacity;
cout << "请输入已入住人数: ";
cin >> dormitory.occupied;
dormitories.push_back(dormitory);
cout << "添加成功!" << endl;
}
// 添加记录
void addRecord() {
int room;
cout << "请输入要添加记录的房间号: ";
cin >> room;
for (auto& dormitory : dormitories) {
if (dormitory.room == room) {
cout << "请输入要添加的已入住人数: ";
cin >> dormitory.occupied;
cout << "添加成功!" << endl;
return;
}
}
cout << "该房间号不存在!" << endl;
}
// 删除记录
void deleteRecord() {
int room;
cout << "请输入要删除记录的房间号: ";
cin >> room;
for (auto it = dormitories.begin(); it != dormitories.end(); it++) {
if (it->room == room) {
dormitories.erase(it);
cout << "删除成功!" << endl;
return;
}
}
cout << "该房间号不存在!" << endl;
}
// 修改记录
void modifyRecord() {
int room;
cout << "请输入要修改记录的房间号: ";
cin >> room;
for (auto& dormitory : dormitories) {
if (dormitory.room == room) {
cout << "请输入新的楼号: ";
cin >> dormitory.building;
cout << "请输入新的房间号: ";
cin >> dormitory.room;
cout << "请输入新的面积: ";
cin >> dormitory.area;
cout << "请输入新的所容纳人数: ";
cin >> dormitory.capacity;
cout << "请输入新的已入住人数: ";
cin >> dormitory.occupied;
cout << "修改成功!" << endl;
return;
}
}
cout << "该房间号不存在!" << endl;
}
// 浏览信息
void browseDormitory() {
if (dormitories.empty()) {
cout << "暂无信息!" << endl;
return;
}
cout << "楼号\t房间号\t面积\t所容纳人数\t已入住人数" << endl;
for (auto dormitory : dormitories) {
cout << dormitory.building << "\t" << dormitory.room << "\t" << dormitory.area << "\t" << dormitory.capacity << "\t\t" << dormitory.occupied << endl;
}
}
// 按房间号查询
void queryByRoom() {
int room;
cout << "请输入要查询的房间号: ";
cin >> room;
for (auto dormitory : dormitories) {
if (dormitory.room == room) {
cout << "楼号\t房间号\t面积\t所容纳人数\t已入住人数" << endl;
cout << dormitory.building << "\t" << dormitory.room << "\t" << dormitory.area << "\t" << dormitory.capacity << "\t\t" << dormitory.occupied << endl;
return;
}
}
cout << "该房间号不存在!" << endl;
}
// 按已入住人数排序
void sortByOccupied() {
sort(dormitories.begin(), dormitories.end(), [](const Dormitory& a, const Dormitory& b) {
return a.occupied < b.occupied;
});
cout << "楼号\t房间号\t面积\t所容纳人数\t已入住人数" << endl;
for (auto dormitory : dormitories) {
cout << dormitory.building << "\t" << dormitory.room << "\t" << dormitory.area << "\t" << dormitory.capacity << "\t\t" << dormitory.occupied << endl;
}
}
private:
vector<string> menu; // 菜单
vector<Dormitory> dormitories; // 学生宿舍基本信息
};
int main() {
DormitoryManagementSystem dms; // 创建学生宿舍管理系统对象
int choice;
do {
dms.showMenu(); // 显示菜单
cout << "请输入功能编号: ";
cin >> choice;
switch (choice) {
case 1:
dms.addDormitory(); // 录入信息
break;
case 2:
dms.addRecord(); // 添加记录
break;
case 3:
dms.deleteRecord(); // 删除记录
break;
case 4:
dms.modifyRecord(); // 修改记录
break;
case 5:
dms.browseDormitory(); // 浏览信息
break;
case 6:
dms.queryByRoom(); // 按房间号查询
break;
case 7:
dms.sortByOccupied(); // 按已入住人数排序
break;
case 0:
cout << "谢谢使用!" << endl;
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
system("pause"); // 暂停,等待输入
system("cls"); // 清屏
} while (choice != 0);
return 0;
}
```
这个程序实现了所述的所有功能,并提供了简单的菜单供用户选择操作。
阅读全文