C Primer Plus(第六版)17.12 编程练习 第4题

// mall.c -- use the Queue interface

// compile with queue.c

#include <stdio.h>

#include <stdlib.h> // for rand() and srand()

#include <time.h> // for time()

#include "queue.h" // change Item typedef

#define MIN_PER_HR 60.0

bool newcustomer(double x); // is there a new customer?

Item customertime(long when); // set customer parameters

int main(void)

{

Queue line,line1;

Item temp; // new customer data

int hours; // hours of simulation

int perhour; // average # of arrivals per hour

long cycle, cyclelimit; // loop counter, limit

long turnaways = 0; // turned away by full queue

long customers = 0; // joined the queue

long served = 0,served1 = 0; // served during the simulation

long sum_line = 0,sum_line1 = 0; // cumulative line length

int wait_time = 0,wait_time1 = 0; // time until Sigmund is free

double min_per_cust; // average time between arrivals

long line_wait = 0,line_wait1 = 0; // cumulative time in line

InitializeQueue(&line);

InitializeQueue(&line1);

srand((unsigned int) time(0)); // random initializing of rand()

puts("Case Study: Sigmund Lander's Advice Booth");

puts("Enter the number of simulation hours:");

scanf("%d", &hours);

cyclelimit = MIN_PER_HR * hours;

puts("Enter the average number of customers per hour:");

scanf("%d", &perhour);

min_per_cust = MIN_PER_HR / perhour;

for (cycle = 0; cycle < cyclelimit; cycle++)

{

if (newcustomer(min_per_cust))

{

if (QueueIsFull(&line)&&QueueIsFull(&line1))

turnaways++;

else

{

customers++;

temp = customertime(cycle);

if (QueueItemCount(&line) < QueueItemCount(&line1))

EnQueue(temp, &line);

else

EnQueue(temp, &line1);

}

}

if (wait_time <= 0 && !QueueIsEmpty(&line))

{

DeQueue (&temp, &line);

wait_time = temp.processtime;

line_wait += cycle - temp.arrive;

served++;

}

if (wait_time1 <= 0 && !QueueIsEmpty(&line1))

{

DeQueue (&temp, &line1);

wait_time1 = temp.processtime;

line_wait1 += cycle - temp.arrive;

served1++;

}

if (wait_time > 0)

wait_time--;

if (wait_time1 > 0)

wait_time1--;

sum_line += QueueItemCount(&line);

sum_line1 += QueueItemCount(&line1);

}

if (customers > 0)

{

printf("customers accepted: %ld\n", customers);

printf(" turnaways: %ld\n", turnaways);

printf(" line:\n");

printf(" customers served: %ld\n", served);

printf("average queue size: %.2f\n",(double) sum_line / cyclelimit);

printf(" average wait time: %.2f minutes\n",(double) line_wait / served);

printf(" line1:\n");

printf(" customers served1: %ld\n", served1);

printf("average queue1 size: %.2f\n",(double) sum_line1 / cyclelimit);

printf(" average wait time1: %.2f minutes\n",(double) line_wait1 / served1);

}

else

puts("No customers!");

EmptyTheQueue(&line);

puts("Bye!");

return 0;

}

// x = average time, in minutes, between customers

// return value is true if customer shows up this minute

bool newcustomer(double x)

{

if (rand() * x / RAND_MAX < 1)

return true;

else

return false;

}

// when is the time at which the customer arrives

// function returns an Item structure with the arrival time

// set to when and the processing time set to a random value

// in the range 1 - 3

Item customertime(long when)

{

Item cust;

cust.processtime = rand() % 3 + 1;

cust.arrive = when;

return cust;

}

相关推荐
以后不吃煲仔饭1 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师2 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
前端拾光者6 分钟前
利用D3.js实现数据可视化的简单示例
开发语言·javascript·信息可视化
The_Ticker7 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
程序猿阿伟8 分钟前
《C++ 实现区块链:区块时间戳的存储与验证机制解析》
开发语言·c++·区块链
傻啦嘿哟26 分钟前
如何使用 Python 开发一个简单的文本数据转换为 Excel 工具
开发语言·python·excel
大数据编程之光30 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
初九之潜龙勿用30 分钟前
C#校验画布签名图片是否为空白
开发语言·ui·c#·.net
爪哇学长43 分钟前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法
爱摸鱼的孔乙己1 小时前
【数据结构】链表(leetcode)
c语言·数据结构·c++·链表·csdn