教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 范文大全 > 文秘资料 >

银行离散事件模拟

来源:网络收集 时间:2026-09-18
导读: /* 银行业务模拟与离散事件模拟 1.问题描述 假设某银行有4个窗口对外接待客户,从早晨银行开门(开门9:00am,关门5:00pm)起不断有客户进入银行。由于每个窗口在某个时刻只能接待一个客户,因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进入银行

/*
银行业务模拟与离散事件模拟
1.问题描述
假设某银行有4个窗口对外接待客户,从早晨银行开门(开门9:00am,关门5:00pm)起不断有客户进入银行。由于每个窗口在某个时刻只能接待一个客户,因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进入银行的客户(建议:客户进入时间使用随机函数产生),若某个窗口的业务员正空闲,则上前办理业务;反之,若4个窗口均有窗户所占,他便会排在人数最少的队伍后面。
2.任务要求
编制一个程序以模拟银行的这种业务活动并计算一天中客户在银行逗留的平均时间。
三、算法的思想与算法实现步骤
1.基本思想
通过队列数据类型进行基本操作,主要有三个模块:分别是主函数模块、
主要操作函数及基本操作函数。其中,主函数负责其他子函数的调用实现以及基本界面的操作,主要函数包括开门函数的实现:OpenForDay,顾客到达函数:CustomerArrived,顾客离开的函数:CustomerDepartion等;而基本操作函数就是对其中牵扯到的操作进行具体的实现,如按时间先后插入队列OrderInsert、寻求最短的队列Mininum、删除队列元素以及销毁等。
2.实现步骤
首先,分析题目要求划分实现模块、画出大致的流程图,定义基本数据类型,诸如结构体、队列等;
其次,考虑基本大致的操作,比如要拟定开门的时间、顾客到来为其提供服务以及离开时的操作等;
再次,针对上述的基本操作实现具体需要进行的操作,具体实现每个环节需要进行的基本操作,即具体编写每个小函数实现功能;
最后,编写主函数对每个实现进行按需调用,实现操作。
*/


#include<stdio.h>
#include<malloc.h>
#include<math.h>
#include<stdlib.h>
#define NULL 0

typedef struct LNode{
int OccurTime;
int NType;
struct LNode *next;
}*LinkList;

typedef LinkList EventList;

typedef struct QNode{
int ArrvalTime;
int Duration;
struct QNode *next;
}*Queue;

struct QueueList
{
Queue front;
Queue rear;
};

typedef int Status;

EventList ev; //事件表
LNode en; //事件
QueueList q[5]; //四个窗口队列
int TotalTime,CustomerNum; //逗留时间和总人数
int CloseTime; //银行关门时间

Status OrderInsert(EventList &ev,LNode en); //插入事件到事件表函数
bool ListEmpty(EventList ev); //判断事件表是否为空
Status GetLinkHead(EventList ev,LNode &en); //取出事件表头事件,并在事件表中删去头事件
int QLength(QueueList q); //返回队列长度
int Mininum(QueueList q[]); //判断最短
队列
Status EnQueue(QueueList &q,int Occurtime,int durtime); //插入客户到队列
Status CustomerArrived(EventList &ev,LNode en); //客户到银行函


bool QueueEmpty(QueueList q); //判断队列是否为空
Status CustomerDeparture(EventList &ev,LNode en); //客户出银行函数

Status main()
{

printf("输入银行关门时间\n");
scanf("%d",&CloseTime);
ev=(EventList)malloc(sizeof(LNode)); //分配事件表空间
ev->next=NULL;
for(int i=1;i<5;i++)
{//分配四个窗口队列空间
q[i].front=(Queue)malloc(sizeof(QNode));
q[i].rear=(Queue)malloc(sizeof(QNode));
q[i].front->next=q[i].rear->next=NULL;
}
TotalTime=0; //逗留时间初始化为0
CustomerNum=0; //总人数初始化为0
en.NType=0; //初始化事件为客户到达
en.OccurTime=0; //初始化事件发生时间为0
OrderInsert(ev,en); //插入事件到事件表
while(!ListEmpty(ev))
{
GetLinkHead(ev,en);
if(en.NType==0)
CustomerArrived(ev,en);
else
CustomerDeparture(ev,en);
};
printf("总人数:\n%d",CustomerNum);
printf("平均逗留时间:\n%f",(float)(TotalTime/CustomerNum));
return 0;
}


Status OrderInsert(EventList &ev,LNode en)
{
LNode *probe=(EventList)malloc(sizeof(LNode));
LNode *node=(EventList)malloc(sizeof(LNode));
probe=ev;
node->NType=en.NType;
node->OccurTime=en.OccurTime;
node->next=NULL;
if(probe->next==NULL)
probe->next=node;
else
{
while(probe->next&&node->OccurTime>probe->next->OccurTime)
probe=probe->next;
node->next=probe->next;
probe->next=node;
}
return 0;
}
Status CustomerArrived(EventList &ev,LNode en)
{
printf("********顾客到达*******\n");
CustomerNum++;
int durtime=rand()%30+1; //处理时间
int intertime=rand()%5+1; //下个客户达到时间间隔
//插入到队列
int i=Mininum(q);
printf("%d\n",en.OccurTime);
EnQueue(q[i],en.OccurTime,durtime);
LNode event;
event.NType=0;
event.next=NULL;
event.OccurTime=en.OccurTime+intertime;
if(event.OccurTime<CloseTime)
OrderInsert(ev,event);
if(QLength(q[i])==1)
{
event.NType=i;
event.OccurTime=en.OccurTime+durtime;
OrderInsert(ev,event);
}
for(int a=1;a<5;a++)
{
printf("队列%d:",a);
for(int j=0;j<QLength(q[a]);j++)
printf("人");
printf("\n");
}
return 0;
}
Status CustomerDeparture(EventList &ev,LNode en)
{
int i=en.NType;
Queue node=(Queue)malloc(sizeof(QNode));
node=q[i].front->next;
TotalTime+=en.OccurTime-node->ArrvalTime;
printf("*******顾客离开******\n");
printf("%d\n",en.OccurTime);
q[i].front->next=node->next;
if(!QueueEmpty(q[i]))
{
en.NType=i;
en.OccurTime+=q[i].front->next->Duration;
en.next=NULL;
OrderInsert(ev,en);
}
for(int a=1;a<5;a++)
{
printf("队列%d:",a);
for(int j=0;j<QLength(q[a]);j+
+)
printf("人");
printf("\n");
}
return 0;
}
Status EnQueue(QueueList &q,int Occurtime,int durtime)
{
Queue node=

(Queue)malloc(sizeof(QNode));
node->ArrvalTime=Occurtime;
node->Duration= …… 此处隐藏:2731字,全部文档内容请下载后查看。喜欢就下载吧 ……

银行离散事件模拟.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/fanwen/736812.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)