for(inti=1;i<=tellerNum;i++)
{
if(tellers.finishServiceTime
if(tellers.finishServiceTime==curTime&&!customer.IsEmpty())
{
intt=NextService();
#ifdefPRINTPROCESS
cout<<'\t'<
#endif
CustomerDeparture();
tellers.totalCustomerCount++;
tellers.totalServiceTime+=t;
tellers.finishServiceTime+=t;
}
#ifdefPRINTPROCESS
elsecout<<"\t";
#endif
}
#ifdefPRINTPROCESS
cout<
#endif
}
PrintResult();
}
voidPtintSimuPara()
{
cout<
cout<<"柜台数量:"<
cout<<"两个顾客来到的最小间隔时间:"<
cout<<"两个顾客来到的最大间隔时间:"<
cout<<"柜台服务最短时间:"<
cout<<"柜台服务最长时间:"<
}
voidPrintResult()
{
inttSN=0;
longtST=0;
cout<
cout<<"----模拟结果------";
cout<
for(inti=1;i<=tellerNum;i++)
{
cout<<"TELLER"<
cout<<'\t'<
cout<<'\t'<
cout<<'\t';
if(tellers.totalCustomerCount)
cout<<(float)tellers.totalServiceTime/(float)tellers.totalCustomerCount;
elsecout<<0;
cout<<""<
}
cout<<"TOTAL\t"<
if(tSN)cout<<(float)tST/(float)tSN;elsecout<<0;
cout<<""<
cout<<"CustomerNumber:\t"<
cout<<"CustomerWaitTime:\t"<
if(tSN)cout<<(float)customerTime/(float)tSN;elsecout<<0;
cout<
}
private:
inttellerNum;
intsimuTime;
intcurTime,nextTime;
intcustomerNum;
longcustomerTime;
intarrivalLow,arrivalHigh,arrivalRange;
intserviceLow,serviceHigh,serviceRange;
Tellertellers[21];
Queuecustomer;
voidNextArrived()
{
nextTime+=arrivalLow+rand()%arrivalRange;
}
intNextService()
{
returnserviceLow+rand()%serviceRange;
}
voidCustomerArrived()
{
customerNum++;
customer.EnQueue(nextTime);
}
voidCustomerDeparture()
{
customerTime+=(long)curTime-(long)customer.DeQueue();
}
};
#endif
几点说明
lRun()的过程是这样的:curTime是时钟,从开始营业计时,自然流逝到停止营业。当顾客到的事件发生时(顾客到时间等于当前时间,小于判定是因为个别时候顾客同时到达——输入arrivalLow=0的情况,而在同一时间,只给一个顾客发号码),给这个顾客发号码(用顾客到时间标示这个顾客,入队,来到顾客数增1)。当柜台服务完毕时(柜台服务完时间等于当前时间),该柜台服务人数增1,服务时间累加,顾客离开事件发生,下一个顾客到该柜台。因为柜台开始都是空闲的,所以实际代码和这个有点出入。最后,停止营业的时候,停止发号码,还在接受服务的顾客继续到服务完,其他还在排队的就散伙了。
l模拟结果分别是:各个柜台的服务人数、服务时间、平均服务时间,总的服务人数、服务时间、平均服务时间,来的顾客总数、没被服务的数目(来的太晚了)、接受服务顾客总等待时间、平均等待时间。
l这个算法效率是比较低的,实际上可以不用队列完成这个模拟(用顾客到时间推动当前时钟,柜台直接公告服务完成时间),但这样就和实际情况有很大差别了——出纳员没等看见人就知道什么时候完?虽然结果是一样的,但是理解起来很莫名其妙,尤其是作为教学目的讲解的时候。当然了,实际中为了提高模拟效率,本文的这个算法是不值得提倡的。
l注释掉的#definePRINTPROCESS,去掉注释符后,在运行模拟的时候,能打印出每个时刻柜台的服务情况(第几个顾客,顾客到达时间,接受服务时间),但只限4个柜台以下,多了的话屏幕就满了(格式就乱了)。
【后记】本来我没打算写这篇,后来,当我开始实现模拟的时候,竟欲罢不能了。这是数据结构这本书中第一个实际应用的例子,而且也有现实意义。你可以看出各个柜台在不同的业务密度下的工作强度(要么给哪个柜台出纳员发奖金,要么轮换柜台),各种情况下顾客的等待时间(人都是轮到自己就不着急了),还有各种情况下设立几个柜台合理(很少的空闲时间,很短的等待时间,几乎为零的未服务人数)。例如这样:
for(inti=1;i<16;i++)
{
Simulationa(i,240,1,4,8,15);
a.Run();
}
你模拟一下就会得出,在不太繁忙的银行,4~5个柜台是合适的——现在的银行大部分都是这样的。