银行的问题

来源:互联网 发布:海马记忆训练软件 编辑:程序博客网 时间:2024/04/29 08:56

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>

typedef struct
{
  int Occurtime;//事件发生当前的时间
  int NType;//标志0表示客户到达,1表示1窗口客户离开,2表示2窗口客户离开,3表示3窗口客户离开,4表示4窗口客户离开
}Event;//事件类型
typedef struct LNode
{
 Event data;
 struct LNode *next;
}*EventList;; //定义一个事件链表
//typedef struct LNode *EventList;
//typedef struct LNode *LinkList;
typedef struct
{
 int Arrivaltime;//客户到达的时间
 int Duration;//客户办理事务的时间
}QElemType;//队列的数据类型
typedef struct QNode
{
 QElemType data;
 struct QNode *next;
}*Queueptr;//定义一个结点指针类型
typedef struct
{
 Queueptr front;
 Queueptr rear;
}LinkQueue; //定义一个队列

int InitList(EventList &L)//初始化事件链表
{
 L=(EventList)malloc(sizeof(LNode));
 if(!L)
  exit(0);
 L->next=NULL;
 return 1;
}

int cmp(Event a,Event b)//比较二个事件发生的时间前后
{
 if(a.Occurtime>b.Occurtime)
  return 1;
 else if(a.Occurtime==b.Occurtime)
  return 0;
 else return -1;
}

int OrderInsert(EventList &L,Event e,int (*cmp)(Event,Event))//按事件发生的时间前后插入到事件链表中
{
 EventList p,s;
 s=(EventList)malloc(sizeof(LNode));
 if(!s)
  exit(0);
 s->data=e;
 if(L->next=NULL)
 {
  L->next=s;
  s->next=NULL;
 }
 else
  {
  p=L->next;
     while(p!=NULL)
     {
         if(cmp(p->data,e)<=0 && cmp(p->next->data,e)>0)
      {
         s->next=p->next;
         p->next=s;
      break;
    }
    p=p->next;
     }
  }
 return 1;
}

int DelFirst(EventList &L,Event e)//删除事件链表中的第一个事件
{
 EventList p;
 p=L->next;
 if(p==NULL) return -1;
 L->next=p->next;
 e=p->data;
 free(p);
 return 1;
}

int ListEmpty(EventList L)//判断事件链表是否为空
{
 if(L->next=NULL)
  return 1;
 else return -1;
}

int InitQueue(LinkQueue &L)//初始化队列
{
 if(!(L.front=(Queueptr)malloc(sizeof(QNode))))
    exit(0);
 L.front=L.rear;
 L.front->next=NULL;
 return 1;
}

int EnQueue(LinkQueue &Q,QElemType e)//在队列中插入元素
{
 QNode *p;
 p=(QNode*)malloc(sizeof(QNode));
  if(!p)
     return 0;
 p->data=e;
 p->next=NULL;
 Q.rear->next=p;
 Q.rear=p;
 return 1;
}

int QueueEmpty(LinkQueue Q)//判断队列是否为空
{
 if(Q.front==Q.rear)
  return 1;
 else return 0;
}

int DelQueue(LinkQueue &Q,QElemType e)//删除队列的第一个元素
{
 if(Q.front==Q.rear)
  return -1;
 QNode *p;
 p=Q.front->next;
 e=p->data;
 Q.front->next=p->next;
 if(Q.rear==p) Q.front=Q.rear;
 free(p);
 return 1;
}

int GetHead(LinkQueue &Q,QElemType e)//得到队列的第一个元素
{
 if(Q.front==Q.rear)
  return -1;
 e=Q.front->next->data;
 return 1;
}
int QueueLength(LinkQueue Q)//计算队列的长度
{
 if(Q.front==Q.rear)
  return 0;
 QNode *p;
 int i=0;
 p=Q.front->next;
 while(p!=NULL)
 {
  p->next;
  i++;
 }
 return i;
}

EventList ev;
Event en;
LinkQueue q[5];
QElemType Customer;
int Totaltime;//总时间
int CustomerNum;//客户总人数
int Closetime;//银行关闭时间
int Minimum(LinkQueue *Q)//求人数最少的一队
{
 int e,j;
 int a[4];
 for(int i=1;i<=5;i++)
  a[i-1]=QueueLength(Q[i]);
 e=a[0];j=1;
 for(i=1;i<=3;i++)
 {
  if(e>a[i])
  {
   e=a[i];
      j=i+1;
  }
 }
 return j;
}

void OpenForDay()//初始化
{
 Totaltime=0;
 CustomerNum=0;
 InitList(ev);
 en.Occurtime=0;
 en.NType=0;
 OrderInsert(ev,en,cmp);
 for(int i=1;i<=4;i++)
  InitQueue(q[i]);
}

void random(int *a,int *b)
{
 *a=rand()%20+0;
 *b=rand()%5+0;
}
void CustomerArrival()
{
 ++CustomerNum;
 int durtime,intertime,t,i,b;
 random(&durtime,&intertime);
 b=en.Occurtime;
 t=en.Occurtime+intertime;
 if(t<Closetime)
 {
  en.Occurtime=t;en.NType=0;
  OrderInsert(ev,en,cmp);
 }
 i=Minimum(q);
 Customer.Arrivaltime=b;
 Customer.Duration=durtime;
 EnQueue(q[i],Customer);
 if(QueueLength(q[i])==1)
 {
  en.Occurtime=b+durtime;en.NType=i;
  OrderInsert(ev,en,cmp);
 }
}

void CustomerDeparture()
{
 int i;
 i=en.NType;
 DelQueue(q[i],Customer);
 Totaltime+=en.Occurtime-Customer.Arrivaltime;
 if(!QueueEmpty(q[i]))
 {
  GetHead(q[i],Customer);
   en.Occurtime=en.Occurtime+Customer.Duration;en.NType=i;
   OrderInsert(ev,en,cmp);
 }
}

void Bank_simulation()
{
 OpenForDay();
 while(!ListEmpty(ev))
 {
  DelFirst(ev,en);
  if(en.NType==0)
   CustomerArrival();
  else CustomerDeparture();
 }
 printf("The Average Time is %.2f/n:",(float)Totaltime/CustomerNum);
}

void main()
{
 //int Closetime;
 printf("输入银行关闭时间:");
 scanf("%d",&Closetime);
 Bank_simulation();
 printf("%d",Closetime);
}


 

原创粉丝点击