版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上精选优质文档-倾情为你奉上专心-专注-专业专心-专注-专业精选优质文档-倾情为你奉上专心-专注-专业汽车渡口管理模拟小牧童原作(2011-9-20)题目:某汽车轮渡口,过江渡船每次能载10辆车,每10分钟有一个渡轮到达。过江车辆分为客车与货车。上渡船有如下规定:客车先于货车上船,每上4辆客车允许上一辆货车;若等待的客车数不满 4辆,则以货车代替。试编写程序,模拟渡口的管理,统计客车与货车的平均等待时间。设车辆到达服从均匀分布,参数由用户指定。实际效果:(二)主程序:/文件名:FerrySimlatorTest.cpp/汽车渡口管理模拟测试程序#includeusing
2、 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 FerrySimu
3、latorprivate:int automobileArrivalLow; /汽车到达间隔时间下限int automobileArrivalHigh; /汽车到达间隔时间上限int truckArrivalLow; /货车到达间隔时间下限int truckArrivalHigh; /货车到达间隔时间上限int automobileNum; /汽车数量int truckNum; /货车数量int automobileAvgWaitTime;/汽车平均等待时间int truckAvgWaitTime; /货车平均等待时间public:FerrySimulator();void avgWaitTi
4、me(); /计算汽车和货车平均等待时间int get_automobileAvgWaitTime() return automobileAvgWaitTime; /返回汽车平均等待时间int get_truckAvgWaitTime() return truckAvgWaitTime; /返回货车平均等待时间;FerrySimulator:FerrySimulator()cout n*模拟开始*n endl;cout automobileArrivalLow automobileArrivalHigh ;cout truckArrivalLow truckArrivalHigh ;cout
5、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) /生成所有的汽车到达事件cu
6、rrentTime += 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
7、.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() & (N
8、umber=5) ) if(truckQueue.getHead()currentTime) ) break; /在Number小于4而队列不为空且队首的值大于currentTime跳出循环 Number = 1; /初始化下一艘船上车的数量currentTime += 10; /初始化下一艘船到达的时间 automobileAvgWaitTime = automobileTotalWaitTime/automobileNum; /求汽车平均等待时间truckAvgWaitTime = truckTotalWaitTime/truckNum; /求货车平均等待时间(四)使用的类1队列/文件名:
9、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 = rea
10、r = 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;
11、/入队函数的现实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; /最后一个
12、元素出队后,要将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 c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T/ZJTSS 028-2025抹茶感官风味轮
- T/GDNSA 004-2025无创正压通气临床应用技术规范
- 2026年导游资格证考试《导游业务》培训试卷(附答案)
- T/CADBM 85-2025建筑用污水提升设备 提升排水坐便器
- 2026年军队文职考试护理专业理论知识模拟试卷及答案(共十六套)
- 《爱的五种能力》课件
- T/ZNZ 301-2024“珍米”稻米加工技术规程
- 初中九年级物理教学设计:电流的微观本质与宏观建模
- 2026年新区城市建设与可持续发展考试的测评试卷及答案
- 2026年全国注册安全工程师安全生产技术基础考试试题(50题)附答案
- 第19课《培养德智体美劳全面发展的社会主义建设者和接班人》教学设计-2026-2027学年统编版九年级语文上册
- 【新版】2025-2026学年北师大版(2024)生物八年级上册全册教案(教案设计)
- 中央生态环境资金项目储备库入库指南(2025版)
- 四年级上信息技术教案
- 2026年4月自考00037美学试题及答案含评分参考
- 人教版小学数学五年级上册第六单元《多边形的面积》单元作业设计
- 2026年法院司法辅助人员题库检测试卷及完整答案详解【全优】
- 2025年商用无人机续航电池供应合同
- 艺术品设计合同范本
- 减震垫片施工方案
- 2024年武汉市市直机关遴选公务员考试真题
评论
0/150
提交评论