汽车渡口管理模拟小牧童原作(2011-9-20)题目:某汽车轮渡口,过江渡船每次能载10辆车,每10分钟有一个渡轮到达。过江车辆分为客车与货车。上渡船有如下规定:客车先于货车上船,每上4辆客车允许上一辆货车;若等待的客车数不满4辆,则以货车代替。试编写程序,模拟渡口的管理,统计客车与货车的平均等待时间。设车辆到达服从均匀分布,参数由用户指定。(一)实际效果:(二)主程序://文件名:FerrySimlatorTest.cpp//汽车渡口管理模拟测试程序#includeiostreamusingnamespacestd;#includeFerrySimulator.hintmain(){FerrySimulatorsample;cout汽车平均等待时间:sample.get_automobileAvgWaitTime()endl;cout货车平均等待时间:sample.get_truckAvgWaitTime()endl;return0;}(三)渡口模拟类//文件名:FerrySimulator.h//渡口模拟类的定义#includeiostreamusingnamespacestd;#includeLQueue.h#includetime.hclassFerrySimulator{private:intautomobileArrivalLow;//汽车到达间隔时间下限intautomobileArrivalHigh;//汽车到达间隔时间上限inttruckArrivalLow;//货车到达间隔时间下限inttruckArrivalHigh;//货车到达间隔时间上限intautomobileNum;//汽车数量inttruckNum;//货车数量intautomobileAvgWaitTime;//汽车平均等待时间inttruckAvgWaitTime;//货车平均等待时间public:FerrySimulator();voidavgWaitTime();//计算汽车和货车平均等待时间intget_automobileAvgWaitTime(){returnautomobileAvgWaitTime;}//返回汽车平均等待时间intget_truckAvgWaitTime(){returntruckAvgWaitTime;}//返回货车平均等待时间};FerrySimulator::FerrySimulator(){cout\n************模拟开始****************\nendl;cout请输入汽车到达间隔时间上、下限:;cinautomobileArrivalLowautomobileArrivalHigh;cout请输入货车到达间隔时间上、下限:;cintruckArrivalLowtruckArrivalHigh;cout请输入汽车数量:;cinautomobileNum;cout请输入货车数量:;cintruckNum;srand(time(NULL));//初始化随机数发生器avgWaitTime();}voidFerrySimulator::avgWaitTime(){intNumber=1,eventTime=0;intcurrentTime=0;intautomobileTotalWaitTime=0;inttruckTotalWaitTime=0;LQueueintautomobileQueue;LQueueinttruckQueue;inti;for(i=0;iautomobileNum;++i)//生成所有的汽车到达事件{currentTime+=automobileArrivalLow+(automobileArrivalHigh-automobileArrivalLow+1)*rand()/(RAND_MAX+1);automobileQueue.enQueue(currentTime);}currentTime=0;for(i=0;itruckNum;++i)//生成所有的货车到达事件{currentTime+=truckArrivalLow+(truckArrivalHigh-truckArrivalLow+1)*rand()/(RAND_MAX+1);truckQueue.enQueue(currentTime);}currentTime=10;//定义渡轮到达的时间while(!(automobileQueue.isEmpty()&truckQueue.isEmpty())){//先让汽车上船while(!automobileQueue.isEmpty()&(Number=4)){if(automobileQueue.getHead()=currentTime){automobileTotalWaitTime+=currentTime-automobileQueue.deQueue();++Number;}if(automobileQueue.isEmpty()||(automobileQueue.getHead()currentTime))break;//在Number小于4而队列不为空且队首的值大于currentTime跳出循环}//再让货车上船while(!truckQueue.isEmpty()&(Number=5)){if(truckQueue.getHead()=currentTime){truckTotalWaitTime+=currentTime-truckQueue.deQueue();++Number;}if(truckQueue.isEmpty()||(truckQueue.getHead()currentTime))break;//在Number小于4而队列不为空且队首的值大于currentTime跳出循环}Number=1;//初始化下一艘船上车的数量currentTime+=10;//初始化下一艘船到达的时间}automobileAvgWaitTime=automobileTotalWaitTime/automobileNum;//求汽车平均等待时间truckAvgWaitTime=truckTotalWaitTime/truckNum;//求货车平均等待时间}(四)使用的类1——队列//文件名:LQueue.h//链接队列类LQueue的定义#includeiostreamusingnamespacestd;#includequeue.htemplateclasselemTypeclassLQueue:publicqueueelemType{private:structnode{//定义结点类elemTypedata;node*next;node(constelemType&x,node*N=NULL){data=x;next=N;}//初始化结点类node():next(NULL){}~node(){}};node*front,*rear;//定义队首指针和队尾指针public:LQueue(){front=rear=NULL;}voidclear();//清空队列函数boolisEmpty()const{returnfront==NULL;}voidenQueue(constelemType&x);elemTypedeQueue();elemTypegetHead();voidoutPut()const;//打印整个队列~LQueue();};//清空函数的现实templateclasselemTypevoidLQueueelemType::clear(){node*tmp;while(front!=NULL){tmp=front;front=front-next;deletetmp;}rear=front;}//入队函数的现实templateclasselemTypevoidLQueueelemType::enQueue(constelemType&x){if(rear==NULL)front=rear=newnode(x);//判断队列是否为空,然后作不同的处理else{rear-next=newnode(x);rear=rear-next;}}//出队函数的实现templateclasselemTypeelemTypeLQueueelemType::deQueue(){node*tmp=front;elemTypevalue=front-data;front=front-next;if(front==NULL)rear=NULL;//最后一个元素出队后,要将rear赋NULLdeletetmp;returnvalue;}//读队首结点的值templateclasselemTypeelemTypeLQueueelemType::getHead(){returnfront-data;}//打印整个队列函数的实现templateclasselemTypevoidLQueueelemType::outPut()const{node*tmp=front;while(tmp!=NULL){if(tmp-next==NULL)couttmp-data;elsecouttmp-data-;tmp=tmp-next;}coutendl;}//队列类析构函数的现实templateclasselemTypeLQueueelemType::~LQueue(){node*tmp;while(front!=NULL){tmp=front;front=front-next;deletetmp;}}(五)使用的抽象类2——队列//文件名:queue.h//抽象类queue的定义templateclasselemTypeclassqueue{public:virtualboolisEmpty()const=0;//判队是否为空virtualvoidenQueue(constelemType&x)=0;//进队virtualelemTypedeQueue()=0;//出队virtualelemTypegetHead()=0;//读队头元素值virtual~queue(){}};