01
离散事件
1、在日常生活中,经常会遇到许多为了维护社会正常秩序而需要排队的情景。这类活动的模拟程序通常需要用到队列和线性表之类的数据结构。
2、部分代码示例
//银行——离散事件模拟、
struct event{
int type,occurtime,money;//type为0,到达;为1,离开窗口;occurtime为0到600;
}ev1,ev2;
struct eventnode{
int type;
int occurtime;
int money;
struct eventnode *next;
} *evlist;
struct queuenode{
int money,duringtime,arrivetime;//arrivetime用于最后计算总时间
struct queuenode *next;
};
struct Queue{
struct queuenode *front;
struct queuenode *rear;
}queue1,queue2;
int totaltime,customernum,closetime,timemark,totalmoney;
void evlistinsert(struct event temp){
struct eventnode *p1,*p2,*p3;
p1=(struct eventnode *)malloc(sizeof(struct eventnode));
p1->type=temp.type;
p1->occurtime=temp.occurtime;
p1->money=temp.money;
p1->next=NULL;
if(!evlist){
evlist=p1;
}
else{
p2=evlist;
while(p2->next)p2=p2->next;
p2->next=p1;
}
}
void arrive(){
int duringtime,intertime;
++customernum;
duringtime=rand()%30+1;
if(ev1.money+totalmoney>=0){//能满足
struct queuenode *p;
p=(struct queuenode*)malloc(sizeof(struct queuenode));
queue1.rear->next=p;
queue1.rear=p;
p->next=NULL;
p->money=ev1.money;
p->duringtime=duringtime;
p->arrivetime=ev1.occurtime;//加入队列1
printf("顾客%d\n到达时间:%d\n业务用时:%d\n金额:%d\n",customernum,ev1.occurtime,duringtime,ev1.money);
ev2.type=1;
ev2.occurtime=timemark+duringtime;
timemark=ev2.occurtime;
ev2.money=ev1.money;
evlistinsert(ev2);//离开事件插入事件表
totaltime+=ev2.occurtime-ev1.occurtime;
}
else{
struct queuenode *p;
p=(struct queuenode *)malloc(sizeof(struct queuenode));
queue2.rear->next=p;
queue2.rear=p;
p->next=NULL;
p->money=ev1.money;
p->duringtime=duringtime;
p->arrivetime=ev1.occurtime;//加入队列2
printf("顾客%d\n到达时间:%d\n欲交易金额:%d,不能满足,进入队列2\n",customernum,ev1.occurtime,ev1.money);
printf("-----------------------------------------------\n");
}
intertime=rand()%10+1;
ev2.type=0;
ev2.occurtime=ev1.occurtime+intertime;
ev2.money=rand()%10000-5000;
if(ev2.occurtime>timemark)timemark=ev2.occurtime;
if(ev2.occurtime<closetime&&timemark<closetime)evlistinsert(ev2);//下一个顾客
}
void leave(){
struct queuenode *p;
if(ev1.type==1){
p=queue1.front->next;
queue1.front->next=p->next;
if(!queue1.front->next)queue1.rear=queue1.front;
free(p);//删除队列1头的顾客
totalmoney+=ev1.money;
printf("离开时间:%d分钟\n银行金额:%d\n",ev1.occurtime,totalmoney);
printf("-----------------------------------------------\n");
}
if(ev1.money<0)return;
struct queuenode *temp=queue2.front->next;
if(!temp)return;
int k=0,flag;
while(temp!=NULL){
k++;
temp=temp->next;
}
flag=k;
k=0;
int temptime=ev1.occurtime,tempmoney;
temp=queue2.front->next;
while(k<flag){
if(totalmoney+temp->money>=0){
tempmoney=temp->money;
totalmoney+=temp->money;
temptime+=temp->duringtime;
totaltime+=temptime-temp->arrivetime;
queue2.front->next=temp->next;
if(!queue2.front->next)queue2.rear=queue2.front;
temp=queue2.front->next;
free(p);//删除队列2头的顾客
printf("队列2有顾客离开了,时间为%d,交易金额为%d,银行金额为%d\n",temptime,tempmoney,totalmoney);
printf("-----------------------------------------------\n");
}
else if(temp->next){
queue2.front->next=temp->next;
temp->next=NULL;
queue2.rear->next=temp;
queue2.rear=temp;
temp=queue2.front->next;//队头掉到队尾
}
k++;
}
if(temptime>timemark)timemark=temptime;
}
void openforday(){
queue1.front=(struct queuenode *)malloc(sizeof(struct queuenode));
queue1.rear=queue1.front;
queue1.rear->next=NULL;
queue2.front=(struct queuenode *)malloc(sizeof(struct queuenode));
queue2.rear=queue2.front;
queue2.rear->next=NULL;
totaltime=0;
customernum=0;
totalmoney=1000;
closetime=1000;
evlist=(struct eventnode*)malloc(sizeof(struct eventnode));
evlist->occurtime=0;
evlist->type=0;
evlist->money=5000;
}
int main(){
struct eventnode *p;
srand(time(NULL));
openforday();//初始化
while(evlist){
ev1.occurtime=evlist->occurtime;
ev1.type=evlist->type;
ev1.money=evlist->money;
p=evlist;
evlist=evlist->next;
free(p);
if(ev1.type==0)arrive();
else leave();
}
struct queuenode *temp=queue2.front->next;
while(temp){
totaltime+=closetime-temp->arrivetime;
temp=temp->next;
}
printf("The average time is %f\n",(float)totaltime/customernum);
}
如果您觉得本篇文章对您有作用,请转发给更多的人,点一下好看就是对小编的最大支持!