void PrintResult()
{
int tSN = 0;
long tST = 0;
cout << endl;
cout << "-------------模擬結(jié)果------------------";
cout << endl << "tellerID\tServiceNum\tServiceTime\tAverageTime" << endl;
for (int i = 1; i <= tellerNum; i++)
{
cout << "TELLER " << i;
cout << '\t' << tellers[i].totalCustomerCount << " "; tSN += ellers[i].totalCustomerCount;
cout << '\t' << tellers[i].totalServiceTime << " "; tST += long)tellers[i].totalServiceTime;
cout << '\t';
if (tellers[i].totalCustomerCount)
cout << (float)tellers[i].totalServiceTime/(float)tellers[i].totalCustomerCount;
else cout << 0;
cout << " " << endl;
}
cout << "TOTAL \t" << tSN << " \t" << tST << " \t";
if (tSN) cout << (float)tST/(float)tSN; else cout << 0;
cout << " " << endl;
cout << "Customer Number:\t" << customerNum << "\tno Service:\t" << customerNum - tSN << endl;
cout << "Customer WaitTime:\t" << customerTime << "\tAvgWaitTime:\t";
if (tSN) cout << (float)customerTime/(float)tSN; else cout << 0;
cout << endl;
}
private:
int tellerNum;
int simuTime;
int curTime, nextTime;
int customerNum;
long customerTime;
int arrivalLow, arrivalHigh, arrivalRange;
int serviceLow, serviceHigh, serviceRange;
Teller tellers[21];
Queue
customer;
void NextArrived()
{
nextTime += arrivalLow + rand() % arrivalRange;
}
int NextService()
{
return serviceLow + rand() % serviceRange;
}
void CustomerArrived()
{
customerNum++;
customer.EnQueue(nextTime);
}
void CustomerDeparture()
{
customerTime += (long)curTime - (long)customer.DeQueue();
}
};
#endif
幾點說明
Run()的過程是這樣的:curTime是時鐘,從開始營業(yè)計時,自然流逝到停止?fàn)I業(yè)。當(dāng)顧客到事件發(fā)生時(顧客到時間等于當(dāng)前時間,小于判定是因為個別時候顧客同時到達(dá)——輸入arrivalLow=0的情況,而在同一時間,只給一個顧客發(fā)號碼),給這個顧客發(fā)號碼(用顧客到時間標(biāo)示這個顧客,入隊,來到顧客數(shù)增1)。當(dāng)柜臺服務(wù)完畢時(柜臺服務(wù)完時間等于當(dāng)前時間),該柜臺服務(wù)人數(shù)增1,服務(wù)時間累加,顧客離開事件發(fā)生,下一個顧客到該柜臺。因為柜臺開始都是空閑的,所以實際代碼和這個有點出入。最后,停止?fàn)I業(yè)的時候,停止發(fā)號碼,還在接受服務(wù)的顧客繼續(xù)到服務(wù)完,其他還在排隊的就散伙了。
模擬結(jié)果分別是:各個柜臺的服務(wù)人數(shù)、服務(wù)時間、平均服務(wù)時間,總的服務(wù)人數(shù)、服務(wù)時間、平均服務(wù)時間,來的顧客總數(shù)、沒被服務(wù)的數(shù)目(來的太晚了)、接受服務(wù)顧客總等待時間、平均等待時間。
這個算法效率是比較低的,實際上可以不用隊列完成這個模擬(用顧客到時間推動當(dāng)前時鐘,柜臺直接公告服務(wù)完成時間),但這樣就和實際情況有很大差別了——出納員沒等看見人就知道什么時候完?雖然結(jié)果是一樣的,但是理解起來很莫名其妙,尤其是作為教學(xué)目的講解的時候。當(dāng)然了,實際中為了提高模擬效率,本文的這個算法是不值得提倡的。
注釋掉的#define PRINTPROCESS,去掉注釋符后,在運行模擬的時候,能打印出每個時刻柜臺的服務(wù)情況(第幾個顧客,顧客到達(dá)時間,接受服務(wù)時間),但只限4個柜臺以下,多了的話屏幕就滿了(格式就亂了)。
【后記】本來我沒打算寫這篇,后來,當(dāng)我開始實現(xiàn)模擬的時候,竟欲罷不能了。這是數(shù)據(jù)結(jié)構(gòu)這本書中第一個實際應(yīng)用的例子,而且也有現(xiàn)實意義。你可以看出各個柜臺在不同的業(yè)務(wù)密度下的工作強度(要么給哪個柜臺出納員發(fā)獎金,要么輪換柜臺),各種情況下顧客的等待時間(人都是輪到自己就不著急了),還有各種情況下設(shè)立幾個柜臺合理(很少的空閑時間,很短的等待時間,幾乎為零的未服務(wù)人數(shù))。例如這樣:
for (int i = 1; i < 16; i++)
{
Simulation a(i,240,1,4,8,15);
a.Run();
}
你模擬一下就會得出,在不太繁忙的銀行,4~5個柜臺是合適的——現(xiàn)在的銀行大部分都是這樣的。