C++实现一个实验机房管理系统,其中机房共有40台计算机,分为5排,每排8台。管理系统应包念台计算机一周内的使用全部信息,包拄计算机编号、位置、状态、使用情况的历史记录(包括使用者姓名、学号、使用时间段)等。本系统可以模拟学生上机中和离开的登记过程,杳询机房计算机的使用情况。
时间: 2024-02-21 10:57:04 浏览: 18
好的,这是一个相对复杂的系统,我可以给您提供一些思路和建议。
首先,我们需要定义一个计算机类,该类应该包含计算机编号、位置、状态、使用情况的历史记录等信息。其中,状态包括空闲、已被预约和正在使用三种状态。历史记录应该包括使用者姓名、学号、使用时间段等信息。具体的代码实现如下:
```cpp
class Computer {
public:
int id; // 计算机编号
int row; // 所在排数
int col; // 所在列数
int status; // 计算机状态:0-空闲,1-已被预约,2-正在使用
vector<pair<string, string>> history; // 历史记录:使用者姓名和学号,使用时间段
Computer(int _id, int _row, int _col) {
id = _id;
row = _row;
col = _col;
status = 0;
}
};
```
接下来,我们可以定义一个机房类,该类包含40台计算机,并提供预约、使用和查询等功能。具体的代码实现如下:
```cpp
class ComputerRoom {
public:
vector<Computer> computers; // 机房中的计算机
ComputerRoom() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 8; j++) {
computers.push_back(Computer((i - 1) * 8 + j, i, j));
}
}
}
bool book(int id, string name, string sid, string start_time, string end_time) {
Computer& computer = computers[id - 1];
if (computer.status != 0) {
return false; // 计算机已被预约或正在使用
}
computer.status = 1;
computer.history.push_back({name, sid + " " + start_time + "-" + end_time});
return true;
}
bool use(int id, string name, string sid, string start_time, string end_time) {
Computer& computer = computers[id - 1];
if (computer.status != 0) {
return false; // 计算机已被预约或正在使用
}
computer.status = 2;
computer.history.push_back({name, sid + " " + start_time + "-" + end_time});
return true;
}
bool leave(int id) {
Computer& computer = computers[id - 1];
if (computer.status != 2) {
return false; // 计算机没有被使用
}
computer.status = 0;
return true;
}
int query() {
int count = 0;
for (int i = 0; i < 40; i++) {
if (computers[i].status != 0) {
++count;
}
}
return count;
}
};
```
最后,我们可以在主函数中测试该系统的功能,例如预约、使用、离开和查询。具体的代码实现如下:
```cpp
int main() {
ComputerRoom room;
string name, sid, start_time, end_time;
int id, choice;
while (true) {
cout << "请输入选项:1-预约,2-使用,3-离开,4-查询,0-退出" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入计算机编号、姓名、学号、开始使用时间和结束使用时间:" << endl;
cin >> id >> name >> sid >> start_time >> end_time;
if (room.book(id, name, sid, start_time, end_time)) {
cout << "预约成功" << endl;
} else {
cout << "预约失败" << endl;
}
break;
case 2:
cout << "请输入计算机编号、姓名、学号、开始使用时间和结束使用时间:" << endl;
cin >> id >> name >> sid >> start_time >> end_time;
if (room.use(id, name, sid, start_time, end_time)) {
cout << "使用成功" << endl;
} else {
cout << "使用失败" << endl;
}
break;
case 3:
cout << "请输入计算机编号:" << endl;
cin >> id;
if (room.leave(id)) {
cout << "离开成功" << endl;
} else {
cout << "离开失败" << endl;
}
break;
case 4:
cout << "当前使用计算机数量:" << room.query() << endl;
break;
case 0:
return 0;
default:
cout << "无效选项" << endl;
break;
}
}
return 0;
}
```
以上就是一个简单的实验机房管理系统的实现思路和代码。需要注意的是,该代码只是一个简单的示例,实际的应用场景可能更加复杂,需要根据实际情况进行修改和完善。