C++对于ATM机的队列模拟

通过设置简单的队列模型,应用到ATM机上去模拟一些数据的获取,详细的代码如下:

//description.h
#ifndef _DESCRIPTION_H_
#define _DESCRIPTION_H_
#include<cstdlib>
const int MAX_QUEUE = 100;
using namespace std;
class Customer
{
private:
	long processtime;
	long  begin_time;
public:
	Customer(){ processtime = 0; begin_time = 0; }
	~Customer(){}
	void set(long t);
	long get_bt(){ return begin_time; }
	long get_pt(){ return processtime; }
};

typedef Customer Item;
class Queue
{
private:
	struct Node
	{
		Item item;
		Node *next;
	};
	Node *front, *rear;
	int num;
	const int Max_num;
	Queue(const Queue &q):Max_num(0){}
	Queue & operator=(const Queue &q){ return *this; }
public:
	Queue(int Size=MAX_QUEUE);
	~Queue();
	bool isempty() const { return num==0; }
	bool isfull()const { return num == Max_num; }
	int get_num() const { return num; }
	bool pushQ(const Item&);
	bool popQ(Item&);
};
#endif //_DESCRIPTION_H_


//define.cpp
/**
*Copyright U+00A9 2018 XXXXX. All Right Reserved.
*Filename:
*Author:XXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/

#include<iostream>
//#include<cstdlib>
#include"description.h"
using namespace std;
void Customer::set(long t)
{
	processtime = rand() % 3 + 1;//随机生成1~3之间的数,代表每个顾客的处理时间
	begin_time = t;
}

Queue::Queue(int Size):Max_num(Size)//对于数据成员为引用&或者非静态const的必须利用初始化列表
{
	front = rear = nullptr;
	num = 0;
}

Queue::~Queue()//将剩余队列中的数据删除,释放内存空间
{
	Node *temp;
	while (front != nullptr)
	{
		temp = front;
		front = front->next;
		delete temp;
	}
	cout << "The rest object is erased!" << endl;
}


bool Queue::pushQ(const Item &item)
{
	if (isfull()) return false;
	Node *temp = new Node;
	temp->item = item;
	temp->next = nullptr;
	if (front == nullptr)
		front = temp;
	else
		rear->next = temp;
	rear = temp;
	++num;
	return true;
}
bool Queue::popQ(Item &item)
{
	if (isempty()) return false;
	Node *temp;
	temp = front;
	item = front->item;
	front = front->next;
	delete temp;
	--num;
	if (num == 0) rear = nullptr;
	return true;
}


//main.cpp
/**
*Copyright U+00A9 2018 XXXXXX. All Right Reserved.
*Filename:
*Author:XXXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/

#include<iostream>
#include<cstdlib>
#include"windows.h"
#include"description.h"
#include<ctime>
using namespace std;
bool newcustomer(double);
int main()
{
	srand(time(0));

	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 = 60 * hours;

	cout << "Enter the average number of customers per hour: ";
	double perhour;
	cin >> perhour;
	double min_per_cus = 60.0 / perhour;//计算多少分钟来一个办理者

	Item temp;
	long turnaway = 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++)//将总共时间分成若干个1分钟
	{
		if (newcustomer(min_per_cus))
		{
			if (line.isfull())
				turnaway++;//如果队列满了,此时新顾客将会离去
			else
			{
				customers++;
				temp.set(cycle);//将第cycle分钟设置为顾客的初始时间
				line.pushQ(temp);
			}
		}
		if (wait_time <= 0 && !line.isempty())//如果队列不为空并且前一个顾客已经办理好wait_time<=0
		{
			line.popQ(temp);//出队列
			wait_time = temp.get_pt();//重新设置新顾客的办理时间
			line_wait += cycle - temp.get_bt();//整个队列等待一个人办好业务所经历的时间
			++served;//已经办理好的人数加1
		}
		if (wait_time > 0)
			--wait_time;//按每分钟为单位等前一个顾客办理好
		sum_line += line.get_num();//所有时刻队列的长度总和
	}
	if (customers > 0)
	{
		cout << "customers accepted: " << customers << endl;
		cout << "customers served: " << served << endl;
		cout << "turnaways: " << turnaway << 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";
  system("pause");
  return 0;
}

bool newcustomer(double x)//通过此函数来模拟每分钟是否来新顾客
{
	return rand()*x / RAND_MAX < 1;   
}

Note:对于数据成员为引用或者是非静态const类型,都必须对于构造函数采用初始化列表形式去赋值

程序运行结果如下图

猜你喜欢

转载自blog.csdn.net/weixin_43871369/article/details/84930719