⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulation.h

📁 银行家算法
💻 H
字号:
#ifndef Simulation_H
#define Simulation_H
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include"Queue.H"
class Teller
{
public:
	int totalCustomerCount;
	int totalServiceTime;
	int finishServiceTime;
	Teller() :totalCustomerCount(0), totalServiceTime(0),
		finishServiceTime(0) {}
};
//#define PRINTPROCESS
class Simulation
{
public:
	Simulation()
	{
		cout<<endl<<"输入模拟参数"<<endl;
		cout<<"柜台数量:";
        cin>>tellerNum;
		cout<<"营业时间:"; 
		cin>>simuTime;
		cout<<"两个顾客来到的最小间隔时间:"; 
		cin>>arrivalLow;
		cout<<"两个顾客来到的最大间隔时间:";
        cin>>arrivalHigh;
		cout<< "柜台服务最短时间:";
		cin>>serviceLow;
		cout<<"柜台服务最长时间:";
		cin>>serviceHigh;
		arrivalRange=arrivalHigh-arrivalLow+1;
		serviceRange=serviceHigh-serviceLow+1;
		srand((unsigned)time(NULL));
	}
	Simulation(int tellerNum, int simuTime, int arrivalLow, int arrivalHigh, int serviceLow, int serviceHigh)
		: tellerNum(tellerNum), simuTime(simuTime), arrivalLow(arrivalLow), arrivalHigh(arrivalHigh),
		serviceLow(serviceLow), serviceHigh(serviceHigh),
		arrivalRange(arrivalHigh - arrivalLow + 1), serviceRange(serviceHigh - serviceLow + 1)
	{
		srand((unsigned)time(NULL)); }
	void Initialize()    //初始化
{
		curTime = nextTime = 0;
		customerNum = customerTime = 0;
		for (int i = 1; i <= tellerNum; i++)
		{
			tellers[i].totalCustomerCount = 0;
			tellers[i].totalServiceTime = 0;
			tellers[i].finishServiceTime = 0;
		}
		customer.MakeEmpty();
	}
	void Run()
	{
		Initialize();
		NextArrived();
#ifdef PRINTPROCESS
		cout << endl;
		cout << "tellerID";
		for (int k = 1; k <= tellerNum; k++) cout << "\tTELLER " << k;
		cout << endl;
#endif
		for (curTime = 0; curTime <= simuTime; curTime++)
		{
			if (curTime >= nextTime)
			{
				CustomerArrived();
				NextArrived();
			}
#ifdef PRINTPROCESS
			cout << "Time: " << curTime << " ";
#endif
			for (int i = 1; i <= tellerNum; i++)
			{
				if (tellers[i].finishServiceTime < curTime) tellers[i].finishServiceTime = curTime;
				if (tellers[i].finishServiceTime == curTime && !customer.IsEmpty())
				{
					int t = NextService();
#ifdef PRINTPROCESS
					cout << '\t' << customerNum + 1 << '(' << customer.GetFront() << ',' << t << ')';
#endif
					CustomerDeparture();
					tellers[i].totalCustomerCount++;
					tellers[i].totalServiceTime += t;
					tellers[i].finishServiceTime += t;
				}
#ifdef PRINTPROCESS
				else cout << "\t ";
#endif
			}
#ifdef PRINTPROCESS
			cout << endl;
#endif
		}
		PrintResult();
	}
	void PtintSimuPara()
	{
		cout<<endl<<"模拟参数"<<endl;
		cout<<"柜台数量:"<<tellerNum << "\t营业时间:" <<simuTime<<endl;
		cout<<"两个顾客来到的最小间隔时间:"<<arrivalLow<<endl;
		cout<<"两个顾客来到的最大间隔时间:"<<arrivalHigh<<endl;
		cout<<"柜台服务最短时间:"<<serviceLow<<endl;
		cout<<"柜台服务最长时间:"<<serviceHigh<<endl;
	}
	void PrintResult()
	{
		int tSN = 0;
		long tST = 0;
		cout << endl;
		cout << "-------------模拟结果-------------------";
		cout << endl << "tellerID\tServiceNum\tServiceTime\tAverageTime" << endl;
		for (int i = 1; i <= tellerNum; i++)
		{
			cout << "TELLER " << i;
			cout << '\t' << tellers[i].totalCustomerCount << " "; 
			tSN += tellers[i].totalCustomerCount;
			cout << '\t' << tellers[i].totalServiceTime << " ";
			tST += (long)tellers[i].totalServiceTime;
			cout << '\t';
			if (tellers[i].totalCustomerCount)
				cout << (float)tellers[i].totalServiceTime/(float)tellers[i].totalCustomerCount;
			else 
				cout << 0;
			cout << " " << endl;
		}
		cout << "TOTAL \t" << tSN << " \t" << tST << " \t";
		if (tSN) cout << (float)tST/(float)tSN; else cout << 0;
		cout << " " << endl;
		cout << "Customer Number:\t" << customerNum << "\tno Service:\t" << customerNum - tSN << endl;
		cout << "Customer WaitTime:\t" << customerTime << "\tAvgWaitTime:\t";
		if (tSN) cout << (float)customerTime/(float)tSN; else cout << 0;
		cout << endl;
	}
private:
	int tellerNum;
	int simuTime;
	int curTime, nextTime;
	int customerNum;
	long customerTime;
	int arrivalLow, arrivalHigh, arrivalRange;
	int serviceLow, serviceHigh, serviceRange;
	Teller tellers[21];
	Queue<int> customer;
	void NextArrived()
	{
		nextTime += arrivalLow + rand() % arrivalRange;
	}
	int NextService()
	{
		return serviceLow + rand() % serviceRange;
	}
	void CustomerArrived()
	{
		customerNum++;
		customer.EnQueue(nextTime);
	}
	void CustomerDeparture()
	{
		customerTime+=(long)curTime-(long)customer.DeQueue();
	}
};
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -