运用队列实现银行叫号系统。
银行叫号系统源代码
#include<iostream>
using namespace std;
struct Customers {
int m_id=1;
};
class Bank {
public:
Bank(int queueCapacity);//创建队列
bool QueueEmpty();//判空队列
bool QueueFull();//判满函数
int QueueLength();//队列长度
bool EnQueue(Customers customer);//新元素入队
bool DeQueue(Customers& customer);//首元素出队
void QueueTraverse();//遍历队列
Customers* m_pQueue;//队列数组指针
int m_QueueLength;//队列元素个数
int m_QueueCapacity;//队列数组容量
int m_Head;//队头
int m_Tail;//队尾
};
Bank::Bank(int queueCapacity) {
m_QueueCapacity = queueCapacity;
m_Head = 0;
m_Tail = 0;
m_QueueLength = 0;
m_pQueue = new Customers[m_QueueCapacity];
}
bool Bank::QueueEmpty() {
return m_QueueLength == 0 ? true : false;
}
bool Bank::QueueFull() {
return m_QueueLength == m_QueueCapacity ? true : false;
}
int Bank::QueueLength() {
return m_QueueLength;
}
bool Bank::EnQueue(Customers customer) {
if (QueueFull())
return false;
else {
m_pQueue[m_Tail] = customer;
m_Tail++;
m_Tail = m_Tail % m_QueueCapacity;
m_QueueLength++;
return true;
}
}
bool Bank::DeQueue(Customers& customer) {
if (QueueEmpty())
return false;
else {
customer = m_pQueue[m_Head];
m_Head++;
m_Head = m_Head % m_QueueCapacity;
m_QueueLength--;
return true;
}
}
void Bank::QueueTraverse() {
for (int i = m_Head; i < m_QueueLength; i++) {
cout << m_pQueue[i % m_QueueCapacity].m_id << endl;
}
}
int main() {
Bank bank(10);
Customers customer;
int count = 1;
cout << "欢迎使用银行叫号系统" << endl;
cout << "1.有新客户到达,取号" << endl;
cout << "2.有窗口空闲,叫号" << endl;
cout << "3.退出系统" << endl;
int number;
while (true) {
cin >> number;
if (number == 1) {
if (bank.EnQueue(customer)) {
cout << "您的id是" << count << ",前面还有" << bank.QueueLength() - 1 << "人在排队!" << endl;
count++;
}
else {
cout << "取号失败!" << endl;
}
}
if (number == 2) {
if (bank.DeQueue(customer)) {
count--;
cout << "请第" << bank.m_Head << "号客户到窗口办理!" << endl;
}
else {
cout << "队列中已没有客户!" << endl;
}
}
if (number == 3)
exit(0);
}
return 0;
}