离散事件模拟

#include<stdio.h>
#include<stdlib.h>
#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<M+1;++i)
if(q[i].length<minx)
{minx=q[i].length;k=i;}
return k;
}

void main()
{
int durtime,inteval,totaltime=0;
int i,c,count=0;
QueueNode f;
EventNode *pe,*event;
eventlst=(EventList*)malloc(sizeof(EventList));
if(eventlst==NULL) return;
if(generate(&pe)==-1) return;
pe->occurTime=0;pe->eventType=0;
pe->next=NULL;
eventlst->front=pe;eventlst->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(&durtime,&inteval);
if((event->occurTime+inteval)<CLOSETIME)
InsertEvent(eventlst,event->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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值