c++primer plus第12章6题,Heather银行想知道,如果在开设一台ATM

#include"Queue.h"
#include<ctime>
#include<cstdlib>
const int MIN_PER_HR = 60;
bool newcustomer(double x)
{
	return (rand()*x / RAND_MAX < 1);
}
int main()
{
	srand(time(0));
	cout << "CaseStudy: Bank of Heather Automatic Teller\n";
	cout << "Enter maximum size if queue: ";
	int qs;
	cin >> qs;
	Queue line1(qs);
	Queue line2(qs);
	cout << "Enter ther number of simulation hours: ";
	int hours;
	cin >> hours;
	long cyclelimit = MIN_PER_HR*hours;
	cout << "Enter the number of simulaters 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_line1 = 0;//总线长
	long sum_line2 = 0;
	int wait_time1 = 0;//1等的时间
	int wait_time2 = 0;
	long line_wait1 = 0;//总等待时间
	long line_wait2 = 0;
	for (int cycle = 0; cycle < cyclelimit; cycle++)
	{
		if (newcustomer(min_per_cust))
		{
			if (line1.isfull())
			{
				if (line2.isfull())
				{
					turnaways++;
				}
			}
			else
			{
				customers++;
				temp.set(cycle);
				if (line1.queuecount() <= line2.queuecount())
				{
					line1.enqueue(temp);
				}
				else
				{
					line2.enqueue(temp);
				}
			}
		}
			if (wait_time1 <= 0 && !line1.isempty())
			{
				line1.dequeue(temp);
				wait_time1 = temp.ptime();
				line_wait1 += cycle - temp.when();
				served++;
			}
			if (wait_time2 <= 0 && !line2.isempty())
			{
				line2.dequeue(temp);
				wait_time2 = temp.ptime();
				line_wait2 += cycle - temp.when();
				served++;
			}
			if (wait_time1 > 0)
			{
				wait_time1--;
			}
			if (wait_time1 > 0)
			{
				wait_time2--;
			}
			sum_line1 += line1.queuecount();
			sum_line2 += line2.queuecount();
		}
		if (customers > 0)
		{
			int line_wait = line_wait1 + line_wait2;
			cout << "customers accepted: " << customers << endl;
			cout << "    customers served:  " << served << endl;
			cout << "               turnaways:  " << turnaways << endl;
			cout << " awerage queue size:  ";
			cout.precision(2);
			cout.setf(ios_base::fixed, ios_base::floatfield);
			cout << (double)sum_line1 / cyclelimit << endl;
			cout << (double)sum_line2 / cyclelimit << endl;
			cout << " average wait time:  " << (double)line_wait / served << " minutes\n";
		}
		else
		{
			cout << " No customers!\n";
		}
		cout << "Done!\n";
		system("pause");
		return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值