数据结构用c++写窗口排队管理系统
时间: 2023-08-04 08:11:34 浏览: 107
C++实现银行排队系统
好的,下面是一个简单的窗口排队管理系统的示例代码,用C++实现。这个示例程序包含了一个窗口类和一个排队管理类,可以让用户在窗口中进行排队和办理业务。
```
#include <iostream>
#include <queue>
#include <string>
using namespace std;
// 定义窗口类
class Window {
public:
Window(int id) : m_id(id), m_busy(false), m_customer("") {}
int getId() const { return m_id; }
bool isBusy() const { return m_busy; }
void serveCustomer(const string& customer) {
m_busy = true;
m_customer = customer;
cout << "Window " << m_id << " serving customer: " << m_customer << endl;
}
void releaseCustomer() {
m_busy = false;
m_customer = "";
cout << "Window " << m_id << " released customer" << endl;
}
private:
int m_id;
bool m_busy;
string m_customer;
};
// 定义排队管理类
class QueueManager {
public:
void addCustomer(const string& customer) {
m_customerQueue.push(customer);
cout << "Added customer: " << customer << endl;
}
void serveCustomer(Window& window) {
if (!m_customerQueue.empty()) {
string customer = m_customerQueue.front();
m_customerQueue.pop();
window.serveCustomer(customer);
}
}
private:
queue<string> m_customerQueue;
};
int main() {
// 初始化窗口和排队管理对象
Window windows[3] = { Window(1), Window(2), Window(3) };
QueueManager queueManager;
// 循环处理用户请求
while (true) {
cout << "Enter command (add <customer name>, serve <window number>, exit): ";
string command;
getline(cin, command);
if (command.substr(0, 3) == "add") {
// 添加新的顾客到队列中
string customer = command.substr(4);
queueManager.addCustomer(customer);
}
else if (command.substr(0, 5) == "serve") {
// 窗口服务顾客
int windowNumber = stoi(command.substr(6)) - 1;
if (windowNumber >= 0 && windowNumber < 3 && !windows[windowNumber].isBusy()) {
queueManager.serveCustomer(windows[windowNumber]);
windows[windowNumber].releaseCustomer();
}
else {
cout << "Invalid window number or window is busy" << endl;
}
}
else if (command == "exit") {
// 退出程序
break;
}
else {
// 非法命令
cout << "Invalid command" << endl;
}
}
return 0;
}
```
在这个示例程序中,窗口类用来表示一个窗口,包含窗口编号、是否忙碌和正在服务的顾客名字等信息。排队管理类用来管理顾客的排队,包含一个顾客队列。在程序中,用户可以通过输入命令来添加新的顾客到队列中或让某个窗口服务顾客。当窗口服务完一个顾客后,它将会释放该顾客并再次变为空闲状态。如果用户希望退出程序,则可以输入 "exit" 命令。
阅读全文