C++汽車渡口模擬(數據結構)_第1頁
C++汽車渡口模擬(數據結構)_第2頁
C++汽車渡口模擬(數據結構)_第3頁
C++汽車渡口模擬(數據結構)_第4頁
C++汽車渡口模擬(數據結構)_第5頁
已閱讀5頁,還剩2頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論