c++编写
时间: 2023-06-29 10:16:33 浏览: 132
利用C++编写一个猜字游戏.rar
好的,以下是C++实现机房收费管理系统的示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 学生类
class Student {
public:
string id; // 学号
string name; // 姓名
string className; // 班级
int startTime; // 上机起始时间
int endTime; // 下机结束时间
Student(string id, string name, string className, int startTime) {
this->id = id;
this->name = name;
this->className = className;
this->startTime = startTime;
}
// 计算上机费用
int getCharge() {
int duration = endTime - startTime;
int hours = duration / 3600; // 将秒转换为小时
if (duration % 3600 != 0) {
hours++; // 不足一小时按一小时计算
}
return hours;
}
// 打印学生信息
void printInfo() {
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "班级:" << className << endl;
cout << "上机时间:" << startTime << endl;
cout << "下机时间:" << endTime << endl;
cout << "上机费用:" << getCharge() << "元" << endl;
}
};
// 机房类
class ComputerRoom {
public:
int id; // 机房编号
int totalComputers; // 机房总共的电脑数量
int freeComputers; // 机房空闲的电脑数量
ComputerRoom(int id, int totalComputers) {
this->id = id;
this->totalComputers = totalComputers;
this->freeComputers = totalComputers;
}
// 显示机房信息
void showInfo() {
cout << "机房编号:" << id << endl;
cout << "总共电脑数量:" << totalComputers << endl;
cout << "空闲电脑数量:" << freeComputers << endl;
}
};
// 机房管理类
class ComputerRoomManager {
public:
vector<Student> students; // 学生列表
vector<ComputerRoom> rooms; // 机房列表
// 添加学生
void addStudent(Student student) {
students.push_back(student);
}
// 添加机房
void addRoom(ComputerRoom room) {
rooms.push_back(room);
}
// 显示学生信息
void showStudents() {
cout << "学生信息如下:" << endl;
for (int i = 0; i < students.size(); i++) {
students[i].printInfo();
cout << endl;
}
}
// 按条件查询学生信息
void queryStudents(string id = "", string name = "", string className = "") {
cout << "查询结果如下:" << endl;
for (int i = 0; i < students.size(); i++) {
if (id != "" && students[i].id != id) {
continue;
}
if (name != "" && students[i].name != name) {
continue;
}
if (className != "" && students[i].className != className) {
continue;
}
students[i].printInfo();
cout << endl;
}
}
// 显示机房信息
void showRooms() {
cout << "机房信息如下:" << endl;
for (int i = 0; i < rooms.size(); i++) {
rooms[i].showInfo();
cout << endl;
}
}
// 预约机房
void bookRoom(int roomId, string id, int startTime, int endTime) {
// 找到指定编号的机房
int index = -1;
for (int i = 0; i < rooms.size(); i++) {
if (rooms[i].id == roomId) {
index = i;
break;
}
}
if (index == -1) {
cout << "该机房不存在!" << endl;
return;
}
// 检查机房是否有空闲电脑
if (rooms[index].freeComputers <= 0) {
cout << "该机房暂时没有空闲电脑,请选择其他机房!" << endl;
return;
}
// 检查学生是否已经预约了其他机房
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id && students[i].endTime > startTime) {
cout << "该学生已经预约了其他机房,请选择其他时间段!" << endl;
return;
}
}
// 预约机房
rooms[index].freeComputers--;
Student student(id, "", "", startTime);
student.endTime = endTime;
students.push_back(student);
cout << "预约成功!" << endl;
}
};
int main() {
// 初始化机房管理类
ComputerRoomManager manager;
manager.addRoom(ComputerRoom(1, 20));
manager.addRoom(ComputerRoom(2, 30));
// 添加学生信息
manager.addStudent(Student("001", "Tom", "Class1", 1600000000));
manager.addStudent(Student("002", "Jerry", "Class2", 1600005000));
// 显示学生信息
manager.showStudents();
// 按条件查询学生信息
manager.queryStudents("001", "", "");
// 显示机房信息
manager.showRooms();
// 预约机房
manager.bookRoom(1, "001", 1600001000, 1600002000);
return 0;
}
```
以上代码实现了机房收费管理系统的基本功能,包括添加学生、查询学生、预约机房等功能。您可以根据自己的需求进行修改和完善。
阅读全文