数据结构与STL 8.5分
读书笔记 7.3.1 CarWash模拟
revivedsun

应用队列queue模拟洗车。通过 进入时间 与 离开时间模拟事件。 进入时间 < 离开时间 入队 。 进入时间 > 离开时间 该任务加入队列 同时处理离开事件

#include <iostream>
#include <string>
#include <queue>
using namespace std;

class Car
{
public:
	Car(){}
	Car(int nextArrivalTime){arrivalTime = nextArrivalTime;}
	int getArrivalTime(){return arrivalTime;}
protected:
	int arrivalTime;
};

class CarWash
{
	public:
		CarWash();
		void runSimulation();
		void printResult();

		void processArrival(int nextArrivalTime,queue<Car>& carQueue);
		void processDeparture(queue<Car>& carQueue);
	protected:
		const static int INFINITY;
		const static int MAX_SIZE;
		const static int WASH_TIME;

		int currentTime,
			nextDepartureTime,
			numberOfCars,
			sumOfWaitingTimes;
};

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;
}



void CarWash::runSimulation()
{
	const string PROMPT = "\nPlease enter the next arrival time.The sentinel is";
	const int SENTINEL = 999;
	queue<Car> carQueue;
	int nextArrivalTime;

	cout<<PROMPT<<SENTINEL<<endl;
	cin>>nextArrivalTime;
	while (nextArrivalTime != SENTINEL)
	{
		if (nextArrivalTime < nextDepartureTime) // 进入事件
		{
			processArrival(nextArrivalTime,carQueue);
			cout<<PROMPT<<SENTINEL<<endl;
			cin>>nextArrivalTime;
		}
		else  // 某车的达到时间达到了对立中第一辆车的离开时间,所以先处理该车的进入事件,在处理队列的离开事件了。
		{
			//processArrival(nextArrivalTime,carQueue);
			//cout<<PROMPT<<SENTINEL<<endl;
			//cin>>nextArrivalTime;
			processDeparture(carQueue);
		}
	}
	while (nextDepartureTime < INFINITY)
	{
		processDeparture(carQueue);
	}
}

void CarWash::processArrival(int nextArrivalTime,queue<Car>& carQueue)
{
	const string OVERFLOWPROMPT = "Overflow";
	currentTime = nextArrivalTime;
	if (carQueue.size() == MAX_SIZE)
	{
		cout<<OVERFLOWPROMPT<<endl;
	}
	else
	{
		numberOfCars++;
		if (nextDepartureTime == INFINITY)
		{
			nextDepartureTime = currentTime + WASH_TIME;
		}
		else
		{
			carQueue.push(Car(nextArrivalTime));
		}
	}
}

void CarWash::processDeparture(queue<Car>& carQueue)
{
	int waitingTime;
	cout<<" departure time ="<<nextDepartureTime<<endl;
	currentTime = nextDepartureTime;
	if (!carQueue.empty())
	{
		Car car = carQueue.front();
		carQueue.pop();
		waitingTime = currentTime - car.getArrivalTime();
		sumOfWaitingTimes += waitingTime;
		nextDepartureTime = currentTime + WASH_TIME;
		cout<<"Queue length is "<<carQueue.size()<<endl;
	}
	else
	{
		nextDepartureTime = INFINITY;
	}// 方法processDeparture
}

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;
	}
}

int main(void)
{
	CarWash carWash;
	carWash.runSimulation();
	carWash.printResult();
	return 0;
}
0
《数据结构与STL》的全部笔记 4篇
豆瓣
免费下载 iOS / Android 版客户端