c语言版数据结构(奇迹冬瓜)-队列实战(1)离散事件模拟(银行排队)
2012-10-10 15:08
841 查看
//c语言版数据结构(奇迹冬瓜)-队列实战(1)离散事件模拟(银行排队) //------头文件--------- #include<stdio.h> #include<stdlib.h> #include<time.h> //-------宏定义--------- #define TURE 1 #define ERROR 0 #define OVERFLOW -2 //------替换及结构体------ typedef int Bool; typedef struct Node { long OccurTime; long LeaveTime; struct Node *next; }Custom,*Customer;//模拟顾客属性 typedef struct { Customer front;//头 Customer rear;//尾 }LinkQueue;//顾客队列化 //-----全局变量------- long TotalTime,TotalCustomer;//总时间,总客户量 long OpenTime=420,CloseTime=1020;//银行开门时间和关门时间分别是420/60=7:00 1020/60=17:00 以24小时制计算 LinkQueue q[5];//银行开4个普通窗口,一个应急窗口 //------函数列表------ Bool InitQueue();//初始化队列 Bool DeQueue(int i,Customer c);//离开队列 Bool EnQueue(int i,Customer c);//进入队列 void OpenForDay();//初始化数据 //void Bank(Customer c,long *StartTime); void CompareTime(Custom c); void QueueLeagth(int *i); void Bank_Simulation(); void Rand(Customer c,long *StartTime); //-------主函数------ void main() { printf("--------------------系统说明-----------------------\n"); printf("1.关于银行业务系统的模拟.\n"); printf("2.银行的营业时间为7:00-17:00.\n"); printf("3.银行有四个窗口对外接待顾客.\n"); printf("4.窗口在某一时刻只能接待一位顾客,因此在客户众多的情况下需要排队等候.\n"); printf("5.若两个窗口均被占,则客户排在最短的窗口.\n"); printf("6.银行办理业务的时间为10-30分钟不等.\n"); Bank_Simulation(); printf("平均每个人在银行的逗留时间:"); printf("%-5.2f\n",(float)TotalTime/TotalCustomer); getchar(); getchar(); } //-------其余函数-------- void Bank_Simulation() { Custom c;//定义一个顾客 long StartTime=420; int i=1; OpenForDay(); Rand(&c,&StartTime); printf("等待时间\t业务办理时间\t窗口号\t\t到达时间\t离开时间\n"); while(c.LeaveTime<1020) { CompareTime(c); QueueLeagth(&i); EnQueue(i,&c); StartTime=c.OccurTime; printf("%d\t\t",i); printf("%d:%d\t\t",c.OccurTime/60,c.OccurTime%60); printf("%d:%d\n",c.LeaveTime/60,c.LeaveTime%60); getchar(); Rand(&c,&StartTime); } } void OpenForDay() { //int i; TotalTime=0; TotalCustomer=0; InitQueue(); } void Rand(Customer c,long *StartTime) { srand((unsigned)time(NULL)); do { c->OccurTime=rand()%1020; }while(c->OccurTime<*StartTime); } Bool InitQueue() { int i=1; for(;i<=4;i++) { q[i].rear=q[i].front=(Customer)malloc(sizeof(Custom)); if(!q[i].front) { exit(OVERFLOW); } q[i].front->next=NULL; } return TURE; } Bool DeQueue(int i,Customer c) { Customer t=(Customer)malloc(sizeof(Custom)); if(!t||q[i].front==q[i].rear) { exit(OVERFLOW); } t=q[i].front->next; c->OccurTime=t->OccurTime; c->LeaveTime=t->LeaveTime; q[i].front->next=t->next; if(q[i].rear==t) { q[i].rear=q[i].front; } free(t); return TURE; } Bool EnQueue(int i,Customer c) { long DurTime,WaitTime=0; Customer t=(Customer)malloc(sizeof(Custom)); if(!t) { exit(OVERFLOW); } if(q[i].front==q[i].rear) { WaitTime=0; } else { WaitTime=q[i].rear->LeaveTime-q[i].rear->OccurTime; } srand((unsigned)time(NULL)); do { DurTime=rand()%31; }while(DurTime<10); c->LeaveTime=c->OccurTime+WaitTime+DurTime; printf("%ld\t\t%ld\t\t",WaitTime,DurTime); t->OccurTime=c->OccurTime; t->LeaveTime=c->LeaveTime; TotalTime+=c->LeaveTime-c->OccurTime; t->next=NULL; q[i].rear->next=t; q[i].rear=t; TotalCustomer++; return TURE; } void CompareTime(Custom c) { int i; Custom t; Customer s; for(i=1;i<=4;i++) { s=q[i].front->next; while(s&&q[i].front!=q[i].rear) { if(c.OccurTime>=s->LeaveTime) { DeQueue(i,&t); s=q[i].front->next; } else { s=s->next; } } } } void QueueLeagth(int *i) { int min,j,k,a[4]; Customer s; for(j=0;j<4;j++) { k=0; s=q[j+1].front; if(q[j+1].front==q[j+1].rear) { a[j]=0; } else { while(s!=q[j+1].rear) { s=s->next; a[j]=++k; } } } min=a[0]; *i=1; for(j=0;j<4;j++) { if(min>a[j]) { min=a[j]; *i=j+1; } } }
相关文章推荐
- 严蔚敏 数据结构C语言 银行排队队列 离散事件模拟
- 离散事件模拟-银行管理 模拟,队列
- 离散事件模拟--银行排队时间模拟
- 3-9-模拟银行排队过程-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版
- c语言版数据结构(奇迹冬瓜)-栈实战(1)栈解迷宫<迷宫算法>
- 数据结构学习笔记 --- 队列的应用举例(离散事件模拟)
- c语言版数据结构(奇迹冬瓜)-单链式队列
- [SDUT](2087)离散事件模拟-银行管理 ---队列
- c语言版数据结构(奇迹冬瓜)-链表实战(4)双链表解一元多项式相乘
- 数据结构作业2 ------ 用队列模拟银行排队的情况
- c语言版数据结构(奇迹冬瓜)-栈实战(4)表达式求值
- 离散事件模拟-银行管理(模拟题,队列)
- 项目实战笔记 | C++ 实现银行排队服务模拟1 数据结构设计 |实验楼项目
- 数据结构学习笔记 --- 队列的应用举例(离散事件模拟)
- c语言版数据结构(奇迹冬瓜)-栈实战(2)整数进制转换<10进制到2,8,16进制>
- SDUT 2087 离散事件模拟-银行管理(队列模拟)
- 离散事件模拟-银行管理——队列思想(双队列)
- 离散事件模拟-银行管理(队列)
- c语言版数据结构(奇迹冬瓜)-链表实战(2)合并两有序线性表
- 事件驱动编程---队列应用--银行排队模拟--学习与思考