#include #include #define CLOSETIME 1000 #define M 3 typedef struct QueueNode{ int arrivetime; int duration; struct QueueNode *next; }QueueNode; typedef struct QueueHeader{ QueueNode *front,*rear; int length; }QueueHeader; typedef struct EventNode{ int occurTime,eventType; struct EventNode *next; }EventNode; typedef struct EventList{ struct EventNode *front,*rear; int eventNum; }EventList; QueueHeader q[M+1]; EventList *eventlst; int seed=300; int generate(EventNode **p) { *p=(EventNode*)malloc(sizeof(EventNode)); if(!(*p)) {printf("allocation error!");return -1;} return 0; } void InsertEvent(EventList *eventlst,int occurTime,int etype) { EventNode *p,*q,*qre; p=(EventNode*)malloc(sizeof(EventNode)); p->occurTime=occurTime;p->eventType=etype; eventlst->eventNum++; q=eventlst->front;qre=NULL; while(q&&occurTime>q->occurTime) { qre=q;q=q->next ; } if(!qre) {p->next=eventlst->front;eventlst->front=p;} else {p->next =q;qre->next =p;} if(eventlst->eventNum==1) eventlst->rear=p; } void DeleteEvent(EventList *eventlst,EventNode **event) { EventNode *p; p=eventlst->front; eventlst->front=p->next; if(--eventlst->eventNum<1) eventlst->rear=eventlst->front; *event=p; } void EnQueue(QueueHeader *swindow,int t1,int t2) { QueueNode *p; p=(QueueNode*)malloc(sizeof(QueueNode)); p->arrivetime=t1;p->duration=t2; swindow->length++; if(swindow->length==1) { p->next=NULL;swindow->front=p;swindow->rear=p; } else { swindow->rear->next=p;swindow->rear=p; } } void DeQueue(QueueHeader *swindow,QueueNode *f) { QueueNode *p; if(swindow->length>0) { f->arrivetime=swindow->front->arrivetime; f->duration=swindow->front->duration; p=swindow->front;swindow->front=swindow->front->next; swindow->length--; if(swindow->length==0) swindow->rear=NULL; free(p); } } void random(int *durtime,int *intevaltime) { *durtime=rand()+1; *intevaltime=rand()+1; } int rand() { int k=(int)(((double)seed/32767)*10000)%29; seed=(13107*seed+6553)%32767; return k; } int Minlength() { int minx,k,i; minx=q[1].length;k=1; for(i=2;i occurTime=0;pe->eventType=0; pe->next=NULL; eventlst->front=pe;eventlst->rear=pe; eventlst->eventNum=1; for(i=1;i eventNum>0) { DeleteEvent(eventlst,&event); if(event->eventType==0) { printf("A client entered!"); count++; random(&durtime,&inteval); if((event->occurTime+inteval) occurTime+inteval,0); c=Minlength(); EnQueue(&q[c],event->occurTime,durtime); printf("Insert to window %d", c); printf("(arrive time:%d duration:%d)\n",event->occurTime,durtime); if(q[c].length==1) InsertEvent(eventlst,event->occurTime+durtime,c); } else { i=event->eventType; DeQueue(&q[i],&f); printf("service window%d: A client left!\n",i); totaltime+=event->occurTime-f.arrivetime; if(q[i].length!=0) InsertEvent(eventlst,event->occurTime+q[i].front->duration ,i); } } printf("\n Total Customers:%d\n",count); printf("Average duration:%d\n",totaltime/count); }