#include <stdio.h> #include <stdlib.h> typedef struct { int occur; int type; } Event; typedef struct { int arrive; int duration; } QElem; typedef struct LNode{ Event event; struct LNode *next; } LNode,*Link; typedef struct { Link head; int len; } LinkList; typedef struct QNode{ QElem customer; struct QNode *next; } QNode,*QPtr; typedef struct { QPtr front; QPtr rear; int len; } LinkQueue; LinkList ev; Event en; LinkQueue q[5]; QElem customer; int TotalTime, CustomerNum; int CloseTime; void L_insert(LinkList &p, Event en) { Link s=(Link)malloc(sizeof(LNode)); s->event=en; Link q=p.head,pre=NULL; while(q!=NULL&&q->event.occur<=en.occur) { pre=q; q=q->next; } if(pre==NULL) { s->next=p.head; p.head=s; } else { s->next=pre->next; pre->next=s; } ++p.len; } void L_Pop(LinkList &p) { Link q=p.head; p.head=p.head->next; free(q); --p.len; } void Q_pop(LinkQueue &p) { QPtr q=p.front; p.front=p.front->next; if(p.front==NULL) p.rear=NULL; free(q); --p.len; } void Q_push(LinkQueue &p, QElem customer) { QPtr q=(QPtr)malloc(sizeof(QNode)); q->customer=customer; q->next=NULL; if(p.front==NULL) { p.front=q; p.rear=q; ++p.len; } else { p.rear->next=q; p.rear=q; ++p.len; } } int Minimum() { int min=10000,re; for(int i=1; i<=4; ++i) if(min>q[i].len) { re=i; min=q[i].len;} return re; } void OpenForDay() { TotalTime=0; CustomerNum=0; ev.len=0, ev.head=NULL; en.occur=0, en.type=0; L_insert(ev,en); for(int i=1; i<=4; ++i) { q[i].front=q[i].rear=NULL; q[i].len=0; } } void CustomerArrived() { ++CustomerNum; int durtime,intertime,t; durtime=rand()%30+1; intertime=rand()%5+1; t=en.occur+intertime; Event tp; tp.occur=t; tp.type=0; if(t<CloseTime) L_insert(ev,tp); int i=Minimum(); QElem tq; tq.arrive=en.occur; tq.duration=durtime; Q_push(q[i],tq); if(q[i].len==1) { Event tt; tt.occur=en.occur+durtime; tt.type=i; L_insert(ev,tt); } printf("A client arrive ! TO WINDOW %d time: %d/n", i, en.occur); } void CustomerDeparture(){ int i=en.type; customer.arrive=q[i].front->customer.arrive; customer.duration=q[i].front->customer.duration; Q_pop(q[i]); TotalTime+=en.occur-customer.arrive; if(q[i].len) { customer.arrive=q[i].front->customer.arrive; customer.duration=q[i].front->customer.duration; Event tt; tt.occur=en.occur+customer.duration; tt.type=i; L_insert(ev,tt); } printf("A client departe ! FROM WINDOW %d time: %d/n", i, en.occur); } void bank_simulation() { printf("输入关门时间(分钟) 最大为24*60,默认银行开门时间为0:00/n"); scanf("%d",&CloseTime); OpenForDay(); while(ev.len) { en=ev.head->event; L_Pop(ev); if(en.type==0) CustomerArrived(); else CustomerDeparture(); } printf("The Average Time is %f/n",(float)TotalTime/CustomerNum); } int main() { bank_simulation(); return 0; } 留个纪念。