【數(shù)據(jù)結(jié)構(gòu)】A類—電梯運行模擬_第1頁
【數(shù)據(jù)結(jié)構(gòu)】A類—電梯運行模擬_第2頁
【數(shù)據(jù)結(jié)構(gòu)】A類—電梯運行模擬_第3頁
【數(shù)據(jù)結(jié)構(gòu)】A類—電梯運行模擬_第4頁
【數(shù)據(jù)結(jié)構(gòu)】A類—電梯運行模擬_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、.#include <stdio.h>#include <time.h>#include<iostream>#include<stdlib.h>#include <windows.h>using namespace std;/初始定義const int maxfloor=14;/最高樓層clock_t tick;/獲取系統(tǒng)當前運行時間/基本數(shù)據(jù)結(jié)構(gòu)及其功能函數(shù)與類的聲明模塊struct Condition/狀態(tài)類float time;/該狀態(tài)下持續(xù)的時間;int sympol;/狀態(tài)的開關(guān)標志符 ;struct LiftConditi

2、on/電梯狀態(tài)類Condition rest;Condition up;Condition down;struct Lift/電梯類int paNum;/乘客數(shù)int presentFloor;/當前樓層LiftCondition liftCon;/電梯狀態(tài)int lightmaxfloor+1;/目標層數(shù)數(shù)組int goalNummaxfloor+1;/要去每層的人數(shù)float workTime;float workTimePre;struct Passenger/乘客類int presentFloor;/乘客當前樓層int goalFloor;/目標層int tolerateTime;/容

3、忍時間float initTime;/建立時間Passenger *next;Passenger *pre;struct QuePassenger *front;/隊列頭指針Passenger *rear;/隊列尾指針;Passenger *mid;int length;void initQue(Que &q)/隊列的建立函數(shù)q.front=(Passenger *)malloc(100*sizeof(Passenger);q.rear =q.front;q.length =0;void addQue(Que &q,Passenger &p)/隊列成員增加函數(shù)q.rea

4、r ->next =&p;p.pre =q.rear ;q.rear =q.rear ->next ;q.length +;int queLength(Que &q)/隊列的長度函數(shù)return q.length ;void outQue(Que &q)/隊首出隊函數(shù)if(queLength(q)>1)q.mid=q.front ->next ;q.front ->next =q.mid->next ;q.mid->next ->pre =q.front;q.mid->next =NULL;q.mid->pre

5、 =NULL;q.length -;elseq.rear ->pre =NULL;q.rear=q.front ;q.rear ->next =NULL;q.length =0;struct Floor/樓層類Que goUp;Que goDown;int callUp;int callDown;int totalNum;/全局變量int passNum=0;Floor fl15;Passenger pa100;int i=0;/循環(huán)計數(shù)器;/函數(shù)模塊extern void addPassenger(Passenger &p);extern void getInUp(Lif

6、t &l);extern void getInDown(Lift &l);extern void liftUp(Lift &l);extern void getOut(Lift &l);extern int ergodicFloorUp(Lift &l);extern int ergodicFloorDown(Lift &l);extern void UserFace(Lift &l);void initFloor()/初始化每個樓層的函數(shù)for(i=1;i<15;i+)initQue(fli.goDown );initQue(fl

7、i.goUp );fli.totalNum =0;fli.callDown =0;fli.callUp =0;void initLift(Lift &l)/初始化電梯l.liftCon.rest.sympol =1;l.liftCon .up.sympol =0;l.liftCon .down .sympol =0;l.liftCon.rest.time =0;l.liftCon .up.time =0;l.liftCon .down .time =0;for(i=1;i<15;i+)l.lighti=0;l.goalNum i=0;l.paNum=0;l.presentFloo

8、r=1;tick=clock();l.workTime=l.workTimePre=(float)tick/CLK_TCK;void addPassenger(Passenger &p)/創(chuàng)建一名乘客cout<<"該乘客所在樓層"<<endl;cin>>p.presentFloor;if(p.presentFloor <1|p.presentFloor >14)cout<<"樓層越界"<<endl;cout<<"重新輸入"<<end

9、l;cin>>p.presentFloor;cout<<"該乘客要去的樓層"<<endl;cin>>p.goalFloor ;if(p.goalFloor <1|p.goalFloor >14)cout<<"樓層越界"<<endl;cout<<"重新輸入"<<endl;cin>>p.goalFloor ;cout<<"該乘客的容忍時間"<<endl;cin>>p

10、.tolerateTime ;if(p.goalFloor >p.presentFloor )flp.presentFloor .callUp =1;addQue(flp.presentFloor .goUp ,p);elseflp.presentFloor .callDown =1;addQue(flp.presentFloor .goDown ,p);flp.presentFloor .totalNum +;tick=clock();p.initTime =(float)tick/CLK_TCK;passNum+;void getInUp(Lift &l)/進入電梯往上行駛函

11、數(shù)l.liftCon .rest.sympol =1;l.liftCon .down .sympol =0;l.liftCon .up .sympol =0;fll.presentFloor .callUp =0;Sleep(500);cout<<"電梯門打開"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"

12、;乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;tick=clock();l.workTime =(float)tick/CLK_TCK;while(l.paNum<2)if(l.workTime -fll.presentFloor .goUp.fr

13、ont ->next ->initTime)<=fll.presentFloor .goUp.front ->next ->tolerateTime )l.lightfll.presentFloor.goUp .front->next ->goalFloor=1;l.goalNumfll.presentFloor.goUp .front->next ->goalFloor+;outQue(fll.presentFloor .goUp );l.paNum +;elsecout<<"有一名乘客因等待時間過長已經(jīng)離開&quo

14、t;<<endl;outQue(fll.presentFloor .goUp );if(queLength(fll.presentFloor .goUp )=0)cout<<"乘客進入電梯完畢,電梯關(guān)門"<<endl;break;if(l.paNum =2)cout<<"電梯已滿員,電梯關(guān)門"<<endl;void getInDown(Lift &l)/進入電梯往下行駛函數(shù)l.liftCon .rest.sympol =1;l.liftCon .down .sympol =0;l.lif

15、tCon .up .sympol =0;fll.presentFloor .callDown=0;Sleep(500);cout<<"電梯門打開"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘

16、客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;Sleep(500);cout<<"乘客正在進入電梯"<<endl;tick=clock();l.workTime =(float)tick/CLK_TCK;while(l.paNum<12)if(l.workTime -fll.presentFloor .goDown.front ->next ->initTime)<=fll.presentFloor .goDo

17、wn.front ->next ->tolerateTime )l.lightfll.presentFloor.goDown .front->next ->goalFloor=1;l.goalNumfll.presentFloor.goDown .front->next ->goalFloor+;outQue(fll.presentFloor .goDown );l.paNum +;elsecout<<"有一名乘客因等待時間過長已經(jīng)離開"<<endl;outQue(fll.presentFloor .goDown

18、);if(queLength(fll.presentFloor .goDown )=0)cout<<"乘客進入電梯完畢,電梯關(guān)門"<<endl;break;if(l.paNum =12)cout<<"電梯已滿員,電梯關(guān)門"<<endl;void getOut(Lift &l)l.liftCon .rest.sympol =1;l.liftCon .down .sympol =0;l.liftCon .up .sympol =0;Sleep(500);cout<<"該樓層電梯內(nèi)正

19、有乘客出電梯"<<endl;Sleep(500);cout<<"該樓層電梯內(nèi)正有乘客出電梯"<<endl;Sleep(500);cout<<"該樓層電梯內(nèi)正有乘客出電梯"<<endl;Sleep(500);cout<<"該樓層電梯內(nèi)正有乘客出電梯"<<endl;Sleep(500);cout<<"該樓層電梯內(nèi)正有乘客出電梯"<<endl;Sleep(500);cout<<"該樓

20、層電梯內(nèi)正有乘客出電梯"<<endl;l.light l.presentFloor =0;l.paNum -=l.goalNum l.presentFloor ;l.goalNum l.presentFloor =0; cout<<"電梯內(nèi)現(xiàn)在有 "<<l.paNum <<" 人"<<endl;int ergodicFloorUp(Lift &l)/電梯內(nèi)人數(shù)為0時遍歷函數(shù)if(l.liftCon .rest.sympol ) =1)for(i=l.presentFloor;i

21、<15;i+)if(fli.callDown =1|fli.callUp =1)return(1);return(0);int ergodicFloorDown(Lift &l)/電梯內(nèi)人數(shù)為0時遍歷函數(shù)if(l.liftCon .rest.sympol ) =1)for(i=l.presentFloor;i>=1;i-)if(fli.callDown =1|fli.callUp =1)return(1);return(0);void liftDown(Lift &l)l.liftCon .rest.sympol =0;l.liftCon .down .sympol

22、 =1;l.liftCon .up .sympol =0;Sleep(500);cout<<"電梯正在下降"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;Sleep(500);cout<<"電梯正在下降"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<

23、<endl;Sleep(500);cout<<"電梯正在下降"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;Sleep(500);cout<<"電梯正在下降"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;Sleep(500);

24、cout<<"電梯正在下降"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;Sleep(500);l.presentFloor -;cout<<"電梯正在下降"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;l.liftCon .dow

25、n.time +=3;cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;int j;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(l.light l.presentFloor !=1&&fll.presentFloor .callDown !=1&&fll.presentFloor.callUp !=1)cout<<"電梯內(nèi)無乘客到該層,且該層沒有呼叫電梯向下,電梯繼續(xù)下降"<

26、<endl;liftDown(l);if(l.light l.presentFloor =1&&fll.presentFloor .callDown !=1)getOut(l);if(l.paNum >0)cout<<"電梯門已經(jīng)關(guān)閉,電梯繼續(xù)下降"<<endl;liftDown(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜

27、止在 "<<l.presentFloor <<" 層"<<endl;cout<<"是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFlo

28、or >l.presentFloor )liftUp(l);else if(papassNum-1.presentFloor <l.presentFloor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFloor )getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);break;elsecontinue;if(l.light l.presentFloor !=1&&fll.presentFloor .callDown =1&&l.paNu

29、m <12)cout<<"該層有人呼叫向下"<<endl;getInDown(l);/以防電梯里沒人時,排隊的人已經(jīng)走了if(l.paNum >0)cout<<"電梯門已經(jīng)關(guān)閉,電梯繼續(xù)下降"<<endl;liftDown(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜止在 "<&l

30、t;l.presentFloor <<" 層"<<endl;cout<<"是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFloor >l.presen

31、tFloor )liftUp(l);else if(papassNum-1.presentFloor <l.presentFloor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFloor )getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);break;elsecontinue;if(l.light l.presentFloor =1&&fll.presentFloor .callDown =1)cout<<"該層有人呼叫向下且有電梯出

32、電梯"<<endl;getOut(l);getInDown(l);if(l.paNum >0)cout<<"電梯門已經(jīng)關(guān)閉,電梯繼續(xù)下降"<<endl;liftDown(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜止在 "<<l.presentFloor <<" 層"&l

33、t;<endl;cout<<"是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFloor >l.presentFloor )liftUp(l);else if(papassNum-1.pre

34、sentFloor <l.presentFloor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFloor ) getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);break;elsecontinue;if(fll.presentFloor .callUp =1)if(l.paNum =0)cout<<"該層有人呼叫向上"<<endl;getInUp(l);if(l.paNum >0)cout<<"電梯門

35、已經(jīng)關(guān)閉,電梯上升"<<endl;liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜止在 "<<l.presentFloor <<" 層"<<endl;cout<<"是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0

36、)UserFace(l);cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFloor >l.presentFloor )liftUp(l);else if(papassNum-1.presentFloor <l.presentFloor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFl

37、oor )getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);break;elsecontinue;void liftUp(Lift &l)if(fl1.callUp=1)getInUp(l);l.liftCon .rest.sympol =0;l.liftCon .down .sympol =0; l.liftCon .up .sympol =1;Sleep(500);cout<<"電梯正在上升"<<endl;cout<<"電梯在 "<<l.pres

38、entFloor <<" 層"<<endl;Sleep(500);cout<<"電梯正在上升"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;Sleep(500);cout<<"電梯正在上升"<<endl;cout<<"電梯在 "<<l.presentFloor <<&qu

39、ot; 層"<<endl;Sleep(500);cout<<"電梯正在上升"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;Sleep(500);cout<<"電梯正在上升"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<e

40、ndl;Sleep(500);l.presentFloor +;cout<<"電梯正在上升"<<endl;cout<<"電梯在 "<<l.presentFloor <<" 層"<<endl;l.liftCon .up.time +=3;cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;int j;cin>>j;if(j!=0)system("cls");addPassenger(p

41、apassNum);if(l.light l.presentFloor !=1&&fll.presentFloor .callUp !=1&&fll.presentFloor.callDown !=1 )cout<<"電梯內(nèi)無乘客到該層,且該層沒有呼叫電梯向上,電梯繼續(xù)上升"<<endl;liftUp(l);if(l.light l.presentFloor =1&&fll.presentFloor .callUp !=1)getOut(l);if(l.paNum !=0)l.liftCon .rest

42、.sympol =0;l.liftCon .down .sympol =1;l.liftCon .up .sympol =0;cout<<"電梯門已經(jīng)關(guān)閉,電梯繼續(xù)上升"<<endl;liftUp(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜止在 "<<l.presentFloor <<" 層"&l

43、t;<endl;cout<<"是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFloor >l.presentFloor )liftUp(l);else if(papassNum-1.pre

44、sentFloor <l.presentFloor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFloor )getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);elsecontinue;if(l.light l.presentFloor !=1&&fll.presentFloor .callUp =1&&l.paNum <12)cout<<"該層有人呼叫向上"<<endl;getInUp(l)

45、;if(l.paNum !=0)l.liftCon .rest.sympol =0;l.liftCon .down .sympol =1;l.liftCon .up .sympol =0;cout<<"電梯門已經(jīng)關(guān)閉,電梯繼續(xù)上升"<<endl;liftUp(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜止在 "<<l.presen

46、tFloor <<" 層"<<endl;cout<<"是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFloor >l.presentFloor )li

47、ftUp(l);else if(papassNum-1.presentFloor <l.presentFloor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFloor )getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);elsecontinue;if(l.light l.presentFloor =1&&fll.presentFloor .callUp =1)cout<<"該層有人呼叫向上且有乘客出電梯"<<co

48、ut;getOut(l);getInUp(l);if(l.paNum !=0)cout<<"電梯門已經(jīng)關(guān)閉,電梯繼續(xù)上升"<<endl;liftUp(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜止在 "<<l.presentFloor <<" 層"<<endl;cout<<&

49、quot;是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFloor >l.presentFloor )liftUp(l);else if(papassNum-1.presentFloor <l.presentF

50、loor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFloor )getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);elsecontinue;if(fll.presentFloor .callDown =1)if(l.paNum =0)cout<<"該層有人呼叫向下"<<endl;getInDown(l);if(l.paNum !=0)cout<<"電梯門已經(jīng)關(guān)閉,電梯下降"<<endl;l

51、iftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"現(xiàn)在無人使用電梯,電梯靜止在 "<<l.presentFloor <<" 層"<<endl;cout<<"是否返回主菜單?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<&q

52、uot;是否有新的乘客出現(xiàn)?是1否0"<<endl;cin>>j;if(j!=0)system("cls");addPassenger(papassNum);if(papassNum-1.presentFloor >l.presentFloor )liftUp(l);else if(papassNum-1.presentFloor <l.presentFloor)liftDown(l);else if(papassNum-1.goalFloor >l.presentFloor )getInUp(l);liftUp(l);elsegetInDown(l);liftDown(l);el

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論