C++汽车渡口模拟(数据结构)

上传人:s9****2 文档编号:548380176 上传时间:2024-02-09 格式:DOCX 页数:7 大小:34.87KB
返回 下载 相关 举报
C++汽车渡口模拟(数据结构)_第1页
第1页 / 共7页
C++汽车渡口模拟(数据结构)_第2页
第2页 / 共7页
C++汽车渡口模拟(数据结构)_第3页
第3页 / 共7页
C++汽车渡口模拟(数据结构)_第4页
第4页 / 共7页
C++汽车渡口模拟(数据结构)_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《C++汽车渡口模拟(数据结构)》由会员分享,可在线阅读,更多相关《C++汽车渡口模拟(数据结构)(7页珍藏版)》请在金锄头文库上搜索。

1、精选优质文档-倾情为你奉上汽车渡口管理模拟小牧童原作(2011-9-20)题目:某汽车轮渡口,过江渡船每次能载10辆车,每10分钟有一个渡轮到达。过江车辆分为客车与货车。上渡船有如下规定:客车先于货车上船,每上4辆客车允许上一辆货车;若等待的客车数不满 4辆,则以货车代替。试编写程序,模拟渡口的管理,统计客车与货车的平均等待时间。设车辆到达服从均匀分布,参数由用户指定。(一) 实际效果:(二)主程序:/文件名:FerrySimlatorTest.cpp/汽车渡口管理模拟测试程序#includeusing namespace std;#include FerrySimulator.hint ma

2、in()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; /汽车到达间隔

3、时间下限int automobileArrivalHigh; /汽车到达间隔时间上限int truckArrivalLow; /货车到达间隔时间下限int truckArrivalHigh; /货车到达间隔时间上限int automobileNum; /汽车数量int truckNum; /货车数量int automobileAvgWaitTime;/汽车平均等待时间int truckAvgWaitTime; /货车平均等待时间public:FerrySimulator();void avgWaitTime(); /计算汽车和货车平均等待时间int get_automobileAvgWaitT

4、ime() 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

5、); /初始化随机数发生器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 +(automobile

6、ArrivalHigh - 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; /定义渡轮

7、到达的时间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)

8、 ) break; /在Number小于4而队列不为空且队首的值大于currentTime跳出循环 Number = 1; /初始化下一艘船上车的数量currentTime += 10; /初始化下一艘船到达的时间 automobileAvgWaitTime = automobileTotalWaitTime/automobileNum; /求汽车平均等待时间truckAvgWaitTime = truckTotalWaitTime/truckNum; /求货车平均等待时间(四)使用的类1队列/文件名:LQueue.h/链接队列类LQueue的定义#include using namespac

9、e 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()

10、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 ele

11、mType &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 - ;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号