今天高兴,呵呵,给你敲敲代码吧呵呵
[CODE]
/*
BANK CUSTOMER SIMULATOR0.0
FROM SOFTWARE DESIGER BOOK
COPY BY ZINKING
200605021
*/
#include<stdio.h>
#include<stdlib.h>
#define CLOSETIME 10000
#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;
int eventType;
struct EventNode* next;
}EventNode;
typedef struct EventList{
strcut 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)
event->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
{
p->next=swindow->rear->next;
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* intervaltime)
{
*durtime=rand()+1;
*intervaltime=rand()+1;
}
int rand()
{
int k;
k=(int)(((double)seed/32767)*10000)%29;
sed=(13107*seed+6553)%32767;
return k;
}
int Minlength()
{
int minx,k,i;
minx=q[1].length;
k=1;
for(i=2;i<M+1;++i)
if(q[i].length<minx) {minx=q[i].length;k=i}
return k;
}
int main()
{
int durtime,interval,totaltime=0;
int i,c,count=0;
QueueNode f;
EventNode *pe,*event;
eventlst(EventList*)malloc(sizeof(EventList));
if(eventlst==NULL)return 0;
if(generate(&pe)==-1)return 1;
pe->occurTime=0;
pe->eventType=0;
pe->next=NULL;
eventlst->front=pe;
evenlst->rear=pe;
eventlst->eventNum=1;
for(i=1;i<M+1;i++)
{
q[i].front=q[i].rear=NULL;
q[i].length=0;
}
while(eventlst->eventNum>0)
{
DeleteEvent(eventlst,&event);
if (event->eventType==0)
{
printf("A client entered!");
count++;
random(&drtime,&interval);
if((event->occurTime+interval)<CLOSETIME)
InsetEvent(eventlst,event->occurTime+interval,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)
InsetEvent(eventlst,event->occurTime+durtime,c);
}
else
{
i=event->eventType;
DeQueue(&q[i],&f);
printf("sevice window %d: A client left! \n",i);
totaltime+=event->occurTime-f.arrivetime;
if(q[i].length!=0)
InsertEvent(enventlst,event->occurtime+q[i].front->duration,i);
}
}
printf("\n Total Customer: %d\n",count);
printf("Average duration: %d\n",totaltime/count);
return 2;
}
[/CODE]