营业窗口队列模拟

营业窗口队列模拟

// @author: Folivora Li
// @copyright: Folivora Li


/*20、【3】营业窗口队列模拟 (选做)(队列)
[问题描述]
实现具有n(n=3)个窗口的现实队列模拟,统计每人的等待时间。
[基本要求]
(1) 随机产生顾客的到达时间和服务时间存储文件。
(2) 利用文件数据实现队列的插入和删除。
(3)当有顾客离开时,根据队列长度调整队尾。
(4)考虑顾客中途离队的情况。
(5) 考虑顾客具有优先级的情况。*/
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef struct
{
	int person;
	int arrivetime;
	int servicetime;
	int prior;
}Element;

typedef struct QueueNode {
	QueueNode* next;
	Element data;
}QueueNode;

typedef struct LinkQueue {
	QueueNode* front;
	QueueNode* rear;
};

void InitQueue(LinkQueue& q)
{
	q.front = q.rear = new QueueNode;
	q.front->next = NULL;
}

void EnQueue(LinkQueue& q, Element e)
{
	QueueNode* p = new QueueNode;
	p->data = e;
	p->next = NULL;
	q.rear->next = p;
	q.rear = p;

}

void DeQueue(LinkQueue& q, QueueNode*& head)
{
	
	QueueNode* p = q.front->next;
	if (p == NULL)
	{
		return;
	}
	q.front->next = p->next;
	head = p->next;
	cout << ":业务办理完成用户信息:" << "姓名:" << p->data.person << "  " << endl;
	if (p == q.rear)
	{
		q.rear = q.front;
	}
	p = NULL;
	delete p;
}
void QuitQueue(LinkQueue& q,Element data)
{
	QueueNode* p = q.front->next;
	QueueNode* prior = q.front;
	while (p != NULL)
	{
		if ((p->data.arrivetime == data.arrivetime) && (p->data.servicetime == data.servicetime) && (p->data.person == data.person) && (p->data.prior == data.prior))
		{
			prior->next = p->next;
			p = NULL;
			delete p;
			return;
		}
		p = p->next;
		prior = prior->next;
		
	}
}
int QueueLength(LinkQueue Q)
{
	int length = 0;
	QueueNode* q = Q.front;
	while (q->next)
	{
		length++;
		q = q->next;
	}
	return length;
}
void PriorInsert(LinkQueue& q, Element e)
{
	QueueNode* p = new QueueNode;
	p->data = e;
	QueueNode* q1 = q.front;
	if (q.front->next == NULL)
	{
		q.front->next = p;
		q.rear = p;
		p->next = NULL;
		return;
	}
	q1 = q1->next;
	while(q1->next != NULL)
	{
		if (q1->next->data.prior < e.prior)
		{
			break;
		}
		q1 = q1->next;
	}
	p->next = q1->next;
	q1->next = p;
}
bool operator<(const Element& tempa, const Element& tempb)
{
	if (tempa.arrivetime < tempb.arrivetime)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void CreateFile()
{
	srand(time(NULL));
	fstream file;
	file.open("D:\\randomprior.txt", ios::out);
	if (file.fail())
	{
		cout << "error" << endl;
		exit(1);
	}
	Element e[100];
	vector<Element> vec;
	vector<Element>::iterator iter;
	
	for (int i = 0; i < 100; i++)
	{
		e[i].person = i + 1;
		e[i].arrivetime = rand() % 580 + 500;
		srand(2 * i);
		e[i].servicetime = rand() % 20 + 1;
		srand(5 * i);
		e[i].prior = rand() % 5;
		srand(time(NULL) * i);
		vec.push_back(e[i]);
	//file << person;
	//file << " ";
	//file << arrivetime;
	//file << " ";
	//file << servicetime;
	//file << " ";
	//if (prior > 0)
	//{
	//	file << 0;
	//	file << "\n";
	//}
	//else
	//{
	//	file << 1;
	//	file << "\n";
	//}

	}
	iter = vec.begin();
	
	sort(iter, vec.end());
	for (int i = 0; i < vec.size(); i++)
	{
		file << vec[i].person;
		file << " ";
		file << vec[i].arrivetime;
		file << " ";
		file << vec[i].servicetime;
		file << " ";
		if (vec[i].prior > 0)
		{
			file << 0;
			file << "\n";
		}
		else
		{
			file << 1;
			file << "\n";
		}

	}
	file.close();
}

void ReadFile(vector<Element>& vec)
{
	int person;
	int arrivetime;
	int servicetime;
	int prior;
	fstream file;
	file.open("D:\\randomprior.txt", ios::in);
	if (file.fail())
	{
		cout << "error" << endl;
		exit(1);
	}
	Element e;
	for (int i = 0; i < 100; i++)
	{
		file >> person;
		file >> arrivetime;
		file >> servicetime;
		file >> prior;
		e.arrivetime = arrivetime;
		e.person = person;
		e.prior = prior;
		e.servicetime = servicetime;
		vec.push_back(e);
	}
}

int Min(int a, int b, int c)
{
	int temp = a;
	if (temp > b)
	{
		temp = b;
	}
	if (temp > c)
	{
		temp = c;
	}
	if (temp == a)
	{
		return 1;
	}
	else if (temp == b)
	{
		return 2;
	}
	else
	{
		return 3;
	}
}
int main()
{

	int Time = 500;
	vector<Element> vec;
	ReadFile(vec);
	LinkQueue q1;
	LinkQueue q2;
	LinkQueue q3;
	InitQueue(q1);
	InitQueue(q2);
	InitQueue(q3);
	int finishtime1 = 0;
	int finishtime2 = 0;
	int finishtime3 = 0;
	int word1 = 0;
	int word2 = 0;
	int word3 = 0;
	int word11 = 0;
	int word22 = 0;
	int word33 = 0;
	int serial = 0;
	QueueNode* head = NULL;
	int hour;
	int min;
	while (Time < 1100)
	{
		hour = Time / 60;
		min = Time % 60;
		if (serial < 100)
		{
			while (vec[serial].arrivetime == Time)
			{

				if (Min(QueueLength(q1), QueueLength(q2), QueueLength(q3)) == 1)
				{
					if (vec[serial].prior == 0)
					{
						EnQueue(q1, vec[serial]);
					}
					else
					{
						PriorInsert(q1, vec[serial]);
					}
					if (word1 == 0)
					{
						word1 = 1;
						finishtime1 = vec[serial].servicetime + Time;
					}
				}
				else if (Min(QueueLength(q1), QueueLength(q2), QueueLength(q3)) == 2)
				{
					if (vec[serial].prior == 0)
					{
						EnQueue(q2, vec[serial]);
					}
					else
					{
						PriorInsert(q2, vec[serial]);
					}
					if (word2 == 0)
					{
						word2 = 1;
						finishtime2 = vec[serial].servicetime + Time;
					}
				}
				else
				{
					if (vec[serial].prior == 0)
					{
						EnQueue(q3, vec[serial]);
					}
					else
					{
						PriorInsert(q3, vec[serial]);
					}
					if (word3 == 0)
					{
						word3 = 1;
						finishtime3 = vec[serial].servicetime + Time;
					}
				}
				
				if (serial < vec.size())
				{
					serial++;
				}

				if (serial == vec.size())
				{
					break;
				}
				//cout << Time << endl;
				//cout << QueueLength(q1) << " " << QueueLength(q2) << " " << QueueLength(q3) << endl;
			}
		}
		
		if (finishtime1 == Time)
		{
			cout << "窗口1在" << hour << "时" << min << "分" << "办理业务完成";
			DeQueue(q1, head);
			if (head != NULL)
			{
				finishtime1 += head->data.servicetime;
			}
			else
			{
				finishtime1 = 0;
				word1 = 0;
			}

		}
		if (finishtime2 == Time)
		{
			cout << "窗口2在" << hour << "时" << min << "分" << "办理业务完成";
			DeQueue(q2, head);
			if (head != NULL)
			{
				finishtime2 += head->data.servicetime;
			}
			else
			{
				finishtime2 = 0;
				word2 = 0;
			}

		}
		if (finishtime3 == Time)
		{
			cout << "窗口3在" << hour << "时" << min << "分" << "办理业务完成";
			DeQueue(q3, head);
			if (head != NULL)
			{
				finishtime3 += head->data.servicetime;
			}
			else
			{
				finishtime3 = 0;
				word3 = 0;
			}
		}
		Time++;
	}


}
要求: 客户业务分为两种。第一种是申请从银行得到一笔资金,即取款或借款。第二种是向银行投入一笔资金,即存款或还款。银行有两个服务窗口,相应地有两个队列。客户到达银行后先排第一个队。处理每个客户业务时,如果居于第一种,且申请额超出银行现存资金总额顺得不到满足,则立刻排入第二个队等候,直至满足时才离开银行;否则业务处理完后立刻离开银行。每接待完一个第二种业务的客户,则顺序检查相处理(如果可能)第二个队列中的客广,对能满足的申请者予以满足,不能满足者重新排列第二个队列的队尾。注意,在此检查过程中,一旦银行资金总额少于或等于刚才第一个队列中最后一个客户(第二种业务)被接待之前的数额,或者本次已将第二个队列检查或处理了一遍,就停止被盗(因为此时已不可能还有能满足者)转而继续接待第一个队列的客户。任何时刻都只开一个窗口。假设检查不需要时间。营业时间结束时所有存户立即离开银行。 写一个上述银行业务的事件驱动模拟系统,通过模拟方法求出客户在银行内逗留的平 均时间。 [测试数据] 一天营业开始时银行拥有的款额为10000(元).营业时间为600(分钟)。其他模拟参量 自定。注意测定两种极端的情况:一是两个到达事件之间的间隔时间很短,而客户的交易时 间很长,另一个恰好相反,设置两个到达事件的间隔时间很长,而客户的交易时间很短。 [实现提示] 事件有两类;到达银行和离开银行。韧始时银行现存资金总额为total。开始营业后的第 —个事件是客户到达,营业时间从0到closetime。到达事件发生时随机地设置此客户的交 易时间相距下一到达事件之间的时间间隔。每个客户要办理的款额也是随机确定的,用负值 和正值分别表示第一类相第二类业务。变量total、closetime以及上述两个随机量的上下界 均文互地从终端读入,作为模拟参数。 两个队列和一个事件表均要用动态存储结构实现。注意弄清应该在什么条件下设置离开事件,以及第二个队列甩怎样的存储结构实现时可以获得较高的效率。注意:事件表是按 时间顺序有序的。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值