![C++汽车渡口模拟(数据结构)_第1页](http://file4.renrendoc.com/view/fb4b080ed80060371740899e4f24ecf7/fb4b080ed80060371740899e4f24ecf71.gif)
![C++汽车渡口模拟(数据结构)_第2页](http://file4.renrendoc.com/view/fb4b080ed80060371740899e4f24ecf7/fb4b080ed80060371740899e4f24ecf72.gif)
![C++汽车渡口模拟(数据结构)_第3页](http://file4.renrendoc.com/view/fb4b080ed80060371740899e4f24ecf7/fb4b080ed80060371740899e4f24ecf73.gif)
![C++汽车渡口模拟(数据结构)_第4页](http://file4.renrendoc.com/view/fb4b080ed80060371740899e4f24ecf7/fb4b080ed80060371740899e4f24ecf74.gif)
![C++汽车渡口模拟(数据结构)_第5页](http://file4.renrendoc.com/view/fb4b080ed80060371740899e4f24ecf7/fb4b080ed80060371740899e4f24ecf75.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- α-Apooxytetracycline-生命科学试剂-MCE-3621
- PB-22-7-Hydroxyisoquinoline-isomer-生命科学试剂-MCE-3092
- L-Arginyl-L-alanine-生命科学试剂-MCE-1970
- BDW-OH-生命科学试剂-MCE-6441
- 4-Chlorocathinone-hydrochloride-生命科学试剂-MCE-4146
- 1-Methyl-3-amino-4-cyanopyrazole-生命科学试剂-MCE-7778
- 2025年度智能城市基础设施合作框架协议
- 二零二五年度茶叶种植基地租赁与经营管理合同
- 二零二五年度货车驾驶员劳动合同(货车驾驶与车辆融资租赁)
- 2025年度解除合同终止合同样本:5G通信技术合作终止合同书
- 2024-2025学年广东省深圳市南山区监测数学三年级第一学期期末学业水平测试试题含解析
- 广东2024年广东金融学院招聘专职辅导员9人笔试历年典型考点(频考版试卷)附带答案详解
- 2025年研究生考试考研英语(二204)试卷与参考答案
- DB31∕731-2020 船舶修正总吨单位产品能源消耗限额
- 2024-年全国医学博士外语统一入学考试英语试题
- 天津市-2024年-社区工作者-上半年笔试真题卷
- 2024年卫生专业技术资格考试卫生检验技术(初级(师)211)相关专业知识试题及答案指导
- 公务用车分时租赁实施方案
- 《手卫生知识培训》培训课件
- 《祛痘产品祛痘产品》课件
- 江苏省南京鼓楼区2024年中考联考英语试题含答案
评论
0/150
提交评论