2016.09.29 mall.c

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值