C++实现银行排队系统:从思路到代码的完整解析
思路分析 为了实现银行排队系统,我们需要设计一个能够处理多个窗口、VIP客户和普通客户的系统。以下是详细的步骤和算法说明: 1. 数据结构设计: - Customer: 表示,包括客户ID、是否为VIP等。 - Window: 表示银行窗口,包括当前正在服务的顾客、等待队列等。 - Bank: 管理所有窗口和客户的整体逻辑。 2. 核心算法: - 客户到达:生成客户ID,如果是VIP客户直接分配到VIP窗口,否则加入普通客户的等待队列。 - 服务窗口:每个窗口轮流从等待队列中取出客户进行服务,并记录服务时间。 - 客户离开:客户完成服务后离开,并可以对服务进行评价。 3. 面向对象设计: - 使用类来封装不同的实体(如Customer, Window, Bank)。 - 使用队列来管理等待的客户。 - 使用优先队列或其他机制来优化VIP客户的服务。 4. 流程图与伪代码: - 描述客户到达、服务窗口、客户离开的流程。 - 提供伪代码以帮助理解算法逻辑。 数据结构与类设计 ```cpp #include <iostream> #include <queue> #include <vector> #include <string> #include <unordered_map> class Customer { public: int id; bool isVIP; std::string serviceFeedback; Customer(int id, bool isVIP) : id(id), isVIP(isVIP), serviceFeedback("") {} }; class Window { public: int windowId; std::queue<Customer*> customerQueue; Customer* currentCustomer; Window(int id) : windowId(id), currentCustomer(nullptr) {} void serveNextCustomer() { if (!customerQueue.empty()) { currentCustomer = customerQueue.front(); customerQueue.pop(); std::cout << "Serving customer " << currentCustomer->id << " at window " << windowId << std::endl; } else { currentCustomer = nullptr; } } void finishService() { if (currentCustomer != nullptr) { std::cout << "Customer " << currentCustomer->id << " finished service at window " << windowId << std::endl; currentCustomer = nullptr; } } }; class Bank { private: std::vector<Window*> windows; std::queue<Customer*> vipQueue; std::queue<Customer*> normalQueue; int nextCustomerId; int vipWindowId; public: Bank(int numWindows, int vipWindowId) : nextCustomerId(1), vipWindowId(vipWindowId) { for (int i = 0; i < numWindows; ++i) { windows.push_back(new Window(i + 1)); } } ~Bank() { for (auto window : windows) { delete window; } } void customerArrives(bool isVIP) { Customer* newCustomer = new Customer(nextCustomerId++, isVIP); if (isVIP && vipWindowId > 0 && vipWindowId <= windows.size()) { windows[vipWindowId - 1]->customerQueue.push(newCustomer); } else { normalQueue.push(newCustomer); } std::cout << "Customer " << newCustomer->id << " arrives" << (isVIP ? " as VIP" : "") << std::endl; } void assignNormalCustomers() { while (!normalQueue.empty()) { for (auto window : windows) { if (window->currentCustomer == nullptr && !normalQueue.empty()) { window->customerQueue.push(normalQueue.front()); normalQueue.pop(); } } } } void process() { for (auto window : windows) { if (window->currentCustomer == nullptr) { window->serveNextCustomer(); } else { window->finishService(); } } } }; ``` 源代码清单及注释 ```cpp #include <iostream> #include <queue> #include <vector> #include <string> #include <unordered_map> // Customer class to represent a customer in the bank system class Customer { public: int id; // Customer ID bool isVIP; // Is the customer a VIP? std::string serviceFeedback; // Service feedback from the customer after leaving the bank // Constructor to initialize a customer with an ID and VIP status Customer(int id, bool isVIP) : id(id), isVIP(isVIP), serviceFeedback("") {} }; // Window class to represent a bank window that serves customers class Window { public: int windowId; // Window ID std::queue<Customer*> customerQueue; // Queue of customers waiting for service at this window Customer* currentCustomer; // Currently serving customer, if any // Constructor to initialize a window with an ID Window(int id) : windowId(id), currentCustomer(nullptr) {} // Method to serve the next customer in the queue void serveNextCustomer() { if (!customerQueue.empty()) { currentCustomer = customerQueue.front(); // Get the next customer from the queue customerQueue.pop(); // Remove the customer from the queue std::cout << "Serving customer " << currentCustomer->id << " at window " << windowId << std::endl; } else { currentCustomer = nullptr; // No customer to serve } } // Method to mark the current customer as finished and clear the current customer slot void finishService() { if (currentCustomer != nullptr) { std::cout << "Customer " << currentCustomer->id << " finished service at window " << windowId << std::endl; currentCustomer = nullptr; // Clear the current customer slot } } }; // Bank class to manage all windows and customers in the bank system class Bank { private: std::vector<Window*> windows; // List of all bank windows std::queue<Customer*> vipQueue; // Queue for VIP customers waiting for service std::queue<Customer*> normalQueue; // Queue for regular customers waiting for service int nextCustomerId; // Next customer ID to be assigned int vipWindowId; // ID of the VIP window, if any public: // Constructor to initialize the bank with a given number of windows and a specific VIP window ID (if any) Bank(int numWindows, int vipWindowId) : nextCustomerId(1), vipWindowId(vipWindowId) { for (int i = 0; i < numWindows; ++i) { windows.push_back(new Window(i + 1)); // Create and add windows to the bank } } // Destructor to clean up allocated memory for windows ~Bank() { for (auto window : windows) { delete window; // Delete each window object } } // Method to handle the arrival of a new customer at the bank void customerArrives(bool isVIP) { Customer* newCustomer = new Customer(nextCustomerId++, isVIP); // Create a new customer with the next available ID and VIP status if (isVIP && vipWindowId > 0 && vipWindowId <= windows.size()) { // If the customer is VIP and there's a designated VIP window windows[vipWindowId - 1]->customerQueue.push(newCustomer); // Add the VIP customer directly to the VIP window's queue } else { // Otherwise, add the customer to the normal queue normalQueue.push(newCustomer); // Add the customer to the normal queue for later assignment to a window } std::cout << "Customer " << newCustomer->id << " arrives" << (isVIP ? " as VIP" : "") << std::endl; // Print arrival message } // Method to assign normal customers to available windows from the normal queue void assignNormalCustomers() { while (!normalQueue.empty()) { // While there are customers in the normal queue for (auto window : windows) { // Iterate through all windows ######[AI写代码神器 | 2048点数解答 | 2024-12-11 15:11:23]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- Java 实现通用文件存储系统:含随机读写与空闲空间回收(字节豆包 | 686点数解答 | 2024-11-21 16:25:51)223
- Golang开发:解析JSON数据时数值默认类型及示例解析(百度文心 | 393点数解答 | 2023-11-09 18:13:06)274
- SQL Working Areas Sizing: Manual vs. Automatic Policy - A Comprehensive Analysis(阿里通义 | 530点数解答 | 2024-05-13 10:55:58)305
- 舞蹈班排队难题:计算婷婷身高排名的C++解法 (字节豆包 | 281点数解答 | 2025-01-14 10:49:34)231
- 舞蹈班排队难题:根据身高算出婷婷前后排队位置(附C++代码)(字节豆包 | 263点数解答 | 2025-01-14 10:49:37)200
- C++ 实现我的世界基岩版:从简单框架到开发要点揭秘(字节豆包 | 182点数解答 | 2025-02-22 15:53:11)193
- C++ 实现完整斗地主:含洗牌、发牌与手牌展示,可按需扩展!(字节豆包 | 1028点数解答 | 2026-01-10 08:02:37)34
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)310
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 ( | 240点数解答 | 2024-05-20 22:11:29)457
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘( | 47点数解答 | 2024-05-20 22:16:48)354
- 深度剖析:黑盒、白盒、单元、集成、系统与验收测试的区别与联系 (百度文心 | 424点数解答 | 2023-11-09 18:24:11)298