题目:排队就餐管理方案设计
【问题描述】
顾客到饭店就餐通常遇到排队等待情况,如果店内有空座,可直接点餐,否则需要排队等待。要求根据顾客的排队情况,及时安排点餐。
【基本要求】
请用C++语言编写程序,模拟顾客排队等待情况。餐桌个数、就餐人数由学生自己设计,存储结构和实现算法由学生自己选定并实现,要求如下。
(1)饭店内餐桌个数为n个;
(2)顾客到达饭店如果有空座可立即坐下点餐,否则需要依次排队等候;
(3)一旦有顾客离去,排在队头的顾客便可开始进店点餐;
(4)如果有VIP顾客,可直接插入队头。
程序包含的基本功能说明如下:
(1)排队:输入排队顾客的编号,加入队列;
(2)就餐:排在队列头的顾客进店点餐,并将其从排队队列中删除;
(3)查看排队:从队头到队尾输出所有等待的顾客编号;
(4)VIP顾客:直接插入对头;
(5)下班:退出运行,提醒顾客营业结束。
【测试数据】
确定餐桌个数(至少8桌),输入排队顾客编号(不少于10人),以较为直观的方式显示出排队等待的顾客编号及就餐顾客编号。
【实现提示】
采用队列算法实现。
运行截图:
代码
#include<iostream>
#include <queue>
using namespace std;
# define MAX_TABLE 8 // 餐厅有多少桌子
//菜单
int Menu()
{
int choice;
do {
cout << endl;
cout << "********************排队就餐管理系统****************" << endl
<< "* 1.排队 *" << endl
<< "* 2.就餐完毕 *" << endl
<< "* 3.查看排队情况 *" << endl
<< "* 4.VIP客户 *" << endl
<< "* 0.下班 *" << endl
<< "****************************************************" << endl;
cout << "输入您的选择: " << endl;
cin >> choice;
} while (choice < 0 || choice > 4);
system("cls");
return choice;
}
int main()
{
// 队列用法
//front():返回 queue 中第一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
//back():返回 queue 中最后一个元素的引用。如果 queue 是常量,就返回一个常引用;如果 queue 为空,返回值是未定义的。
//push(const T& obj):在 queue 的尾部添加一个元素的副本。这是通过调用底层容器的成员函数 push_back() 来完成的。
//push(T&& obj):以移动的方式在 queue 的尾部添加元素。这是通过调用底层容器的具有右值引用参数的成员函数 push_back() 来完成的。
//pop():删除 queue 中的第一个元素。
//size():返回 queue 中元素的个数。
//empty():如果 queue 中没有元素的话,返回 true。
//emplace():用传给 emplace() 的参数调用 T 的构造函数,在 queue 的尾部生成对象。
//swap(queue<T> &other_q):将当前 queue 中的元素和参数 queue 中的元素交换。它们需要包含相同类型的元素。也可以调用全局函数模板 swap() 来完成同样的操作。
queue<int> waiting; // 等待队列
queue<int> waiting1;
queue<int> eating; // 用餐队列
int number = 0;
while (true)
{
switch (Menu())
{
case 0:
cout << "营业结束" << endl;
exit(0);
break;
case 1: // 1.排队
number++;
waiting.push(number); // 入队
cout << "您的用餐号码为: " << number << endl;
if (eating.size() < MAX_TABLE)
{
cout << "当前有空桌, 请直接入座就餐" << endl;
waiting.pop();
eating.push(number);
}
else {
cout << "当前无空桌, 请排队等待" << endl;
}
break;
case 2: // 2.就餐完毕
if (eating.size() != 0)
{
eating.pop();
cout << "有顾客就餐完毕" << endl;
if (waiting.size() != 0) // 等待队列不为空
{
int eat;
eat = waiting.front();
cout << "请" << eat << "号入座就餐" << endl;
waiting.pop();
eating.push(eat);
}
else {
cout << "当前没有顾客排队" << endl;
}
}
else {
cout << "当前没有顾客就餐" << endl;
}
break;
case 3: // 3.查看排队情况
// 队列没有遍历功能, 访问元素的唯一方式是遍历容器内容,并移除访问过的每一个元素。
int temp;
while (!waiting1.empty())
{
waiting1.pop();
}
while (!waiting.empty())
{
temp = waiting.front();
cout << temp << "号 ";
waiting.pop();
waiting1.push(temp);
}
waiting = waiting1;
cout << endl;
break;
case 4: // 4.VIP客户
number++;
while (!waiting.empty()) // 当前队列置空
{
temp = waiting.front();
waiting.pop();
waiting1.push(temp);
}
waiting.push(number); // vip入队
while (!waiting1.empty())
{
temp = waiting1.front();
waiting1.pop();
waiting.push(temp); // 普通用户入队
}
cout << "尊敬的VIP客户, 您的用餐号码为: " << number << endl;
if (eating.size() < MAX_TABLE)
{
cout << "当前有空桌, 请直接入座就餐" << endl;
waiting.pop();
eating.push(number);
}
else {
cout << "当前无空桌, 请排队等待" << endl;
}
break;
}
}
return 0;
}