杂货店排队模拟程序

该模拟程序中包含多个队列,可以使用队列数组来模拟这些队列。假设杂货店共5条收银线,顾客可随机进入支付。顾客会进入最短的队伍,如果队伍一样长,那么选择最靠近的一个队伍。每次交易完成所消耗的时间也是随机的。
完成一些额外工作,扩展杂货店排队程序,使得客户可以:
■如果等待超过了某个时间,可以离开队伍。
■在给定的时间间隔内,检查另一个队伍是否更短。
■如果另一个队伍更短,则切换队伍。
■如果有朋友正在排队,则可以插队。

问题描述

本项目是一个杂货店排队模拟程序,其中包含多个队列,顾客可以随机选择队伍,并具有以下功能:

1.如果等待超过了某个时间,可以离开队伍。

2.在给定的时间间隔内,检查另一个队伍是否更短。

3.如果另一个队伍更短,则切换队伍。

4如果有朋友正在排队,则可以插队

基本要求

1.使用队列数组模拟多个收银线

2.顾客随机选择最短的队伍,如果队伍一样长,选择最靠近的队伍

3.每次交易完成所消耗的时间是随机的

系统功能

1.顾客可以选择加入某一队伍

2.顾客可以等待一定时间后离开队伍

3.顾客可以检查其他队伍的长度

4.顾客可以切换队伍

5.顾客可以插队

方法概述

本程序使用队列数组来模拟多个收银线队列,每个队列中存储顾客结构体,包括顾客ID和朋友ID。程序实现了增加顾客到指定的收银线、添加顾客朋友、查找最短队伍的收银线、将顾客插入其他队伍、打印每条队伍的人数以及排队情况等功能。

流程图

代码实现(各模块)

//头文件

#include <iostream>

#include <queue>

#include <map>

using namespace std;

// 定义顾客结构体

struct Customer {

    int id;

    int friendId;

};

// 建立五条收银线队列

queue<Customer> cashiers[5];

// 增加顾客到指定的收银线

void addCustomer(int cashierIndex, int customerId) {

    Customer newCustomer = { customerId, -1 };

    cashiers[cashierIndex].push(newCustomer);

}                                           

// 添加顾客朋友

void addFriend(int friendId, int targetCashier, int customerId) {

    bool foundCustomer = false;                               

    queue<Customer> tempQueue;                                

    // 在目标队伍中查找顾客

    while (!cashiers[targetCashier].empty()) {

        Customer customerToMove = cashiers[targetCashier].front();

        cashiers[targetCashier].pop();

        if (customerToMove.id == customerId) {

            tempQueue.push(customerToMove);

            Customer newCustomer = { friendId, -1 };

            tempQueue.push(newCustomer);

            while (!cashiers[targetCashier].empty()) {

                tempQueue.push(cashiers[targetCashier].front());

                cashiers[targetCashier].pop();

            }

            while (!tempQueue.empty()) {

                cashiers[targetCashier].push(tempQueue.front());

                tempQueue.pop();

            }

            foundCustomer = true;

            break;

        }

        else {

            tempQueue.push(customerToMove);

        }

    }

    // 如果没有找到目标顾客,输出错误信息

    if (!foundCustomer) {

        cout << "未找到目标顾客" << endl;

    }

}

// 查找最短队伍的收银线

int findShortestQueue() {

    int shortestQueue = 0;

    int minLength = cashiers[0].size();

    for (int i = 1; i < 5; i++) {

        if (cashiers[i].size() < minLength) {

            shortestQueue = i;

            minLength = cashiers[i].size();

        }

    }

    return shortestQueue;

}

// 将顾客插入其他队伍

void insertCustomer(int sourceCashier, int targetCashier, int customerId) {

    if (sourceCashier == targetCashier) {

        cout << "顾客已在目标队伍中" << endl;

        return;

    }

    Customer customerToMove;

    bool foundCustomer = false;

    // 从源队伍中查找顾客

    queue<Customer>& sourceQueue = cashiers[sourceCashier];

    while (!sourceQueue.empty()) {

        customerToMove = sourceQueue.front();

        sourceQueue.pop();

        if (customerToMove.id == customerId) {

            foundCustomer = true;

            break;

        }

    }

    if (!foundCustomer) {

        cout << "未找到顾客" << endl;

        return;

    }

    // 将顾客插入到目标队伍

    cashiers[targetCashier].push(customerToMove);

    cout << "顾客已成功插入到第" << targetCashier + 1 << "条收银线" << endl;

}

// 打印每条队伍的人数

void printQueueStatus() {

    for (int i = 0; i < 5; i++) {

        cout << "第" << i + 1 << "条收银线的人数为:" << cashiers[i].size() << ", 排队情况为:";

        if (cashiers[i].empty()) {

            cout << "队列为空" << endl;

        }

        else {

            queue<Customer> tempQueue = cashiers[i];

            while (!tempQueue.empty()) {

                cout << tempQueue.front().id << " ";

                tempQueue.pop();

            }

            cout << endl;

        }

    }

}

主函数

int main() {

    // 从键盘输入每条线的人数和顾客id

    for (int i = 0; i < 5; i++) {

        int numCustomers;

        cout << "请输入第" << i + 1 << "条收银线的顾客人数:";

        cin >> numCustomers;

        for (int j = 0; j < numCustomers; j++) {

            int customerId;

            cout << "请输入顾客ID:";

            cin >> customerId;

            addCustomer(i, customerId);

        }

    }

    // 显示菜单

    int choice;

    do {

        cout << "菜单:" << endl;

        cout << "1. 增加顾客" << endl;

        cout << "2. 查找最短队伍" << endl;

        cout << "3. 将顾客插入其他队伍" << endl;

        cout << "4. 添加顾客朋友" << endl;

        cout << "5. 打印每条队伍的人数以及排队情况" << endl;

        cout << "0. 退出" << endl;

        cout << "请选择操作:" << endl;

        cin >> choice;

        switch (choice) {

        case 1: {

            int cashierIndex, customerId;

            cout << "请输入收银线编号和顾客ID:";

            cin >> cashierIndex >> customerId;

            addCustomer(cashierIndex - 1, customerId);

            break;

        }

        case 2: {

            int shortestQueue = findShortestQueue();

            cout << "最短队伍为第" << shortestQueue + 1 << "条收银线" << endl;

            break;

        }

        case 3: {

            int sourceCashier, targetCashier, customerId;

            cout << "请输入源收银线、目标收银线和顾客ID:";

            cin >> sourceCashier >> targetCashier >> customerId;

            insertCustomer(sourceCashier - 1, targetCashier - 1, customerId);

            break;

        }

        case 4: {

            int friendId, targetCashier, customerId;

            cout << "请输入朋友ID、目标收银线和顾客ID:";

            cin >> friendId >> targetCashier >> customerId;

            addFriend(friendId, targetCashier - 1, customerId);

            break;

        }

        case 5: {

            printQueueStatus();

            break;

        }

        case 0: {

            cout << "退出程序" << endl;

            break;

        }

        default: {

            cout << "无效操作" << endl;

            break;

        }

        }

    } while (choice != 0);

    return 0;

}

运行结果

  

相关推荐

  1. 程序设计--归并排序

    2024-07-11 22:48:01       27 阅读
  2. 单词分析---模拟排序

    2024-07-11 22:48:01       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 22:48:01       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 22:48:01       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 22:48:01       62 阅读
  4. Python语言-面向对象

    2024-07-11 22:48:01       72 阅读

热门阅读

  1. 前端面试题日常练-day85 【面试题】

    2024-07-11 22:48:01       22 阅读
  2. Vue的学习之class与style绑定

    2024-07-11 22:48:01       23 阅读
  3. day11:01文件处理

    2024-07-11 22:48:01       26 阅读
  4. C语言 会员卡计费系统

    2024-07-11 22:48:01       18 阅读
  5. RKNN3588——利用推理YOLOv8推理图片

    2024-07-11 22:48:01       18 阅读
  6. 如何使用thinkPHP3.2.* 版本开发

    2024-07-11 22:48:01       23 阅读
  7. EasyPOI与Apache POI

    2024-07-11 22:48:01       23 阅读
  8. Go 垃圾回收(GC)

    2024-07-11 22:48:01       24 阅读
  9. ccf认证 202312-3

    2024-07-11 22:48:01       24 阅读
  10. hid-ft260驱动学习笔记 5 - ft260_i2c_probe

    2024-07-11 22:48:01       23 阅读