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

📄 carwash.cpp

📁 一个模拟洗车场的小程序
💻 CPP
字号:
#include "CarWash.h"
#include "Car.h"
#include <math.h>
#include <string>

const int CarWash::INFINITY=10000;
const int CarWash::MAX_SIZE=5;
const int CarWash::WASH_TIME =10;

CarWash::CarWash() 
{
	currentTime=0;
	numberOfCars=0;
	sumOfWaitingTimes=0;
	nextDepartureTime=INFINITY;
}

int CarWash::get_rand(int meanTime)
{

	double randomDouble=rand()/double(RAND_MAX+1);
	int timeTillNext=floor(-meanTime*log(1-randomDouble)+0.5);
	return timeTillNext;
}

int CarWash::get_mean ()
{
	cout<<"please enter the meanServiceTime:";
	cin>>meanServiceTime;
	return meanServiceTime;
}


void CarWash::runSimulation ()
{
	const string PROMPT=
		"\nthe the sentinel is 999 and the next random arrival time is :";
	const int SENTINEL=999;
	queue<Car>carQueue;
	cout<<"please enter the meanArrivalTime :";
	int meanArrivalTime;
	cin>>meanArrivalTime;
	int nextArrivalTime=this->get_rand(meanArrivalTime);
	cout<<nextArrivalTime<<endl;
	while(nextArrivalTime!=SENTINEL)
	{
		if(nextArrivalTime< nextDepartureTime)
		{
			processArrival(nextArrivalTime,carQueue);
			cout<<PROMPT<<endl;
			nextArrivalTime+=(this->get_rand(meanArrivalTime));
			cout<<nextArrivalTime<<endl;
		}//if
		else
			processDepature(carQueue);
	}//while SENTINEL not reached
	//wash any cars remaining on the carQueue
	while(nextDepartureTime<INFINITY)
		processDepature(carQueue);
}//runsimulation


void CarWash::processDepature(std::queue<Car> &carQueue)
{
	int waitingTime;

	cout<<"departuretime= "<<nextDepartureTime<<endl;
	currentTime=nextDepartureTime;
	if(!carQueue.empty())
	{
		Car car = carQueue.front();
		carQueue.pop();
		waitingTime=currentTime-car.getArrivalTime();
		sumOfWaitingTimes+=waitingTime;
		nextDepartureTime=currentTime+(this->get_rand (meanServiceTime));
	}//carqueue was not empty
	else
		nextDepartureTime=INFINITY;
}//method processDepature


void CarWash::printResult()
{
	const string NO_CARS_MESSAGE="there were no cars in the car wash.";
	const string AVERAGE_WAITING_TIME_MESSAGE="\nthe average waiting time,in minutes,was";
	if(numberOfCars==0)
		cout<<NO_CARS_MESSAGE<<endl;
	else
		cout<<AVERAGE_WAITING_TIME_MESSAGE
		<<((double)sumOfWaitingTimes/numberOfCars)
		<<endl;
}//method printresult


void CarWash::processArrival(int nextArrivalTime, std::queue<Car> &carQueue)
{
	
	currentTime=nextArrivalTime;
	if(carQueue.size()==MAX_SIZE)
		cout<<"Overflow"<<endl;
	else 
	{
		numberOfCars++;
		if(nextDepartureTime==INFINITY)
			nextDepartureTime=currentTime+(this->get_rand(meanServiceTime));
		else
		carQueue.push(Car(nextArrivalTime));
	}
}//not an overflow

⌨️ 快捷键说明

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