严蔚敏 数据结构C语言 银行排队队列 离散事件模拟

本文主要是介绍严蔚敏 数据结构C语言 银行排队队列 离散事件模拟,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

以下代码源自http://blog.csdn.net/nuaazdh/article/details/7059630,在CentoS下用 【g++ -o bank bank.c 】编译通过,稍微修改。

系统每次随机生成的是1)当前顾客顾客的柜台被服务时间2)当前顾客和下一个顾客到达的间隔时间,记住这2点就理解了整个代码,书里说得不太清楚。每个顾客在银行的等待时间取决于队列里前一个节点的离开时间,而不是自己的到达时间+服务时间。

代码设置了EventList这个事件链表 ,来保存随机生成的顾客到达时间和服务时间,模拟排队业务。

不过现实生活中,顾客达到时间可以模拟,但是被服务时间一般不好模拟!不知道这样的事件驱动模型在哪个实际场景中有用?

//离散事件模拟,模拟银行营业时的排队情况  
//不考虑顾客中途离开,顾客到达事件随机,业务办理时间  
//长度随机,选择最短的队排队,不再换队  
//作者:nuaazdh  
//时间:2011年12月10日 08:52:37  
#include <stdio.h>  
#include <time.h>  
#include <stdlib.h>  #define OK 1  
#define ERROR 0  
#define TRUE 1  
#define FALSE 0  typedef int Status;  
typedef struct Event{   //事件类型  int OccurTime;  //事件发生时刻  int NType;      //事件类型,0表示到达事件,1至4表示四个窗口的离开事件  struct Event *next;  
}Event,ElemType;  typedef struct{ //单向链表结构  ElemType *head;//头指针  ElemType *tail;//尾指针  int len;    //长度  
}LinkList;  typedef LinkList EventList; //事件链表  typedef struct QElemType{ //队列元素  int ArriveTime;//到达时间  int Duration;//办理业务所需时间  struct QElemType *next;  
}QElemType;  typedef struct{//队列结构  QElemType *head;//头指针  QElemType *tail;//尾指针  
}LinkQueue;  Event NewEvent(int occurT,int nType);  //根据OccurTime和NType值,创建新事件  
Status InitList(LinkList *L);  //初始化事件链表  
Status OrderInsert(LinkList *L,Event e);  //将事件e按发生时间顺序插入有序链表L中  
Status ListEmpty(LinkList *L);  //判断链表L是否为空,为空返回TRUE,否则返回FALSE  
Status DelFirst(LinkList *L,ElemType *e);  //链表L不为空,删除其首结点,用e返回,并返回OK;否则返回ERROR  
Status ListTraverse(LinkList *L);  //遍历链表  
Status InitQueue(LinkQueue *Q);  //初始化队列Q  
Status EmptyQueue(LinkQueue *Q);  //若队列Q为空,返回TRUE,否则返回FALSE  
Status DelQueue(LinkQueue *Q,QElemType *e);  //若队列Q不为空,首结点出队,用e返回,并返回OK;否则返回ERROR  
Status EnQueue(LinkQueue *Q,QElemType e);  //结点e入队Q  
int QueueLength(LinkQueue Q);  //返回队列Q的长度,即元素个数  
Status GetHead(LinkQueue *Q,QElemType *e);  //若队列Q不为空,用e返回其首结点,并返回OK,否则返回ERROR  
Status QueueTraverse(LinkQueue *Q);  //遍历队列Q  //------------------//  
int Min(int a[],int n);  //返回长度为n的数组a第一个最小值的下标,从1开始  
int ShortestQueue();  //获取最短队列的编号  
void OpenForDay();  //初始化操作  
void CustomerArrived();  //顾客达到事件  
void CustomerDepature();  //顾客离开事件  
void Bank_Simulation();  //银行排队模拟  
void PrintEventList();  //输出事件队列  
void PrintQueue();  //打印当前队列  
//----全局变量-----//  
EventList ev;  
Event en;  
LinkQueue q[5];  
QElemType customer;  
int TotalTime,CustomerNum;  
int CloseTime=200;//关闭时间,即营业时间长度  //--------------main()------------------//  
int main()  
{  Bank_Simulation();  return 0;  
}  //--------------模拟排队----------------//  
void OpenForDay(){  //初始化操作  int i;  TotalTime=0;    CustomerNum=0;  InitList(&ev);//初始化事件队列  en.OccurTime=0;  en.NType=0;  OrderInsert(&ev,en);  for(i=1;i<=4;i++)  InitQueue(&q[i]);//初始化四个窗口队列  
}//OpenForDay  void CustomerArrived(){  //顾客达到事件  int durtime,intertime,i,t;  QElemType e;  ++CustomerNum;  intertime=rand()%5+1;//间隔时间在5分钟内  durtime=rand()%30+1;//办理业务时间在30分钟内  t=en.OccurTime+intertime;  if( en.OccurTime<CloseTime){//银行尚未关门  printf("A new customer arrived at:%d,his durTime=%d,the next intertime=%d|\n",en.OccurTime,durtime,intertime);//下一位顾客达到时间  OrderInsert(&ev,NewEvent(t,0));  i=ShortestQueue();//最短队列  e.ArriveTime=en.OccurTime;  e.Duration=durtime;  EnQueue(&q[i],e);  if(QueueLength(q[i])==1)  OrderInsert(&ev,NewEvent(en.OccurTime+durtime,i));  }else{printf("maxinum exceed!stop,en.OccurTime=%d,intertime=%d\n",en.OccurTime,intertime);}  
}  void CustomerDepature(){  //顾客离开事件  int i=en.NType;  DelQueue(&q[i],&customer);  printf("A customer leaves at:%d\n",en.OccurTime);//输出顾客离开时间  TotalTime+=en.OccurTime-customer.ArriveTime;  if(!EmptyQueue(&q[i])){  GetHead(&q[i],&customer);  OrderInsert(&ev,NewEvent(en.OccurTime+customer.Duration,i));  }  
}  void Bank_Simulation(){  //银行排队模拟  OpenForDay();  srand((unsigned)time(NULL));  while(!ListEmpty(&ev)){  DelFirst(&ev,&en); printf("--------action--------------------------\n"); if(en.NType==0)  CustomerArrived();  else  CustomerDepature();  PrintQueue();  PrintEventList();}  printf("\nTotal time is: %d min,average time is: %g min.\n",TotalTime,(float)TotalTime/CustomerNum);  
}  void PrintQueue(){  //打印当前队列  int i;  for(i=1;i<=4;i++){  printf("Queue %d have %d customer(s):",i,QueueLength(q[i]));  QueueTraverse(&q[i]);  }  printf("\n");  
}  void PrintEventList(){  //输出事件队列  printf("Current Eventlist is:\n");  ListTraverse(&ev);  
}  
int Min(int a[],int n){  //返回长度为n的数组a第一个最小值的下标,从0开始  int i,tmp,ind=0;  tmp=a[0];  for(i=1;i<n;i++){  if(a[i]<tmp){  tmp=a[i];  ind=i;  }  }  return ind;  
}  int ShortestQueue(){  //获取最短队列的编号  int i,a[4];  for(i=1;i<=4;i++){  a[i-1]=QueueLength(q[i]);  //printf("队%d的长度为%d\n",i,QueueLength(q[i]));  }  return Min(a,4)+1;//队列从1开始编号  
}  //-----------队和链表操作--------------//  
Event NewEvent(int occurT,int nType){  //根据OccurTime和NType值,创建新事件  Event e;  e.OccurTime=occurT;  e.NType=nType;  return e;  
}  Status InitList(LinkList *L){  //初始化事件链表  L->head=L->tail=(ElemType *)malloc(sizeof(ElemType));  if(!L->head){  printf("Apply for memory error.LinkList initialize failed.\n");  exit(0);  }  L->head->next=NULL;  return OK;  
}  Status OrderInsert(LinkList *L,Event e){  //将事件e按发生时间顺序插入有序链表L中  ElemType *p,*q,*newptr;  newptr=(ElemType *)malloc(sizeof(ElemType));  if(!newptr){  printf("Apply for memory error,new node can't insert intot the Eventlist.\n");  exit(0);  }  *newptr=e;  if(TRUE==ListEmpty(L)){//链表为空  L->head->next=newptr;  L->tail=newptr;  L->tail->next=NULL;  return OK;  }  q=L->head;  p=L->head->next;  while(p){//遍历整个链表  if(p->OccurTime>=newptr->OccurTime)  break;  q=p;  p=p->next;  }  q->next=newptr;  newptr->next=p;  if(!p)//插入位置为链表尾部  L->tail=newptr;  return OK;  
}  Status ListEmpty(LinkList *L){  //判断链表L是否为空,为空返回TRUE,否则返回FALSE  if((L->head==L->tail)&&(L->head!=NULL))  return TRUE;  else  return FALSE;  
}  Status DelFirst(LinkList *L,ElemType *e){  //链表L不为空,删除其首结点,用e返回,并返回OK;否则返回ERROR  ElemType *p=L->head->next;  if(!p)  return ERROR;  L->head->next=p->next;  *e=*p;  free(p);  if(L->head->next==NULL)  L->tail=L->head;  return OK;  
}  Status ListTraverse(LinkList *L){  //遍历链表  Event *p=L->head->next;  if(!p){  printf("List is empty.\n");  return ERROR;  }  while(p!=NULL){  printf("OccurTime:%d,Event Type:%d\n",p->OccurTime,p->NType);  p=p->next;  }  printf("\n");  return OK;  
}  Status InitQueue(LinkQueue *Q){  //初始化队列Q  Q->head=Q->tail=(QElemType *)malloc(sizeof(QElemType));  if(!Q->head){  printf("Apply for memory error.LinkQueue initialize failed.\n");  exit(0);  }  Q->head->next=NULL;  return OK;  
}  Status EmptyQueue(LinkQueue *Q){  //若队列Q为空,返回TRUE,否则返回FALSE  if(Q->head==Q->tail&&Q->head!=NULL)  return TRUE;  else  return FALSE;  
}  Status DelQueue(LinkQueue *Q,QElemType *e){  //若队列Q不为空,首结点出队,用e返回,并返回OK;否则返回ERROR  QElemType *p=Q->head->next;  if(!p)  return ERROR;  *e=*p;  Q->head->next=p->next;//修正队首指针  free(p);  if(!Q->head->next)//队空  Q->tail=Q->head;  return OK;  
}  Status EnQueue(LinkQueue *Q,QElemType e){  //结点e入队Q  QElemType *p=(QElemType *)malloc(sizeof(QElemType));  if(!p){  printf("Apply for memory error,new element can't enqueue.\n");  exit(0);  }  *p=e;  p->next=NULL;  Q->tail->next=p;//插入队尾  Q->tail=p;//修改队尾指针  return OK;  
}  int QueueLength(LinkQueue Q){  //返回队列Q的长度,即元素个数  int count=0;  QElemType *p=Q.head->next;  while(p){  p=p->next;  count++;  }  return count;  
}  Status GetHead(LinkQueue *Q,QElemType *e){  //若队列Q不为空,用e返回其首结点,并返回OK,否则返回ERROR  if(EmptyQueue(Q))  return ERROR;  *e=*(Q->head->next);  return OK;  
}  Status QueueTraverse(LinkQueue *Q){  //遍历队列Q  QElemType *p=Q->head->next;  if(!p){  printf("--Is empty.\n");  return ERROR;  }  while(p){  printf("(%d,%d) ",p->ArriveTime,p->Duration);  p=p->next;  }  printf("\n");  return OK;  
}  


这篇关于严蔚敏 数据结构C语言 银行排队队列 离散事件模拟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/796738

相关文章

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

python运用requests模拟浏览器发送请求过程

《python运用requests模拟浏览器发送请求过程》模拟浏览器请求可选用requests处理静态内容,selenium应对动态页面,playwright支持高级自动化,设置代理和超时参数,根据需... 目录使用requests库模拟浏览器请求使用selenium自动化浏览器操作使用playwright

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

Go语言并发之通知退出机制的实现

《Go语言并发之通知退出机制的实现》本文主要介绍了Go语言并发之通知退出机制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、通知退出机制1.1 进程/main函数退出1.2 通过channel退出1.3 通过cont

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)