数据结构——离散事件模拟

来源:互联网 发布:linux ftp登录 编辑:程序博客网 时间:2024/06/05 15:44

- 算法:

假设某银行有4个窗口对外接待客户,从早晨银行开门起不断有客户进入银行。由于每个窗口在某个时刻只能接待一个客户,因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进入银行的客户,如果某个窗口的业务员正空闲,则可上前输业务;反之,若4个窗口均有客户所占,他便会排在人数最少的队伍后面。编制一个程序模拟银行的这种业务活动并计算一天中客户在银行的平均逗留时间。

typedef struct{    int OccurTime;//事件发生时刻    int NType;//事件类型,0表示到达事件,14表示四个窗口离开事件}Event,ElemType;//事件类型,有序链表LinkList的数据元素类型typedef LinkList EventList//事件链表类型,定义为有序链表typedef struct{    int ArrivalTime;//到达时刻    int Duration;//办理事务所需时间}QElemType;//队列的数据元素类型EventList ev;                                //事件链表Event en;                                    //事件结点LinkQueue q[5];                                //客户队列QElemType customer;                             //客户记录int TotalTime,CustomerNum;                    //累计逗留时间 ,累计客户数量int cmp(Event a, Event b);//依事件a的发生时刻<或=或>事件b的发生时刻分别返回-101void OpenDay(){    TotalTime=0;    CustomerNum=0;    InitList(ev);                            //初始化事件链表    en.OccurTime=0;                            //设置第一个客户到达事件    en.NType=0;    //插入事件    OrderInsert(ev,en,cmp);        //初始化任务队列    for(i=1;i<5;i++){        InitQueue(q[i]);    }    }//客户到达void CustomerArrived(){    ++CustomerNum;                                //增加客户数量    Random(durtime,intertime);      t=en.OccurTime+intertime;    if(t<CloseTime)        OrderInsert(ev,(t,0),cmp);       i=Minimum(q);    Enqueue(q[i],(en.OccurTime,durtime));    if(QueueLength(q[i])==1)    OrderInsert(ev,(en.OccurTime+durtime,i),cmp);     }//客户离开void CustomerDepature(){        i=en.NType;    DeQueue(q[i],c);                                //删除队列元素(客户离开)    TotalTime+=en.Duration-customer.ArrivalTime;                            //累加客户业务等待时间    if(!QueueEmpty(q[i])){        GetHead(q[i],customer);        orderInsert(ev,(en.OccurTime+curtomer.Duration,i),(*cmp)());    }}//银行排队模拟程序void Bank_Simulation(int CloseTime){    OpenDay();    while(!ListEmpty(ev)){                DelFirst(GetHead(ev),p);en=GetCurElem(p);        if(en.NType==0){            CustomerArrived();                    //客户到来        }else{             CustomerDepature();                    //客户离开        }    }    printf("The Average Time is %f\n",(float)TotalTime/CustomerNum);}
1 0
原创粉丝点击