C++ Prime Plus 学习笔记028

书籍:C++ Primer Plus (第六版)(中文版)

工具:Dev-C++ 5.11

电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz

系统类型:64位操作系统,基于X64的处理器 windows10 专业版

第12章 类和动态内存分配

12.6 复习各种技术

12.7 队列模拟

实例12.6

queue.h

cpp 复制代码
#ifndef QUEUE_H_
#define QUEUE_H_

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);
 };
 
#endif

queue.cpp

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 *add = new Node;
	add->item=item;
	add->next=NULL;
	items++;
	if(front == NULL)
	{
		front =add;
	}
	else
	{
		rear->next =add;
	}
	rear=add;
	return true;
}

bool Queue::dequeue(Item &item) 
{
	if(front == NULL)
		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 = std::rand()%3 + 1;
	arrive = when;
}

bank.cpp

cpp 复制代码
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "queue.h"
const int MIN_PER_HR = 60;
bool newcustomer(double x);

int main()
{
	using std::cin;
	using std::cout;
	using std::endl;
	using std::ios_base;
	std::srand(std::time(0));
	
	cout<<"Case study: Bamk of Heather Automatic Teller\n";
	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 hours: ";
	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;
	long line_wait=0;
	
	for(int cycle=0;cycle<cyclelimit;cycle++)
	{
		if(newcustomer(min_per_cust))
		{
			if(line.isfull())
				turnaways++;
			else
				{
					customers++;
					temp.set(cycle);
					line.enqueue(temp);
				}			
		}
		if(wait_time<=0 && !line.isempty())
		{
			line.dequeue(temp);
			wait_time=temp.ptime();
			line_wait+=cycle-temp.when();
			served++;
		}
		if(wait_time>0)
			wait_time--;
		sum_line += line.queuecount();
	}
	
	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\n";		
	}
	else
		cout<<"No customers!\n";
	cout<<"Done!\n";
	
	return 0;	
}

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

编译运行结果:

cpp 复制代码
Case study: Bamk of Heather Automatic Teller
Enter maximum size of queue: 10
Enter the number of simulation hours: 100
Enter the average number of customers per hours: 15
Customers accepted: 1472
Customers served: 1471
           turnaways: 0
average queue size: 0.14
 average wait time: 0.58 minutes
Done!

--------------------------------
Process exited after 34.8 seconds with return value 0
请按任意键继续. . .
相关推荐
ACP广源盛139246256732 小时前
Type-C 扩展坞方案选型笔记:DP1.4 转 HDMI2.0 桥接方案评估@ACP
笔记·硬件架构·hdmi·国产芯片·扩展坞
Canmag 兴隆磁性2 小时前
中大型钕铁硼产品的磁通测量与自动分选:一条产线自动化的工程实现思路
运维·学习·自动化·永磁材料
ss2734 小时前
AI时代首先数据库-PostgreSQL实战笔记 | 22 PostgreSQL实例与集群
数据库·笔记·postgresql
ss2734 小时前
AI时代首先数据库-PostgreSQL实战笔记 | 21 PostgreSQL进程与内存架构介绍
数据库·笔记·postgresql
小黄人软件5 小时前
桌面需要整理吗?有工具自动分类
学习
老衲不用@飘柔5 小时前
SFP与SFP+学习知识整理
学习
X54先生(人文科技)5 小时前
社会嵌入式终身学习机器与修复时代-ELR架构和技术演化探讨
人工智能·深度学习·学习·架构·开源协议
电子云与长程纠缠6 小时前
UE5魂系游戏开发教程05
开发语言·学习·游戏·ui·ue5
syagain_zsx6 小时前
BRpc 二次封装:Channel 管理、服务管理与回调工厂
c++·学习·程序人生·rpc·谷歌
其实防守也摸鱼6 小时前
Cylab在线靶场——CTF中的通用技能
linux·数据库·学习·算法·靶场·ctf·学习网络安全