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

📄 simulation.cpp

📁 银行的简易存储系统
💻 CPP
字号:
#include ".\simulation.h"
#using <mscorlib.dll>
#include <iostream>

using namespace std;
 
Simulation::Simulation(void)
{                                    
	int i;
	cout << "Enter the range of simulation: ";
	simulationTimeBegin.readTime();
	simulationTimeEnd.readTime(); 
	if(simulationTimeBegin > simulationTimeEnd)
		{
			cerr << "The begin time must be less than the end time!" << endl;
		    cout << "Enter the range of simulation: ";
			simulationTimeBegin.readTime();
			simulationTimeEnd.readTime();
		}
	cout << "Enter the number of bank tellers: ";
	cin >> tellerNum;
	cout << "Enter the range of arrival times in minutes: ";
	cin >> arrivalLow >> arrivalHigh;
	cout << "Enter the range of service times in minutes: ";
	cin >> serviceLow >> serviceHigh;
	//Event firstevent;
	for(i = 1;i <= 10;i++)
	{
		tstate[i].finishService = simulationTimeBegin;
		tstate[i].totalCustomerCount = 0;
		tstate[i].totalCustomerWait = 0;
		tstate[i].totalService = 0;
	}
	nextCustomer = 1;
	eventSim.push(Event(simulationTimeBegin,arrival,1,0,0,0));
}

int Simulation::NextArrivalTime()
{
	return arrivalLow + rnd.random(arrivalHigh - arrivalLow + 1);
}

int Simulation::GetServiceTime()
{
	return serviceLow + rnd.random(serviceHigh - serviceLow + 1);
}

int Simulation::NextAvailableTeller()
{
	time24 minfinish = simulationTimeEnd;
	int minfinishindex = rnd.random(tellerNum) + 1;
	for(int i = 1;i <= tellerNum;i++)
		if(tstate[i].finishService < minfinish)
		{
			minfinish = tstate[i].finishService;
			minfinishindex = i;
		}
	return minfinishindex;
}

void Simulation::RunSimulation()
{
	int nexttime,servicetime,waittime,tellerID;
	time24 nextTime,waitTime;
	nextTime = simulationTimeBegin;
	Event e = eventSim.top();
	Event newevent;
	nexttime = 0;
	while(nextTime < simulationTimeEnd || nextTime == simulationTimeEnd)
	{
	    servicetime = GetServiceTime();
		tellerID = NextAvailableTeller();
		if(tstate[tellerID].finishService == simulationTimeBegin)
			tstate[tellerID].finishService = e.GetTime();
		if(tstate[tellerID].finishService < e.GetTime())
			tstate[tellerID].finishService = e.GetTime();
		waitTime = tstate[tellerID].finishService - e.GetTime();
		waittime = waitTime.getMinute();
		if(waittime < 0)
			waittime = 0;
		if(tstate[tellerID].finishService > simulationTimeEnd)
		{
			newevent = Event(e.GetTime(),departure,e.GetCustomerID(),0,0,0);
			eventSim.push(newevent);
			break;
		}
		tstate[tellerID].totalCustomerWait += waittime;
		tstate[tellerID].totalCustomerCount++;
		tstate[tellerID].totalService += servicetime;
		tstate[tellerID].finishService += servicetime;
		newevent = Event(tstate[tellerID].finishService,departure,e.GetCustomerID(),tellerID,waittime,servicetime);
		eventSim.push(newevent);
		
		nextTime = e.GetTime() + NextArrivalTime();
		if(nextTime > simulationTimeEnd)
			break;
		else
		{
			nextCustomer++;
			newevent = Event(nextTime,arrival,nextCustomer,0,0,0);
			e = newevent;
			eventSim.push(newevent);
		}
	}
	e = eventSim.top();
	simulationTimeEnd = (e.GetTime() < simulationTimeEnd || e.GetTime() == simulationTimeEnd)? simulationTimeEnd:e.GetTime();

}

void Simulation::PrintProcess()
{
	Event e;
	e = eventSim.top();
	eventSim.pop();
    if(!eventSim.empty())
		PrintProcess();

    if(e.GetEventType() == arrival)
	{	
		cout << "\nTime: ";
	    cout << e.GetTime()
			<< "   arrival of customer " << e.GetCustomerID() << endl;;
	}
	if(e.GetEventType() == departure)
	{
		cout << "\nTime: ";
		cout << e.GetTime() << "   departure of customer " 
			<< e.GetCustomerID() << endl;
		cout << "        Teller " << e.GetTellerID() << " Wait " << e.GetWaitTime()
			<< " Service " << e.GetServiceTime() << endl;
	}
}
void Simulation::PrintSimulation()
{
	int i,cumCustomers,cumWait,tellerWorkPercent;
	double avgCustWait,tellerWork;
	time24 dur;
	cumCustomers = 0;
    cumWait = 0;
	dur = simulationTimeBegin.duration(simulationTimeEnd);
	simulationdur = dur.getHour() * 60 + dur.getMinute();
	PrintProcess();
	for(i = 1;i <= tellerNum;i++)
	{
		cumCustomers += tstate[i].totalCustomerCount;
		cumWait += tstate[i].totalCustomerWait;
	}
	cout << endl;
	cout << "* * * * * * * * Simulation Summary * * * * * * * *" << endl;
	cout << "Simulation time: ";
	cout << simulationTimeBegin << " to ";
	cout << simulationTimeEnd << endl;
	cout << "  No.  of Customers:  " << cumCustomers << endl;
	cout << "  Average Customer Wait: ";
    avgCustWait = double(cumWait)/cumCustomers + 0.5;
	cout << int(avgCustWait) << " minutes" << endl;
	for(i = 1;i <= tellerNum;i++)
	{
		cout << "  Teller #" << i << " % Working: ";
		tellerWork = double(tstate[i].totalService)/simulationdur;
		tellerWorkPercent = int(tellerWork * 100 + 0.5);
		cout << tellerWorkPercent << endl;
	}
}

bool operator< (const Event& lhs,const Event& rhs)
{
	return lhs.GetTime() < rhs.GetTime();
}

Simulation::~Simulation(void)
{
}

⌨️ 快捷键说明

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