C++学习日记之队列模拟

项目:
本文内容为c++primer 第十二章,编写一个ATM自动柜员机的排队模拟系统,主要知识内容是进一步了解类的使用。
多了不说,代码如下:
queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
class Customer
{
private:
	long arrive;//arrival time for customer
	int processtime;//processing time for customer
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;
	//preemptive definitiions to prevent public copying
	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

#include "queue.h"
#include<cstdlib>
Queue::Queue(int qs):qsize(qs)
{
	front=rear=nullptr;
	items=0;
}
Queue::~Queue()
{
	Node *temp;
	while(front!=nullptr)
	{
		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=nullptr;
	items++;
	if(front==nullptr)
		front=add;
	else
		rear->next=add;
	rear=add;
	return true;
}
bool Queue::dequeue(Item &item)
{
	if(front==nullptr)
		return false;
	item = front->item;
	items--;
	Node *temp=front;
	front = front->next;
	delete temp;
	if(items==0)
		rear=nullptr;
	return true;
}
void Customer::set(long when)
{
	processtime = rand()%3 +1;
	arrive = when;
}



main.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
#include"queue.h"
using namespace std;
const int MIN_PER_HR = 60;
bool newcustomer(double x);

int main()
{
	srand((unsigned int)time(0));
	cout << "case study:bank 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 hour:";
	double perhour;
	cin >> perhour;
	double min_per_cust;
	min_per_cust= MIN_PER_HR/perhour;
	Item temp;          //new customer data  
	long turnaways =0;  //turned away by full queue
	long customers= 0;  //joined the queue
	long served = 0;    //served during the simulation
	long sum_line = 0;  //cumulative line length
	int wait_time=0;    //time until autoteller is free
	long line_wait = 0;//cumulative time in line
	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 << "customer accepted : " << customers << endl;
		cout << "customer 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 customers!";
	cout << "done!"<<endl;
	return 0;
}
bool newcustomer(double x)
{
	return (rand()*x/RAND_MAX<1);
}


 
运行结果如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值