增加一个添加窗口功能,然后把完整的代码写出来
时间: 2024-02-27 14:52:25 浏览: 32
好的,增加添加窗口功能需要在程序中加入一个窗口管理类,然后在主函数中调用该类的添加窗口函数。下面是增加添加窗口功能后的完整代码:
```c++
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
struct Customer {
string name;
int num;
};
class Window {
public:
Window(int id) {
this->id = id;
this->busy = false;
}
int get_id() { return this->id; }
bool is_busy() { return this->busy; }
void serve(Customer c) {
this->busy = true;
cout << "窗口" << this->id << "正在为" << c.name << "服务,请等候..." << endl;
}
void release() {
this->busy = false;
cout << "窗口" << this->id << "服务完成!" << endl;
}
private:
int id;
bool busy;
};
class QueueSystem {
public:
QueueSystem() {}
void enqueue() {
Customer c;
cout << "请输入姓名和编号:" << endl;
cin >> c.name >> c.num;
q.push(c);
cout << "已加入队列!" << endl;
}
void dequeue() {
if (q.empty()) {
cout << "队列为空!" << endl;
return;
}
Customer c = q.front();
q.pop();
for (int i = 0; i < windows.size(); i++) {
if (!windows[i].is_busy()) {
windows[i].serve(c);
return;
}
}
waitings.push_back(c);
cout << "所有窗口忙,已加入等待队列,当前等待人数为" << waitings.size() << "人!" << endl;
}
void display() {
if (q.empty()) {
cout << "队列为空!" << endl;
return;
}
cout << "当前排队情况:" << endl;
queue<Customer> tmp = q;
while (!tmp.empty()) {
Customer c = tmp.front();
tmp.pop();
cout << c.name << " " << c.num << endl;
}
}
void del() {
if (q.empty()) {
cout << "队列为空!" << endl;
return;
}
cout << "请输入要删除的队列编号:" << endl;
int num;
cin >> num;
queue<Customer> tmp;
while (!q.empty()) {
Customer c = q.front();
q.pop();
if (c.num != num) {
tmp.push(c);
} else {
cout << "已删除" << c.name << "!" << endl;
}
}
q = tmp;
}
void add_window() {
int id = windows.size() + 1;
Window w(id);
windows.push_back(w);
cout << "已添加窗口" << id << "!" << endl;
}
void serve() {
for (int i = 0; i < windows.size(); i++) {
if (windows[i].is_busy()) {
windows[i].release();
if (!waitings.empty()) {
Customer c = waitings[0];
waitings.erase(waitings.begin());
windows[i].serve(c);
}
}
}
}
private:
queue<Customer> q;
vector<Window> windows;
vector<Customer> waitings;
};
int main() {
QueueSystem qs;
while (true) {
cout << "请选择操作(1-排队,2-叫号,3-查看队列,4-删除队列,5-添加窗口,0-退出):" << endl;
int op;
cin >> op;
switch (op) {
case 1:
qs.enqueue();
break;
case 2:
qs.dequeue();
break;
case 3:
qs.display();
break;
case 4:
qs.del();
break;
case 5:
qs.add_window();
break;
case 0:
return 0;
default:
cout << "无效操作!" << endl;
break;
}
qs.serve();
}
return 0;
}
```
在这个程序中,我们加入了一个窗口管理类`Window`,里面包括了窗口编号、是否忙碌和服务函数等属性和方法。另外,我们还加入了一个队列系统类`QueueSystem`,里面包括了排队、叫号、查看队列、删除队列、添加窗口和服务等功能。最后,在主函数中,我们通过调用队列系统类的相关函数来实现整个排队窗口管理系统。