#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"queue.h"
#define MIN_PRE_HR 60.0
/*有新顾客到来吗?*/
bool newcustomer(double x);
/*设置顾客参量*/
Item customertime(long when);
int main()
{
Queue line;
Item temp; /*关于顾客的新数据 */
int hours; /*模拟的小时数 */
int perhour; /*每小时顾客的平均数 */
long cycle, cyclelimt; /*循环计数器,计数器上界 */
long turnaways = 0; /*因队列满被拒绝的客户数 */
long customers = 0; /*加入对列的顾客数 */
long served = 0; /*得到服务的顾客数 */
long sum_line = 0; /*累计的队列长度 */
int wait_time = 0; /*当前到sigmund空闲的时间 */
double min_per_cust; /*顾客到来的平均时间间隔 */
long line_wait = 0; /*队列累计等待时间 */
InitializeQueue(&line);
srand(time(0));
puts("Case Study: Sigmund Lander's Advice Booth");
puts("Enter the number of simulation hours:");
scanf("%d", &hours);
cyclelimt = MIN_PRE_HR * hours;
puts("Enter the average number of customers per hour:");
scanf("%d", &perhour);
min_per_cust = MIN_PRE_HR / perhour;
for (cycle = 0; cycle < cyclelimt; cycle++)
{
if (newcustomer(min_per_cust))
{
if (QueueIsFull(&line))
turnaways++;
else
{
customers++;
temp = customertime(cycle);
EnQueue(temp, &line);
}
}
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("customers accepted: %ld\n", customers);
printf(" customers served: %ld\n", served);
printf(" turnaways: %ld\n", turnaways);
printf("average queue size: %.2f\n", (double)sum_line / cyclelimt);
printf("average wait time: %.2f minutes\n", (double)line_wait / served);
}
else
puts("No customers!");
EmptyTheQueue(&line);
getchar();
getchar();
return 0;
}
/* x是顾客到来的平均时间间隔(以秒计) */
/* 如果这1分钟有顾客到来,则返回true */
bool newcustomer(double x)
{
if (rand() * x / RAND_MAX < 1)
return true;
else
return false;
}
/* when是顾客到来的时间 */
/* 函数返回一个Item结构,该结构的顾客到来时间设置为when */
/* 需要的咨询时间设置为一个范围在1到3之间的随机值 */
Item customertime(long when)
{
Item cust;
cust.processtime = rand() % 3 + 1;
cust.arrive = when;
return cust;
}
2016.09.29 mall.c
最新推荐文章于 2024-11-13 15:16:02 发布
本文介绍了一个使用C语言实现的简单模拟排队系统的程序。该程序通过随机生成顾客到达时间和所需服务时间来模拟真实世界的排队情况,并计算平均队列长度及平均等待时间等统计数据。
1236

被折叠的 条评论
为什么被折叠?



