⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 理发馆的排队.txt

📁 使用队列模拟理发馆的排队现象
💻 TXT
字号:
/*
*题目内容:使用队列模拟理发馆的排队现象,通过仿真手法评估其营业状况。
*copyright@Beggarson 2004---2006
*
*/

#include <stdlib.h>   /*头文件*/
#include <math.h>
#include <stdio.h>
#define MAX 1000
#define ERROR 0
#define OVERFLOW
#define TRUE 1
#define NULL 0
#define FALSE 0
#define status int
#define R rand()

float wait_length;                           /*等待总长度*/
float totalnum;                                /*总共顾客数*/
float totaltime;                                /*顾客理发所需总时间*/
int start;                                           /*开张时间*/
int curtime;                                    /*当前时间*/
int N_valid;                                    /*可用椅子数*/
int T,N;


typedef struct customer
  {
    int NO;                       /*编号*/
    int durtime;                  /*理发所需时间*/
    int intime;                  /*进入理发店时间*/
    int starttime;               /*开始理发时间*/
    int interval;                 /*他的下一个人到来的时间间隔*/
    int wait_flag,serve_flag;     /*是否在等待,是否在理发*/
    }customer;
customer cus[MAX];

typedef struct Qnode{
   int num;
   struct Qnode *next;
}Qnode,*QueuePtr;
typedef struct{
  QueuePtr front;
  QueuePtr rear;
}LinkQueue;
LinkQueue *W;             /*等待队列*/

void InitQueue(LinkQueue *Q)          /*等待队列初始化*/
{
   Q->front=Q->rear=(QueuePtr)malloc(sizeof(Qnode));
   Q->front->next=NULL;
}

status Queue_Length(LinkQueue *Q)          /*求等待队列的当前长度*/
{
   Qnode *p;
   int length;
   length=0;
   p=Q->front->next;
   while (p!=NULL)
     {
       length++;
       p=p->next;
     }
   return length;
}

void EnQueue(LinkQueue *Q,int e)               /*入队*/
{
   QueuePtr p;
   wait_length+=Queue_Length(W)+1;
   p=(QueuePtr)malloc(sizeof(Qnode));
   p->num=e;
   p->next=NULL;
   Q->rear->next=p;
   Q->rear=p;
   }

status DeQueue(LinkQueue *Q)                        /*出队*/
{
   Qnode *p;
   int e;
   p=Q->front->next;
   e=p->num;
   Q->front->next=p->next;
   if(Q->rear==p)
       Q->rear=Q->front;
   free(p);
   return e;
  }

status QueueEmpty(LinkQueue *Q)                    /*判断等待队列是否为空*/
{
   return(Q->front==Q->rear? TRUE:FALSE);
}


void customer_serve(int n)                          /*为顾客理发*/
{
     cus[n].starttime=curtime;
     N_valid--;
     cus[n].serve_flag=TRUE;
     cus[n].wait_flag=FALSE;
     totaltime+=cus[n].durtime;

}

void customer_in()                       /*顾客进入理发店*/
{
   /*  srand(time(0));                    每次产生不同的随机数*/
   totalnum++;
   cus[totalnum].NO=totalnum;
   cus[totalnum].intime=curtime;               /*记录顾客进入时间*/
   cus[totalnum].durtime=15+R%50;              /*产生随机数记录顾客理发所需时间*/
   cus[totalnum].interval=2+R%10;                /*此顾客的下一个顾客来的时间间隔*/
   if(QueueEmpty(W)&&N_valid>0)
         customer_serve(totalnum);                  /*有空闲位置并无人参与竞争,调用服务函数*/
   else
       {
         cus[totalnum].wait_flag=TRUE;                /*否则入队等待*/
         EnQueue(W,cus[totalnum].NO);
       }

}

void customer_leave(int n)                   /*顾客离开理发店*/
{
  cus[n].serve_flag=FALSE;
  N_valid++;
}

void list()                        /*打印所有值*/
{
  int i;
  float aver_serve_time,aver_wait_len;          /*顾客平均等待时间,顾客平均等待长度*/
  aver_serve_time=totaltime/totalnum;
  aver_wait_len=wait_length/totalnum;
  printf("\n\n&&&&&&&&&&There are all the information you want :&&&&&&&&&&&&&\n");
  printf("    NO      in       start       durtime       leave");
  for(i=1;i<=totalnum;i++)
         printf("\n%5d%9d%12d%12d%12d",cus[i].NO,cus[i].intime+start,cus[i].starttime+start,cus[i].durtime,cus[i].starttime+cus[i].durtime+start);
  printf("\n***************************************************************");
  printf("\n*                                                             *");
  printf("\n*  The total number is : %2d                                   *",i-1);
  printf("\n*  The average time is: %6.2f                                *",aver_serve_time);
  printf("\n*  The average waiting length is : %6.2f                     *",aver_wait_len);
  printf("\n*                                                             *");
  printf("\n***************************************************************\n");
  return;
}


main()
{
  int i;
  curtime=0;
  totaltime=0,totalnum=0,wait_length=0;
  printf("Please input the total number of the chairs :\n");
  scanf("%d",&N);
  N_valid=N;
  printf("Please input the very clock you want to start to  run such as 8 clock?:\n");
  scanf("%d",&start);
  printf("Please input the total running time:\n");
  scanf("%d",&T);
  InitQueue(W);
  customer_in();
  while(curtime<T)         /*当前时间属于营业时间,允许顾客进入、为顾客服务*/
    {
     curtime++;                      /*当前时间*/
     for(i=1;i<=totalnum;i++)
       {                                             /*判断有没有人离开*/
     if((cus[i].serve_flag==TRUE)&&(cus[i].starttime+cus[i].durtime==curtime))
             customer_leave(i);
       }
     while(N_valid>0&&!QueueEmpty(W))          /*让等待队列中的人去理发*/
       customer_serve(DeQueue(W));
                        /*判断是否有人符合要进的条件*/
     if((cus[totalnum].intime+cus[totalnum].interval)==curtime)
        customer_in();

     }
   while(!QueueEmpty(W))   /*   为等待的顾客服务,但 不允许顾客进来了*/
    {
     curtime++;
     for(i=1;i<=totalnum;i++)
       {                                             /*判断有没有人离开*/
     if((cus[i].serve_flag==TRUE)&&(cus[i].starttime+cus[i].durtime==curtime))
             customer_leave(i);
        }
     while(N_valid>0&&!QueueEmpty(W))                /*让等待队列中的人去理发*/
       customer_serve(DeQueue(W));
    }
   list();
   getch();
  } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -