C++ primer plus 编程练习12.6

bank.cpp:

#include 
#include  // for rand() and srand)#include  // for time()
#include"queue.h"
const int MIN_PER_HR = 60;
bool newcustomer(double x); // is there a new customer?
int main() {
	using std::cin;
	using std::cout;
	using std::endl;
	using std::ios_base;
	// setting things up
	std::srand(std::time(0));
	cout << "Case Study: Bank of Heather Automatic Teller\n";
	cout << "Enter maximum gize of queue: ";
	int qs;
	cin >> qs;
	Queue line(qs);
	Queue line2(qs);
	// line queue holds up to gs people
	cout << "Enter the number of simulation hours:";
	int hours;// hours of simulation
	cin >> hours;// simulation will run 1 cycle per minute
	long cyclelimit = MIN_PER_HR * hours;//循环次数

	cout << "Enter the average number of customers per hour: ";
	double perhour;

	cin >> perhour;
	double min_per_cust;   //隔多少时间来一个客户
	min_per_cust = MIN_PER_HR / perhour;

	Item temp;
	long turnaways = 0;
	long customers = 0;
	long served = 0;
	long sum_line = 0;
	int wait_time = 0;
	int wait_time2 = 0;
	long line_wait = 0;
	do
	{
		while (!line.isempty())
		{
			line.dequeue(temp);
		}
		while (!line2.isempty())
		{
			line2.dequeue(temp);
		}
		
		for (int cycle = 0; cycle < cyclelimit; cycle++) {
			if (newcustomer(min_per_cust)) {
				if (line.isfull() && line2.isfull())
					turnaways++;//队满了人就离开;
				else {
					customers++;
					temp.set(cycle);
					if (line.queuecount() <= line2.queuecount())
						line.enqueue(temp);
					else line2.enqueue(temp);
				}
			}
			if (!line.isempty() && wait_time == 0) {
				line.dequeue(temp);
				wait_time = temp.ptime(); //wait_time = 前一个用户的processtime
				line_wait += cycle - temp.when();//总等待时间 = 所有人的等待时间相加,个人的等待时间 = 出队时间 - 到达时间
				served++;
			}
			if (!line2.isempty() && wait_time2 == 0) {
				line2.dequeue(temp);
				wait_time2 = temp.ptime(); //wait_time = 前一个用户的processtime
				line_wait += cycle - temp.when();//总等待时间 = 所有人的等待时间相加,个人的等待时间 = 出队时间 - 到达时间
				served++;
			}
			if (wait_time > 0)
				wait_time--;
			if (wait_time2 > 0)
				wait_time2--;
		}
		perhour++;
		min_per_cust = MIN_PER_HR / perhour;
	} while ((double)line_wait / served <= 1.0);

	//while ((double)line_wait / served <= 1);served == 1000
	if (customers > 0) {
		cout << "customers accepted:  " << customers << endl;
		cout << "customerg served :" << served << endl;
		cout << "turnaways: " << turnaways << endl;
		cout << "average queue size : ";
		cout.precision(2);
		cout.setf(ios_base::fixed, ios_base::floatfield);
		cout << (double)sum_line / cyclelimit << endl;
		cout << " average wait time :" << (double)line_wait / served << " minutes\n";
		cout << "per_customer_count: " << perhour << endl;
	}
	else cout << "no customer!\n";
	cout << "Done!\n";

	return 0;

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

每次在进入for循环之前,需要清空队列,否则可能陷入死循环:

while (!line.isempty())
		{
			line.dequeue(temp);
		}
		while (!line2.isempty())
		{
			line2.dequeue(temp);
		}

参考链接:C++ Primer Plus(第六版)编程练习答案 第12章 类和动态内存分配_Leonardo Liu的博客-CSDN博客

你可能感兴趣的:(c++,开发语言)