已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
汽车渡口管理模拟小牧童原作(2011-9-20)题目:某汽车轮渡口,过江渡船每次能载10辆车,每10分钟有一个渡轮到达。过江车辆分为客车与货车。上渡船有如下规定:客车先于货车上船,每上4辆客车允许上一辆货车;若等待的客车数不满 4辆,则以货车代替。试编写程序,模拟渡口的管理,统计客车与货车的平均等待时间。设车辆到达服从均匀分布,参数由用户指定。(一) 实际效果:(二)主程序:/文件名:FerrySimlatorTest.cpp/汽车渡口管理模拟测试程序#includeusing namespace std;#include FerrySimulator.hint main()FerrySimulator sample;cout 汽车平均等待时间: sample.get_automobileAvgWaitTime() endl;cout 货车平均等待时间: sample.get_truckAvgWaitTime() endl;return 0;(三)渡口模拟类/文件名:FerrySimulator.h/渡口模拟类的定义#include using namespace std;#include LQueue.h#include time.hclass FerrySimulatorprivate:int automobileArrivalLow; /汽车到达间隔时间下限int automobileArrivalHigh; /汽车到达间隔时间上限int truckArrivalLow; /货车到达间隔时间下限int truckArrivalHigh; /货车到达间隔时间上限int automobileNum; /汽车数量int truckNum; /货车数量int automobileAvgWaitTime;/汽车平均等待时间int truckAvgWaitTime; /货车平均等待时间public:FerrySimulator();void avgWaitTime(); /计算汽车和货车平均等待时间int get_automobileAvgWaitTime() return automobileAvgWaitTime; /返回汽车平均等待时间int get_truckAvgWaitTime() return truckAvgWaitTime; /返回货车平均等待时间;FerrySimulator:FerrySimulator()cout n*模拟开始*n endl;cout automobileArrivalLow automobileArrivalHigh ;cout truckArrivalLow truckArrivalHigh ;cout automobileNum ;cout truckNum ; srand(time(NULL); /初始化随机数发生器avgWaitTime();void FerrySimulator:avgWaitTime()int Number = 1, eventTime = 0;int currentTime=0;int automobileTotalWaitTime=0;int truckTotalWaitTime=0;LQueue automobileQueue;LQueue truckQueue;int i; 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) ) break; /在Number小于4而队列不为空且队首的值大于currentTime跳出循环 /再让货车上船 while( !truckQueue.isEmpty() & (Number=5) ) if(truckQueue.getHead()currentTime) ) break; /在Number小于4而队列不为空且队首的值大于currentTime跳出循环 Number = 1; /初始化下一艘船上车的数量currentTime += 10; /初始化下一艘船到达的时间 automobileAvgWaitTime = automobileTotalWaitTime/automobileNum; /求汽车平均等待时间truckAvgWaitTime = truckTotalWaitTime/truckNum; /求货车平均等待时间(四)使用的类1队列/文件名:LQueue.h/链接队列类LQueue的定义#include using namespace std;#include queue.htemplateclass LQueue:public queueprivate:struct node /定义结点类elemType data;node *next;node(const elemType &x, node *N=NULL) data = x; next = N; /初始化结点类node():next(NULL)node();node *front, *rear; /定义队首指针和队尾指针public:LQueue() front = rear = NULL; void clear(); /清空队列函数bool isEmpty() const return front = NULL; void enQueue(const elemType &x);elemType deQueue();elemType getHead(); void outPut() const; /打印整个队列LQueue();/清空函数的现实template void LQueue:clear()node *tmp;while(front!=NULL)tmp = front;front = front-next;delete tmp;rear = front;/入队函数的现实template void LQueue:enQueue(const elemType &x)if(rear=NULL) front = rear = new node(x); /判断队列是否为空,然后作不同的处理else rear-next = new node(x);rear = rear-next;/出队函数的实现template elemType LQueue:deQueue()node *tmp = front; elemType value = front-data;front = front-next;if(front=NULL) rear=NULL; /最后一个元素出队后,要将rear赋NULLdelete tmp;return value;/读队首结点的值template elemType LQueue:getHead()return front-data;/打印整个队列函数的实现template void LQueue:outPut() constnode *tmp = front; while(tmp!=NULL) if(tmp-next=NULL) cout data;else cout data next;cout endl;/队列类析构函数的现实template LQueue:LQueue()node *tmp;while(front!=NULL)tmp = front;front = front-next;delete tmp;(五)使用的抽象类2队列/文件名:queue.h/抽象类queue的定义template clas
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年安徽泰康教育投资有限公司招聘19人考前自测高频考点模拟试题浓缩300题及答案1套
- 2026江西江投数字经济技术有限公司及所属企业第2批次社会招考前自测高频考点模拟试题浓缩300题及答案1套
- 公会独家合同
- 国外工程合同
- 2026国网陕西省电力公司高校毕业生提前批招聘笔试参考题库及答案1套
- 国家管网集团湖南公司2026届秋季高校毕业生招聘笔试参考题库(浓缩500题)及答案详解(典优)
- 有几份购房合同
- 2026年仙桃职业学院单招职业适应性测试必刷测试卷必考题
- 2026年云南国防工业职业技术学院单招职业适应性考试题库及答案1套
- 2026年安顺职业技术学院单招综合素质考试题库必考题
- app开发外包合同协议
- GB/T 3091-2025低压流体输送用焊接钢管
- 埋地钢质管道直流干扰防护技术标准
- SL631水利水电工程单元工程施工质量验收标准第2部分:混凝土工程
- DB32-T 5082-2025 建筑工程消防施工质量验收标准
- 《半导体物理学》【ch08】半导体表面与MIS-结构-教学课件
- 教辅资料征订方案
- 儿童摄影员工管理规章制度
- 课件:《马克思主义基本原理概论》(23版):导论
- 系统用户权限分配表
- 链家 合同范本
评论
0/150
提交评论