《数据结构(C++)》学习辅导系列:队列应用(1)
我看的两本教科书(《数据结构(c语言版)》还有这本黄皮书)都是以这个讲解队列应用的,而且都是银行营业模拟(太没新意了)。细比较,这两本书模拟的银行营业的方式还是不同的。1997版的《数据结构(c语言版)》的银行还是老式的营业模式(毕竟是1997年的事了),现在的很多地方还是这种营业模式 ——几个窗口同时排队。这种方式其实不太合理,经常会出现先来的还没有后来的先办理业务(常常前面一个人磨磨蹭蹭,别的队越来越短,让你恨不得把前面那人干掉)。1999版的这本黄皮书的银行改成了一种挂牌的营业方式,每个来到的顾客发一个号码,如果哪个柜台空闲了,就叫号码最靠前的顾客来办理业务;如果同时几个柜台空闲,就按照一种法则来决定这几个柜台叫号的顺序(最简单的是按柜台号码顺序)。这样,就能保证顾客按照先来后到的顺序接受服务——因为大家排在一个队里。这样的营业模式我在北京的西直门工商银行见过,应该说这是比较合理的一种营业模式。不过,在本文中最重要的是,这样的营业模式比较好模拟(一个队列总比n个队列好操作)。
原书的这部分太难看了,我看的晕晕的,我也不知道按照原书的方法能不能做出来,因为我没看懂(旁白:靠,你小子这样还来现眼)。我按照实际情况模拟,实现如下:
#ifndef simulation_h
#define simulation_h
#include
#include
#include
class teller
{
public:
int totalcustomercount;
int totalservicetime;
int finishservicetime;
teller() :totalcustomercount(0), totalservicetime(0),
finishservicetime(0) {}
};
//#define printprocess
class simulation
{
public:
simulation()
{
cout << endl << "输入模拟参数" << endl;
cout << "柜台数量:"; cin >> tellernum;
cout << "营业时间:"; cin >> simutime;
cout << "两个顾客来到的最小间隔时间:"; cin >> arrivallow;
cout << "两个顾客来到的间隔时间:"; cin >> arrivalhigh;
cout << "柜台服务最短时间:"; cin >> servicelow;
cout << "柜台服务最长时间:"; cin >> servicehigh;
arrivalrange = arrivalhigh - arrivallow + 1;
servicerange = servicehigh - servicelow + 1;
srand((unsigned)time(null));
}
simulation(int tellernum, int simutime, int arrivallow, int arrivalhigh, int servicelow, int servicehigh)
: tellernum(tellernum), simutime(simutime), arrivallow(arrivallow), arrivalhigh(arrivalhigh),
servicelow(servicelow), servicehigh(servicehigh),
arrivalrange(arrivalhigh - arrivallow + 1), servicerange(servicehigh - servicelow + 1)
{ srand((unsigned)time(null)); }
void initialize()
{
curtime = nexttime = 0;
customernum = customertime = 0;
for (int i = 1; i <= tellernum; i++)
{
tellers[i].totalcustomercount = 0;
tellers[i].totalservicetime = 0;
tellers[i].finishservicetime = 0;
}
customer.makeempty();
}
void run()
{
initialize();
nextarrived();
#ifdef printprocess
cout << endl;
0条评论