c链表,队列实现银行用户事务的模拟

1.问题描述:假设某银行有四个窗口对外接待客户,从早晨银行开门起不断有客户进入银行。由于每个窗口在某个时刻只能接待一个客户,因此在客户人数众多时需在每个窗口前顺次排队,对于刚进入银行的客户,如果某个窗口的业务员正空闲,则可上前办理业务,反之,若四个窗口均有客户所占,他便会排在人数最少的队伍后面。现在需要编制程序以模拟银行的这种业务活动并计算一天中客户在银行逗留的平均时间。

2.基本要求

(1)初始化(OpenForDay),模拟银行开门时各数据结构的状态。

(2)事件驱动(EventDrived), 对客户到达和离开事件做相应处理。

(3)下班处理(CloseForDay),模拟银行关门时的动作,统计客户平均逗留时间。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<math.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;
#define Qu 4
typedef struct
{
	int OccurTime;
	int NType;
}Event,ElemType;
typedef struct LNode
{
	ElemType data;
	LNode *next;
}*Link,*Position;
struct LinkList
{
	Link head,tail;
	int len;
};

typedef struct
{
	int ArrivalTime;
	int Duration;
}QElemType;
typedef struct QNode
{
	QElemType data;
	QNode *next;
}*QueuePtr;
struct LinkQueue
{
	QueuePtr front,rear;
};
typedef LinkList EventList;
EventList ev;
Event en;
Event et;
LinkQueue q[Qu];
QElemType customer;
int TotalTime=0,CustomerNum=0;
int CloseTime;
Status InitList(LinkList &L)
{
	Link p;
	p=(Link)malloc(sizeof(LNode));
	if(p)
	{
		p->next=NULL;
		L.head=L.tail=p;
		L.len=0;
		return OK;
	}
	else
		return ERROR;
}
Status DelFirst(LinkList &L,Link h,Link &q)
{
	q=h->next;
	if(q)
	{
		h->next=q->next;
		if(!h->next)
			L.tail=h;
		L.len--;
		return OK;
	}
	else
		return FALSE;
}
ElemType GetCurElem(Link p)
{
	return p->data;
}
Status ListEmpty(LinkList L)
{
	if(L.len)
		return FALSE;
	else
		return TRUE;
}
int ListLength(LinkList L)
{
	return L.len;
}
Position GetHead(LinkList L)
{
	return L.head;
}
Status OrderInsert(LinkList &L,ElemType e,int(*comp)(ElemType,ElemType))
{
	Link o,p,q;
	q=L.head;
	p=q->next;
	while(p!=NULL&&comp(p->data,e)<0)
	{
		q=p;
		p=p->next;
	}
	o=(Link)malloc(sizeof(LNode));
	o->data=e;
	q->next=o;
	o->next=p;
	L.len++;
	if(!p)
		L.tail=o;
	return OK;
}
Status InitQueue(LinkQueue &Q)
{
	if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
		exit(OVERFLOW);
	Q.front->next=NULL;
	return OK;
}
Status QueueEmpty(LinkQueue Q)
{
	if(Q.front==Q.rear)
		return TRUE;
	else
		return FALSE;
}
int QueueLength(LinkQueue Q)
{
	int i=0;
	QueuePtr p;
	p=Q.front;
	while(Q.rear!=p)
	{
		i++;
		p=p->next;
	}
	return i;
}
Status GetHead(LinkQueue Q,QElemType &e)
{
	QueuePtr p;
	if(Q.front==Q.rear)
		return ERROR;
	p=Q.front->next;
	e=p->data;
	return OK;
}
Status EnQueue(LinkQueue &Q,QElemType e)
{
	QueuePtr p;
	if(!(p=(QueuePtr)malloc(sizeof(QNode))))
		exit(OVERFLOW);
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{
	QueuePtr p;
	if(Q.front==Q.rear)
		return ERROR;
	p=Q.front->next;
	e=p->data;
	Q.front->next=p->next;
	if(Q.rear==p)
		Q.rear=Q.front;
	free(p);
	return OK;
}
int cmp(Event a,Event b)
{
	if(a.OccurTime==b.OccurTime)
		return 0;
	else
		return  (a.OccurTime-b.OccurTime)/abs(a.OccurTime-b.OccurTime);
}
void OpenForDay()
{
	int i;
	InitList(ev);
	en.OccurTime=0;
	en.NType=Qu;
	OrderInsert(ev,en,cmp);
	for(i=0;i<Qu;++i)
		InitQueue(q[i]);
}
void Random(int &d,int &i)
{
	d=rand()%30+1;
	i=rand()%5+1;
}
int Minimum(LinkQueue Q[])
{
	int t[Qu];
	int i,k;
	for(i=0;i<Qu;i++)
		t[i]=QueueLength(Q[i]);
	k=0;
	for(i=1;i<Qu;i++)
		if(t[i]<t[0])
		{
			t[0]=t[i];
			k=i;
		}
		return k;
}

void CustomerArrived(LinkQueue Q[])
{
	QElemType f;
	int durtime,intertime,i,j;
	++CustomerNum;
	Random(durtime,intertime);
	et.OccurTime=en.OccurTime+intertime;
	
	et.NType=Qu;
	if(et.OccurTime<CloseTime)
		OrderInsert(ev,et,cmp);
	i=Minimum(q);
	f.ArrivalTime=en.OccurTime;
	f.Duration=durtime;
	EnQueue(q[i],f);
	if(QueueLength(q[i])==1)
	{
		et.OccurTime=en.OccurTime+durtime;
		et.NType=i;
		OrderInsert(ev,et,cmp);
	}
	printf("客户%d 到达时间为:%d 窗口:%d \n",CustomerNum,et.OccurTime,i+1);
	for(i=0;i<Qu;i++)
	{
		printf("窗口%d: ",i+1);
	for(j=0;j<QueueLength(Q[i]);j++)
		printf("♀");
	printf("\n");
	}
}
void CustomerDeparture()
{
	int i;
	i=en.NType;
	DeQueue(q[i],customer);
	TotalTime+=en.OccurTime-customer.ArrivalTime;
	if(!QueueEmpty(q[i]))
	{
		GetHead(q[i],customer);
		et.OccurTime=en.OccurTime+customer.Duration;
		et.NType=i;
		OrderInsert(ev,et,cmp);
	}
}
void Bank_Simulation()
{
	Link p;
	OpenForDay();
	while(!ListEmpty(ev))
	{
		DelFirst(ev,GetHead(ev),p);
		en.OccurTime=GetCurElem(p).OccurTime;
		en.NType=GetCurElem(p).NType;
		if(en.NType==Qu)
			CustomerArrived(q);
		else
			CustomerDeparture();
	}
	printf(" 顾客总数:%d, 所有顾客共耗时 :%d分,平均每人耗时 %d 分钟\n",CustomerNum,TotalTime,TotalTime/CustomerNum);
}
void main()
{
	
	printf("假设客户办理业务时间为1到30分钟,每1到5分钟到来一个客户\n");
	printf("输入营业时间(分钟)\n");
	scanf("%d",&CloseTime);
	Bank_Simulation();
}

 

 

假设某银行有n个窗口对外接待客户,从早晨银行9点开门起到5点关门,不断有客户进入银行,由于每个窗口在某个时刻只能接待一个客户。因此在客户人数众多时需要在每个窗口前顺次排队,对于刚进银行的客户。如果某个窗口的业务员正空闲,则可上前输业务。反之,若个窗口有客户所占,他便会排在为数最少的队伍后面。编制一个程序模拟银行的这种业务活动并计算一天中客户在银行的平逗留时间。 首先从题目分析:N个窗口排队,首先就要建立N个队列来存储排队的用户信息 ,然后算出那个队列最短就用户就到那个队伍排队,同时通过随机生成他办理业务的时间和到来的时间,通过计算用户的到来时间和离开时间就可以计算出某个用户银行的逗留时间 ;话不多说直接上代码。 下面是主函数,由用户输入银行上下班时间,计算营业多长时间Total_time,如何当前时间小于关门的时间,就一直进入customer_into();函数,用户不断的进来 #define FALSE 0 #define TRUE 1 #define QUEUE_SUM 4 //窗口的数量 int rand_business_time=0, rand_wait_time=0;//定义办理时间,等待时间变量 int Total_time=0,now_tim=0;//总时间,当前时间 int go_time[4] = {0,0,0,0};//定义数组存储每个窗口最后一位办理完业务的时间 int sum_nan[4] = {0,0,0,0};//定义数组存储每个窗口排队的人数 int Sign=TRUE; //是否关门标志位 float Sum_Wait_Time=0.0; //等待的总时间 float Sun_Nan=0.0; //总人数 int open_time;//开门时间 int off_time; //关门时间 int main() { Prompted(); printf("输入银行的24小时制营业时间:如营业时间为9:00--17:00,则应输入:9,17\n"); scanf("%d,%d", &open;_time,&off;_time); Total_time = (off_time - open_time) * 60;//计算银行总营业多少分钟 for (int i = 0; i now_time) { customer_into(); //客户进入函数 } printf("银行关门时间到不再接收客人\n\n"); for (int i = 0; i < QUEUE_SUM; i++) { DisposeQueue(&queue;[i],i);//输入在银行关门前还没有办理完业务的客户信息 } printf("平时间为%.2f分钟",Sum_Wait_Time/Sun_Nan); /*通过各个客户的总等待时间总和/总人数算出逗留平时间*/ _getch(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值