#ifndef QUEUE_H_INCLUDED
#define QUEUE_H_INCLUDED
#include <stdio.h>
#include <stdbool.h>
#define MAXSIZE 10
typedef struct
{
long arrive;
int processtime;
}Item;
typedef struct node
{
Item item;
struct node *next;
}Node;
typedef struct
{
Node *front;
Node *rear;
int items;
}Queue;
void InitializeQueue(Queue *pq);
bool QueueisEmpty(const Queue *pq);
bool QueueisFull(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool EnQueue(Queue *pq,Item item);
bool DeQueue(Item *pitem,Queue *pq);
void EmptyTheQueue(Queue *pq);
void InitializeQueue(Queue *pq)
{
pq->front=NULL;
pq->rear=NULL;
pq->items=0;
}
bool QueueisEmpty(const Queue *pq)
{
return pq->items==0;
}
bool QueueisFull(const Queue *pq)
{
return pq->items==MAXSIZE;
}
int QueueItemCount(const Queue *pq)
{
return pq->items;
}
bool EnQueue(Queue *pq,Item item)
{
if(QueueisFull(pq)) return false;
Node *pnew;
pnew=(Node *)malloc(sizeof(Node));
if(pnew==NULL)
{
fprintf(stderr,"Unable to allocate memory!\n");
exit(0);
}
if(QueueisEmpty(pq)) pq->front=pnew;
else pq->rear->next=pnew;
pnew->next=NULL;
pq->rear=pnew;
pq->items++;
pnew->item=item;
return true;
}
void CopyToNode(Item item,Node *p)
{
p->item=item;
}
bool DeQueue(Item *pitem,Queue *pq)
{
if(QueueisEmpty(pq)) return false;
*pitem=pq->front->item;
Node *psave;
psave=pq->front;
pq->front=pq->front->next;
free(psave);
pq->items--;
if(QueueisEmpty(pq))
pq->rear=NULL;
return true;
}
void EmptyTheQueue(Queue *pq)
{
Item ha;
while(!QueueisEmpty(pq))
{
DeQueue(&ha,pq);
}
}
#endif
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#include <time.h>
#define MIN_PER_HR 60.0
bool newcustomer(double x);
Item customertime(long when);
void InitializeQueue(Queue *pq);
bool QueueisEmpty(const Queue *pq);
bool QueueisFull(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool EnQueue(Queue *pq,Item item);
bool DeQueue(Item *pitem,Queue *pq);
void EmptyTheQueue(Queue *pq);
int main()
{
Queue line;
Item temp;
int hours;
int perhours;
long cycle,cyclelimit;
long turnaways=0;
long customers=0;
long served=0;
long sum_line=0;
long wait_time=0;
double min_per_cust;
long line_wait=0;
InitializeQueue(&line);
srand((unsigned int)time(0));
puts("请输入测试时长:");
scanf("%d",&hours);
puts("请输入每小时的平均顾客数:");
scanf("%d",&perhours);
cyclelimit=MIN_PER_HR*hours;
min_per_cust=MIN_PER_HR/perhours;
for(cycle=0;cycle<cyclelimit;cycle++)
{
if(newcustomer(min_per_cust))
{
if(QueueisFull(&line)) turnaways++;
else
{
temp=customertime(cycle);
EnQueue(&line,temp);
customers++;
}
}
if(wait_time<=0 && !QueueisEmpty(&line))
{
DeQueue(&temp,&line);
wait_time=temp.processtime;
line_wait+=cycle-temp.arrive;
served++;
}
if(wait_time>0) wait_time--;
sum_line+=QueueItemCount(&line);
}
if(customers>0)
{
printf("接收的顾客数:%ld\n",customers);
printf("服务的顾客数:%ld\n",served);
printf("拒收的顾客数:%ld\n",turnaways);
printf("队伍的平均长度为:%lf\n",(double)sum_line/cyclelimit);
printf("平均等待时长:%lf\n",(double)line_wait/served);
}
else
{
puts("糟糕至极没有顾客");
}
EmptyTheQueue(&line);
puts("程序完成!");
return 0;
}
bool newcustomer(double x)
{
if(rand()*x/RAND_MAX<1) return true;
else return false;
}
Item customertime(long when)
{
Item cust;
cust.processtime=rand()%3+1;
cust.arrive=when;
return cust;
}