小项目分析之C++ 实现模拟银行排队

 
一、问题定义与分析
问题定义 •要解决的问题——银行一天之内的:

   1.总客户数

   2.客户总逗留时间

   3.客户平均逗留时间

问题分析

•新来的人找个短的队伍,站在队尾开始排队 •排在队头的人可以办理业务 •排队等待办业务的客户是在分散的、随机的时间点到来的 •特点:离散事件、要排队 •掌握每个客户到达银行和离开银行这两个时刻 •统计出客户总数 •称客户到达银行和客户离开银行这两个时刻发生的事情为“事件” •整个模拟按事件发生的先后顺序进行处理 •事件驱动模拟 •事件的主要信息是事件类型和事件发生的时刻 •两类事件:客户到达事件和客户离开事件 •事件应存储在有序表里 •有序表按照事件发生的时刻顺序排序 •队列中的客户的主要信息是客户到达的时刻和客户办理业务所需要的时间 •队列数量和银行的窗口数量相同 •每个队列的队头客户就是正在办理业务的客户 •每个队头客户都存在一个将要发生的客户离开事件  
二、类与算法设计
类设计 •有序表选用有序链表,主要操作是插入和删除 •队列,客户排队   •类图:用来表示类以及类和类之间的关系的逻辑视图 •利用类图来记录类的结构,这些类构成了程序的架构
《小项目分析之C++ 实现模拟银行排队》

银行类:

•要有一个表示队列数量的属性 •要有一个打烊时间属性 •要有一个总客户数属性 •要有一个客户总逗留时间属性 •聚合一个或多个队列和一个有序链表

银行类的方法:

•开门营业 •处理客户到达事件 •处理客户离开事件 •帮助客户选择一个最短的队列 •确保按照事件发生的时间顺序处理事件   银行类类图:  
《小项目分析之C++ 实现模拟银行排队》

队列:

•使用STL中的queue •queue的节点:
《小项目分析之C++ 实现模拟银行排队》

有序链表:

•使用STL中的list •list的节点:
《小项目分析之C++ 实现模拟银行排队》  
算法设计

void Bank::Simulation()算法:

1.开门营业,OpenForDay()

2.如果有事件发生,那么:

  (1) 对于到达事件,处理到达事件,CustomerArrived(Event *event)

  (2) 对于离开事件,处理离开事件,CustomerDeparture(Event *event)

3.重复第2步

4.输出统计结果

void Bank::OpenForDay()算法:

   1.初始化_queue_number为某个正整数

   2.初始化_close_time为以秒为单位的时间,比如8*3600,表示8个小时

   3.初始化_total_time为0

   4.初始化_customer_number为0

   5.设定第一个客户到达事件,客户到达时刻为0

   6.队列和有序链表的初始化(这是由C++STL类自己完成的)

void CustomerArrived(Event *event)算法:

  1.产生随机数:客户办理业务需要的时间,假设一个客户最多需要30分钟

  2.产生随机数:下一个客户到达的时间间隔,假设最多10分钟来一个客户

  3.下一个客户到达时刻是当前事件发生时刻和时间间隔的和

  4.如果到达时刻银行没有下班,产生一个新的客户到达事件插入事件有序链表

  5.给链表按事件的发生时刻排序(因为STL中没有有序链表)

  6.找一个最短的队列

  7.在最短的队列中插入新到的客户

  8.如果队列中有且只有一个客户,生成该客户的一个离开事件插入到事件表

 这种情况下,离开事件发生时刻=到达时刻+办理业务需要的时间

  9.统计客户数量

void CustomerDeparture(Event *event)算法:

  1.计算该客户在银行中的逗留时间,并且累加总逗留时间

      客户在银行中的逗留时间=客户离开事件发生时刻-客户到达时刻

  2.从队列中删除该客户

  3.如果队列不空则设定一个新的队头客户离开事件

    队头离开事件发生时刻=上个离开事件发生时刻(队头开始办业务的时刻)+队头办业务需要时间

 
三、C++程序编写  附注:
Lambda表达式简介 •只有当可以应用一个()调用操作符符到一个对象时,这个对象才是可调用对象 •函数:可调用对象 •函数指针:可调用对象 •重载了()调用操作符的类:可调用对象 •Lambda表达式:可调用对象 •一个Lambda表达式表示了一个可调用的代码单元 •可以认为是一个无名的、内联的函数 •可以定义在函数内部 •有一个返回类型、一个参数列表和一个函数体

[capture list](parameter list)->return type {function body}

[capture list](parameter list)->return type {function body}

•capture list:Lambda表达式所在的函数的局部变量 •把局部变量写在capture list里可以在Lambda表达式内部使用它们 •return type:返回类型(尾置返回指定方式) •parameter list:参数列表 •function body:函数体

举例:

_event_list.sort(

      [](const Event &e1, const Event &e2) -> bool

      {return e1._occur_time < e2._occur_time;});

定义头文件

•头文件中通常包含类的定义、常量的定义和常量表达式的定义 •习惯:头文件的名字通常和其中定义的类的名字相同 •头文件可以用#include包含其它头文件 •#include是预处理器提供的功能 •为了让头文件可以被安全地包含多次,就需要使用预处理器 •预处理器不是C++语言的一部分 •可以用#define定义一个预处理变量 •预处理器变量有两种状态:己定义和未定义 •#ifdef:如果预处理变量己定义,返回true •#ifndef:如果预处理量未定义,返回true •只有测试为true时,才继续预处理直到#endif •头文件保护符,以bank.h为例 :

  #ifndef __BANK_H__

  #define __BANK_H__

  // #include<其它头文件>

  // const、constexpr and class 定义

  // extern 多文件共享变量声明

  #endif

 

<——以下为实现代码——>

 
《小项目分析之C++ 实现模拟银行排队》 bank.cc

#include "stdafx.h"
#include "bank.h"
#include <iostream>
#include <clocale>
#include <chrono>
#include <cstdlib>

/*
#include <algorithm>
std::sort(_work_queue, _work_queue + _queue_number, 
      [](const std::queue<QueueNode> &q1, 
        const std::queue<QueueNode> &q2) -> bool
      {return q1.size() < q2.size();});
*/

Bank::Bank(int window, int close_time)
:_queue_number(window), _close_time(close_time),
  _total_time(0), _customer_number(0)
{
  _work_queue = new std::queue<QueueNode>[window];

  srand(std::chrono::system_clock
      ::to_time_t(std::chrono::system_clock::now()));
}

Bank::~Bank()
{
  delete[] _work_queue;
}

void Bank::OpenForDay()
{
  // 第一个客户到达
  _event_list.push_back({0, 0});
}

// 客户到达事件
// 客户到达时,有三件事需要做:
// 1:为此时刻到达的客户随机产生一个办理事务需要时间
// 2:随机产生下一客户到达的时间间隔
// 3:把到达的客户放入一个最短的工作队列
void Bank::CustomerArrived(Event *event)
{
  ++_customer_number;

  int duration_time, inter_time;

  // 此时刻到达客户办理事务需要时间
  duration_time = rand() % 1800 + 1; //一个客户最多要30分钟
  // 下一个客户在从event->_occur_time+inter_time时刻到来
  inter_time = rand() % 600 + 1; // 最多10分钟来一个客户

  // 下一个客户到达时间
  int t = event->_occur_time + inter_time;

  // 银行还没有关门
  if(t < _close_time) {
    _event_list.push_back({t, 0});
    // 按到达时间排序事件表,早前晚后
    SortEventList();
  }
  // 选一个最短队列排队
  int i;
  i = FindShortestQueue();
  
  _work_queue[i].push({event->_occur_time, duration_time});

  if(_work_queue[i].size() == 1) {
  // 这个i队列第一个客户,生成他的离开事件
    _event_list.push_back(
        {event->_occur_time + duration_time, i + 1});
    SortEventList();
  }
}

void Bank::CustomerDeparture(Event *event)
{
   int i = event->_type - 1;

   QueueNode customer;

   // 客户事务处理完毕,离开
   customer = _work_queue[i].front();
   _work_queue[i].pop();

   _total_time 
     += event->_occur_time - customer._arrival_time;

   // 第i个队列的一个离开事件
   if(!_work_queue[i].empty()) {
     customer = _work_queue[i].front();
    _event_list.push_back(
        {customer._duration_time + event->_occur_time, i + 1});
    SortEventList();
   }
}

int Bank::FindShortestQueue()
{
  int result = 0;
  for(int i = 0; i < _queue_number; ++i) {
    if(_work_queue[result].size() > _work_queue[i].size())
      result = i;
  }
  return result;
}

void Bank::SortEventList()
{
  // 方法一,Lambda表达式:
  _event_list.sort(
      [](const Event &e1, const Event &e2) -> bool
      {return e1._occur_time < e2._occur_time;});

  // 方法二:
  // 你知道怎么写一个函数来比较两个event吗?
  // 其实就是把Lambda表达式写成一个函数,把
  // 这个函数作为sort的参数就可以了。

  // 方法三,使用 struct Event::operator< :
  _event_list.sort();

  // 注意:上面的方法一和方法二可以注释掉任何一个,
  // 写两个,只是为了演示。
}

void Bank::Simulation()
{
  OpenForDay();
  Event event;
  while(!_event_list.empty()) {
    event = _event_list.front();
    _event_list.pop_front();
    if(event._type == 0) // 到达事件
      CustomerArrived(&event);
    else
      CustomerDeparture(&event);
  }
  // 计算并输出平均逗留时间
  std::wcout << L"客户数:" << _customer_number << std::endl
    << L"总逗留时间(小时):" << (double)_total_time / 3600.0 
    << std::endl
    << L"平均逗留时间(分钟):" 
    << (double)_total_time / (double)(_customer_number * 60)
    << std::endl;
}

int wmain(int argc, wchar_t *argv[], wchar_t *env[])
{
  _wsetlocale(LC_ALL, L"");

  Bank bank;
  bank.Simulation();
  return 0;
}

stdafx.cpp

// stdafx.cpp : source file that includes just the standard includes
// bank.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

头文件:

bank.h

#ifndef __BANK_H__
#define __BANK_H__

#include <queue>
#include <list>

struct Event
{
	int _occur_time; // 事件发生的时刻
	int _type; // 事件类型,0表示到达事件,1到
	                 // 4 表示四个窗口的离开事件
  bool operator<(const Event &rhs)
  {
    return _occur_time < rhs._occur_time;
  }
};

struct QueueNode
{
	int _arrival_time; // 客户到达时间
	int _duration_time;// 客户需要的服务员时间
};

class Bank
{
	public:
		explicit Bank(int window_number = 4, 
				      int close_time = 8 * 3600);
		~Bank();
		void Simulation();
	private:
		int _queue_number;                 // 队列个数
		int _close_time;                   // 关门时间
		int _total_time;                   // 累计客户逗留时间
		int _customer_number;              // 客户总数

		std::list<Event>      _event_list; // 事件链表
		std::queue<QueueNode> *_work_queue;// 工作队列

		void OpenForDay();
		void CustomerArrived(Event *event);
		void CustomerDeparture(Event *event);
    int FindShortestQueue();
    void SortEventList();
};

#endif

stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here

targetver.h

#pragma once

// Including SDKDDKVer.h defines the highest available Windows platform.

// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.

#include <SDKDDKVer.h>

运行结果:

《小项目分析之C++ 实现模拟银行排队》  

  

  

  

  

    原文作者:#小金乌会发光&
    原文地址: https://www.cnblogs.com/carsonzhu/p/5528901.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞