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

📄 源代码.txt

📁 数据结构作业.用C语言写的模拟理发馆理发排队队列问题.
💻 TXT
字号:
                                                                                                                                                                         /*题目内容:以理发馆的运作情况为模型,讨论排队问题的系统仿真 */

#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 N1;                                                  /*可用椅子数*/
int T,N;                                                /*输入的开始营业时间,椅子总数*/


typedef struct customer
  {
    int NO;                                            /*进门顾客的编号*/
    int durtime;                                   /*理发所需时间*/
    int intime;                                    /*进入理发店时间,假设第一个顾客光临时间为0*/
    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=1;
   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);
   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;
     N1--;
     cus[n].serve_flag=TRUE;
     cus[n].wait_flag=FALSE;
     totaltime+=cus[n].durtime;

}

void customer_in()                                          /*顾客进入理发店*/
{
                                                                     /*  每次产生不同的随机数,顾客进门时产生*/

totalnum++;
   cus[totalnum].NO=totalnum;
   cus[totalnum].intime=curtime;                   /*记录顾客进入时间,为当前时间*/
   cus[totalnum].durtime=15+R%50;            /*产生随机数,记录顾客理发所需时间*/
   cus[totalnum].interval=2+R%10;             /*此顾客的下一个顾客来的时间间隔*/
   if(QueueEmpty(W)&&N1>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;
  N1++;
}

void  printresult()                                       /*输出结果*/
{
  int i;
  float aver_serve_time,aver_wait_len;          /*顾客平均等待时间,顾客平均等待长度*/
  aver_serve_time=totaltime/totalnum;
  aver_wait_len=wait_length/totalnum;
  printf("\n\n************You can refer to the following information :************\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                                   \n",i-1);
  printf("\n*  The average time is: %6.2f                                \n",aver_serve_time);
  printf("\n*  The average waiting length is : %6.2f                    \n",aver_wait_len);
  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 :");
  scanf("%d",&N);
  N1=N;
  printf("\nPlease input the very clock you want to start to  run such as 8 clock?:  ");
  scanf("%d",&start);
  printf("\nPlease input the total running time:  ");
  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(N1>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(N1>0&&!QueueEmpty(W))                /*让等待队列中的人去理发*/
       customer_serve(DeQueue(W));
    }
  printresult();
   
  } 

⌨️ 快捷键说明

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