




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 商业代理合作协议补充协议
- 企业手机品牌发展历程与市场策略分析
- 右美托咪定与丙泊酚在腰硬联合麻醉手术中的镇静效应对比:多维度临床分析
- 企业年度财务总结报告及下一年预算方案
- 现代物流配送效率提升案例分析
- 篮球训练课程标准与实操方案
- 公共场合礼仪展示解说稿
- 初中班主任年度工作总结范文合集
- XX高值医用耗材生产商ESG发展报告:推动行业绿色发展
- 三一重工企业简称2022-2023环境、社会与公司治理报告:学术机构评估的建筑机械行业ESG表现
- 立克次体病患者护理
- 新《职业病危害工程防护》考试复习题库(浓缩500题)
- 合作代建合同协议书
- 送养协议书范本
- 三星手机市场定位、营销策略及消费者行为研究
- 全职妈妈工作简历模板
- 中国石化考试题及答案
- 2025-2030中国抗癫痫药行业市场发展趋势与前景展望战略研究报告
- 水土保持试题多选及答案
- 幼儿园点点爱上幼儿园-(绘本)课件
- GB/T 45266-2025消费品质量分级导则羽绒制品
评论
0/150
提交评论