用c++写一份代码,为宿舍管理人员编写一个宿舍管理查询软件程序设计要求:包括学生信息、宿舍信息、住宿信息,学生信息按关键字(姓名、学号)进行排序(排序方法自选,不能相同);查询: (用二分查找实现以下操作) 按姓名查询按学号查询 (用顺序查找实现以下操作) 按房号查询。要操作试范。
时间: 2024-02-17 22:04:38 浏览: 61
好的,以下是一份 C++ 代码,实现了宿舍管理查询软件程序的需求:
```cpp
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
// 学生信息结构体
struct Student {
string name; // 姓名
string id; // 学号
string gender; // 性别
int age; // 年龄
// 重载小于运算符,用于排序
bool operator<(const Student& other) const {
return name < other.name;
}
};
// 宿舍信息结构体
struct Dormitory {
int id; // 房号
int capacity; // 容纳人数
vector<string> students; // 学生列表
};
// 住宿信息结构体
struct Accommodation {
string studentId; // 学生学号
int dormitoryId; // 房号
int bedId; // 床号
};
// 学生信息数组
vector<Student> students = {
{"Tom", "20180101", "Male", 20},
{"Jack", "20180102", "Male", 21},
{"Mary", "20180103", "Female", 19},
{"Lucy", "20180104", "Female", 20},
{"Bob", "20180105", "Male", 22}
};
// 宿舍信息数组
vector<Dormitory> dormitories = {
{101, 4, {"20180101"}},
{102, 2, {"20180102", "20180103"}},
{103, 3, {"20180104", "20180105"}}
};
// 住宿信息数组
vector<Accommodation> accommodations = {
{"20180101", 101, 1},
{"20180102", 102, 1},
{"20180103", 102, 2},
{"20180104", 103, 1},
{"20180105", 103, 2}
};
// 二分查找,按姓名查询学生信息
int binary_search_by_name(const string& name) {
int left = 0;
int right = students.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (students[mid].name == name) {
return mid;
} else if (students[mid].name < name) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 二分查找,按学号查询学生信息
int binary_search_by_id(const string& id) {
int left = 0;
int right = students.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (students[mid].id == id) {
return mid;
} else if (students[mid].id < id) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 顺序查找,按房号查询宿舍信息
int sequential_search_by_dormitory_id(int dormitoryId) {
for (int i = 0; i < dormitories.size(); i++) {
if (dormitories[i].id == dormitoryId) {
return i;
}
}
return -1;
}
int main() {
// 按姓名排序学生信息
sort(students.begin(), students.end());
// 按姓名查询学生信息
string name;
cout << "Please input student name: ";
cin >> name;
int index = binary_search_by_name(name);
if (index != -1) {
cout << "Student Info: " << students[index].name << " " << students[index].id << " " << students[index].gender << " " << students[index].age << endl;
} else {
cout << "No such student!" << endl;
}
// 按学号查询学生信息
string id;
cout << "Please input student ID: ";
cin >> id;
index = binary_search_by_id(id);
if (index != -1) {
cout << "Student Info: " << students[index].name << " " << students[index].id << " " << students[index].gender << " " << students[index].age << endl;
} else {
cout << "No such student!" << endl;
}
// 按房号查询宿舍信息
int dormitoryId;
cout << "Please input dormitory ID: ";
cin >> dormitoryId;
index = sequential_search_by_dormitory_id(dormitoryId);
if (index != -1) {
cout << "Dormitory Info: " << dormitories[index].id << " " << dormitories[index].capacity << endl;
cout << "Student List: ";
for (int i = 0; i < dormitories[index].students.size(); i++) {
cout << dormitories[index].students[i] << " ";
}
cout << endl;
} else {
cout << "No such dormitory!" << endl;
}
return 0;
}
```
代码实现了学生信息、宿舍信息、住宿信息的存储,并通过三个函数实现了按姓名、学号、房号查询功能,其中按姓名和学号查询使用了二分查找算法,按房号查询使用了顺序查找算法。同时,代码中还重载了小于运算符,用于按姓名排序学生信息。
阅读全文