C++primer plus第六版课后编程题答案12.5

myQueue.cpp

#include <iostream>
using namespace std;
class Customer
{
private:
	long arrive;
	int processtime;
public:
	Customer(){arrive=processtime=0;};
	void set(long when){
		processtime=rand()%3+1;
		arrive=when;
	}
	long when()const{
		return arrive;
	}
	int ptime()
	{
		return processtime;
	}
	friend ostream&operator<<(ostream &os,const Customer &c)//用于检测
	{
		static int j=0;
		os<<j++<<"  arrive:"<<c.arrive<<"    need time:"<<c.processtime<<endl;
		return os;
	}
};

typedef Customer Item;
class Queue{
	struct Node{
		Item items;
		struct Node *next;
	};
	enum {Q_SIZE=10};
	Node *front;
	Node *rear;
	int items;//当前队列人数
	const int qsize;//队列最大值?const??一次性??
	Queue(const Queue &q):qsize(0){ };
	Queue &operator=(const Queue &q){return *this;};//???真看不太懂啊
public:
	int s()
	{return items;};
	Queue(int qs=Q_SIZE):qsize(qs)
	{
		front=rear=nullptr;
		items=0;
	}
	~Queue()
	{
		Node *temp;
		while(front!=nullptr)
		{
			temp=front;
			front=front->next;
			delete temp;
		}
	}
	bool isEmpty()const
	{
	  return items==0;
	}
	bool isFull()const
	{
		return items==qsize;//qsize是这一个队列的最大长度
	}
	int queuecount()const
	{
		return items;
	}
	bool enQueue(const Item &it)
	{
		if(isFull())
				//cout<<"The queue is full!"<<endl;
			return false;
		Node *add=new Node;
		add->items=it;
		add->next=nullptr;//这一句??//为后面作准备
		items++;
		if(front==nullptr)//如果在头部插入
		{
			front=add;
		}
		else
			rear->next=add;
		rear=add;//令rear指向最后一个元素
		return true;
	}
	bool deQueue(Item &it)
	{
		if(front==nullptr)
			//cout<<"The queue is empty!"<<endl;
			return false;
		it=front->items;
		items--;//这里居然忘记写了,我去啊,难怪只会进不会出
		Node *temp=front;
		front=front->next;
		delete temp;
		if(items==0)
			rear=nullptr;
		return true;
	}
	friend ostream&operator<<(ostream &os,const Queue &q)
	{
		auto *p=q.front;//真神奇,用auto可以,用Node 居然会报错??
		while(p++!=q.rear)
			cout<<p->items;
		return os;
	}
};

main125.cpp

#include <iostream>
#include "myQueue.cpp"
#include <cstdlib>
#include <ctime>
using namespace std;
const int MIN_PER_HR=60;
bool newCustomer(double x)//每隔x次,rand()/RAND_max会有一次值<1
{
	return rand()*x/RAND_MAX<1;
}
void main125()
{
	srand(time(0));//初始化rand();
	cout<<"Case Study:Bank of Heather Automatic Teller"<<endl;
	cout<<"Enter maximum size of queue:";
	int qs;
	cin>>qs;
	Queue line(qs);

	cout<<"Enter the number of simulation hours:";
	int hours;
	cin>>hours;
	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 served=0;
	long sum_line=0;
	int wait_time=0;
	long line_wait=0;

	for(int cycle=0;cycle<cyclelimit;cycle++)//cycle每循环一次,代表过了一分钟
	{
		//line.s();
		if(newCustomer(min_per_cust))
		{
			if(line.isFull())
				turnaways++;//拒绝服务???//恩,应该就是拒绝服务的人数
			else
			{
				customers++;//队列人数//应该是顾客人数+1而不是队列人数+1
				temp.set(cycle);//cycle是到达时间
				line.enQueue(temp);//这里的items++才是队列人数+1
				//cout<<"after insert "<<line;//test

			}

		}
		if(wait_time<=0&&!line.isEmpty())//用户处理完了业务
		{
			line.deQueue(temp);
			//cout<<"after the delete"<<line;//test
			wait_time=temp.ptime();//wait_time是该客户处理业务所用时间
			line_wait+=cycle-temp.when();//cycle-temp.when();是该客户一共在队列中等了多久
			//一开始wait_time初始化是0,然后进入这里之后,又重新设置了使其=processtime
			//line_wait是该队列一共等了多久??哦,应该是所有客户的等待时间
			//line_wait是客户等待总时间
			served++;//服务人数+1
		}

		if(wait_time>0)//正在处理业务的处理时间-1,因为过了一分钟
			wait_time--;//话说这wait_time是谁设置了?一直=0?
		//wait_time=temp.ptime()这里随机设置了wait-time
		sum_line+=line.queuecount();//sum_line又是神马东东??
		//sum_line是队伍总长度,每分钟计算一次队伍长度
	}

	if(customers>0)
	{
		cout<<"customers accepted:"<<customers<<endl;
		cout<<"		customers 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"<<endl;
	}
	else
		cout<<"No coustomer!"<<endl;
	cout<<"Done!"<<endl;
	system("pause");
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值