【C++ Primer Plus习题】12.6

1

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

在这里插入图片描述

解答:
main.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "queue.h"

using namespace std;

const int MIN_PER_HR = 60;
const int MIN_SIM_HOURS = 150;

bool newcustomer(double x);

int main()
{
	srand(time(0));
	cout << "研究案例:Heather银行自动柜员机\n";
	cout << "请输入队列的最大人数: ";
	int qs;
	cin >> qs;
	Queue line_one(qs);
	Queue line_two(qs);

	cout << "模拟时间要大于等于100." << endl;
	int hours = MIN_SIM_HOURS;//这里就固定为150;
	long cyclelimit = MIN_PER_HR * hours;

	double perhour = 0;
	double min_per_cust;
	Item temp;
	long turnaways;
	long customers, customers_one, customers_two;
	long served;
	long sum_line_one, sum_line_two;
	int wait_time_one, wait_time_two;
	long line_wait;
	double average_wait = 0;

	while (average_wait<1.0)
	{
		turnaways = 0;
		customers = customers_one = customers_two = 0;
		served = 0;
		sum_line_one = sum_line_two = 0;
		wait_time_one = wait_time_two = 0;
		line_wait = 0;
		perhour++;
		min_per_cust = MIN_PER_HR / perhour;
		while (!line_one.isempty())line_one.dequeue(temp);//下次进入前清空队列
		while (!line_two.isempty())line_two.dequeue(temp);//下次进入前清空队列

		for (int cycle = 0; cycle < cyclelimit; cycle++)
		{
			if (newcustomer(min_per_cust))
			{
				if (line_one.isfull()&&line_two.isfull())turnaways++;
				else
				{
					customers++;
					temp.set(cycle);
					if (line_one.queuecount() < line_two.queuecount())
					{
						line_one.enqueue(temp);
						customers_one++;
					}
					else
					{
						line_two.enqueue(temp);
						customers_two++;
					}
				}
			}
			if (wait_time_one <= 0 && !line_one.isempty())
			{
				line_one.dequeue(temp);
				wait_time_one = temp.ptime();
				line_wait += cycle - temp.when();
				served++;
			}
			if (wait_time_two <= 0 && !line_two.isempty())
			{
				line_two.dequeue(temp);
				wait_time_two = temp.ptime();
				line_wait += cycle - temp.when();
				served++;
			}
			
			if (wait_time_one > 0)wait_time_one--;
			if (wait_time_two > 0)wait_time_two--;
			sum_line_one += line_one.queuecount();
			sum_line_two += line_two.queuecount();

		}

		average_wait = (double)line_wait / served;
		if (average_wait < 1)
		{
			if (customers > 0)
			{
				cout << "接受顾客数为:" << customers << endl;
				cout << "服务顾客数为:" << served << endl;
				cout << "turnaways:" << turnaways << endl;
				cout << "平均每分钟的队列长度: ";
				cout.precision(2);
				cout.setf(ios_base::fixed, ios_base::floatfield);
				cout << (double)(sum_line_one+sum_line_two) / cyclelimit << endl;
				cout << "每个顾客平均排队等待时间: " << average_wait << " 分钟\n";
			}
			else
				cout << "No customers!\n";
			cout << "The average " << perhour << " of arrival per hour,and average wait time is" << average_wait << endl<<endl;
		}

	}

	cout << "Done!" <<endl;
	return 0;
}

bool newcustomer(double x)
{
	return (rand() * x / RAND_MAX < 1);
}

queue.h

#pragma once
class Customer
{
private:
	long arrive;//顾客到达的时间
	int processtime;//顾客办事的时间
public:
	Customer() { arrive = processtime = 0; }

	void set(long when);
	long when()const { return arrive; }
	int ptime()const { return processtime; }
};

typedef Customer Item;

class Queue
{
private:
	struct Node { Item item; struct Node* next; };
	enum {Q_SIZE=10};

	Node* front;
	Node* rear;
	int items;
	const int qsize;
	Queue(const Queue&q):qsize(0){}
	Queue& operator=(const Queue& q) { return *this; }

public:
	Queue(int qs=Q_SIZE);
	~Queue();
	bool isempty()const;
	bool isfull()const;
	int queuecount()const;
	bool enqueue(const Item& item);
	bool dequeue(Item& item);
};




queue.cpp

#include "queue.h"
#include <cstdlib>


Queue::Queue(int qs):qsize(qs)
{
	front = rear = NULL;
	items = 0;
}
Queue::~Queue()
{
	Node* temp;
	while (front!=NULL)
	{
		temp = front;
		front = front->next;
		delete temp;
	}
}
bool Queue::isempty()const
{
	return items == 0;
}
bool Queue::isfull()const
{
	return items == qsize;
}
int Queue::queuecount()const
{
	return items;
}
bool Queue::enqueue(const Item& item)
{
	if (isfull())return false;
	Node* node = new Node;
	node->item = item;
	node->next = NULL;
	items++;
	if (front == NULL)
	{
		front = node;
	}
	else
	{
		rear->next = node;
	}
	rear = node;
	return true;
}
bool Queue::dequeue(Item& item)
{
	if (isempty())return false;
	item = front->item;
	items--;
	Node* temp = front;
	front = front->next;
	delete temp;
	if (items == 0)
	{
		rear == NULL;
	}
	return true;
}

void Customer::set(long when)
{
	processtime = rand() % 3 + 1;
	arrive = when;
}

运行结果:
在这里插入图片描述

考查点:

  • 自己看吧…

2024年9月8日16:21:37

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值