《數(shù)據(jù)結(jié)構(gòu)(C++)》學(xué)習(xí)輔導(dǎo)系列:隊(duì)列應(yīng)用(1)

字號:

我看的兩本教科書(《數(shù)據(jù)結(jié)構(gòu)(c語言版)》還有這本黃皮書)都是以這個講解隊(duì)列應(yīng)用的,而且都是銀行營業(yè)模擬(太沒新意了)。細(xì)比較,這兩本書模擬的銀行營業(yè)的方式還是不同的。1997版的《數(shù)據(jù)結(jié)構(gòu)(c語言版)》的銀行還是老式的營業(yè)模式(畢竟是1997年的事了),現(xiàn)在的很多地方還是這種營業(yè)模式 ——幾個窗口同時排隊(duì)。這種方式其實(shí)不太合理,經(jīng)常會出現(xiàn)先來的還沒有后來的先辦理業(yè)務(wù)(常常前面一個人磨磨蹭蹭,別的隊(duì)越來越短,讓你恨不得把前面那人干掉)。1999版的這本黃皮書的銀行改成了一種掛牌的營業(yè)方式,每個來到的顧客發(fā)一個號碼,如果哪個柜臺空閑了,就叫號碼最靠前的顧客來辦理業(yè)務(wù);如果同時幾個柜臺空閑,就按照一種法則來決定這幾個柜臺叫號的順序(最簡單的是按柜臺號碼順序)。這樣,就能保證顧客按照先來后到的順序接受服務(wù)——因?yàn)榇蠹遗旁谝粋€隊(duì)里。這樣的營業(yè)模式我在北京的西直門工商銀行見過,應(yīng)該說這是比較合理的一種營業(yè)模式。不過,在本文中最重要的是,這樣的營業(yè)模式比較好模擬(一個隊(duì)列總比n個隊(duì)列好操作)。
    原書的這部分太難看了,我看的暈暈的,我也不知道按照原書的方法能不能做出來,因?yàn)槲覜]看懂(旁白:靠,你小子這樣還來現(xiàn)眼)。我按照實(shí)際情況模擬,實(shí)現(xià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 << "輸入模擬參數(shù)" << endl;
     cout << "柜臺數(shù)量:"; cin >> tellernum;
     cout << "營業(yè)時間:"; cin >> simutime;
     cout << "兩個顧客來到的最小間隔時間:"; cin >> arrivallow;
     cout << "兩個顧客來到的間隔時間:"; cin >> arrivalhigh;
     cout << "柜臺服務(wù)最短時間:"; cin >> servicelow;
     cout << "柜臺服務(wù)最長時間:"; 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;