【数据结构】A类—电梯运行模拟_第1页
【数据结构】A类—电梯运行模拟_第2页
【数据结构】A类—电梯运行模拟_第3页
【数据结构】A类—电梯运行模拟_第4页
【数据结构】A类—电梯运行模拟_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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;/获取系统当前运行时间/基本数据结构及其功能函数与类的声明模块struct Condition/状态类float time;/该状态下持续的时间;int sympol;/状态的开关标志符 ;struct LiftConditi

2、on/电梯状态类Condition rest;Condition up;Condition down;struct Lift/电梯类int paNum;/乘客数int presentFloor;/当前楼层LiftCondition liftCon;/电梯状态int lightmaxfloor+1;/目标层数数组int goalNummaxfloor+1;/要去每层的人数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)/队列的建立函数q.front=(Passenger *)malloc(100*sizeof(Passenger);q.rear =q.front;q.length =0;void addQue(Que &q,Passenger &p)/队列成员增加函数q.rea

4、r ->next =&p;p.pre =q.rear ;q.rear =q.rear ->next ;q.length +;int queLength(Que &q)/队列的长度函数return q.length ;void outQue(Que &q)/队首出队函数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;/循环计数器;/函数模块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()/初始化每个楼层的函数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)/创建一名乘客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、数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<<"有一名乘客因等待时间过长已经离开&quo

14、t;<<endl;outQue(fll.presentFloor .goUp );if(queLength(fll.presentFloor .goUp )=0)cout<<"乘客进入电梯完毕,电梯关门"<<endl;break;if(l.paNum =2)cout<<"电梯已满员,电梯关门"<<endl;void getInDown(Lift &l)/进入电梯往下行驶函数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<<"有一名乘客因等待时间过长已经离开"<<endl;outQue(fll.presentFloor .goDown

18、);if(queLength(fll.presentFloor .goDown )=0)cout<<"乘客进入电梯完毕,电梯关门"<<endl;break;if(l.paNum =12)cout<<"电梯已满员,电梯关门"<<endl;void getOut(Lift &l)l.liftCon .rest.sympol =1;l.liftCon .down .sympol =0;l.liftCon .up .sympol =0;Sleep(500);cout<<"该楼层电梯内正

19、有乘客出电梯"<<endl;Sleep(500);cout<<"该楼层电梯内正有乘客出电梯"<<endl;Sleep(500);cout<<"该楼层电梯内正有乘客出电梯"<<endl;Sleep(500);cout<<"该楼层电梯内正有乘客出电梯"<<endl;Sleep(500);cout<<"该楼层电梯内正有乘客出电梯"<<endl;Sleep(500);cout<<"该楼

20、层电梯内正有乘客出电梯"<<endl;l.light l.presentFloor =0;l.paNum -=l.goalNum l.presentFloor ;l.goalNum l.presentFloor =0; cout<<"电梯内现在有 "<<l.paNum <<" 人"<<endl;int ergodicFloorUp(Lift &l)/电梯内人数为0时遍历函数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)/电梯内人数为0时遍历函数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<<"是否有新的乘客出现?是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<<"电梯内无乘客到该层,且该层没有呼叫电梯向下,电梯继续下降"<

26、<endl;liftDown(l);if(l.light l.presentFloor =1&&fll.presentFloor .callDown !=1)getOut(l);if(l.paNum >0)cout<<"电梯门已经关闭,电梯继续下降"<<endl;liftDown(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静

27、止在 "<<l.presentFloor <<" 层"<<endl;cout<<"是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出现?是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);/以防电梯里没人时,排队的人已经走了if(l.paNum >0)cout<<"电梯门已经关闭,电梯继续下降"<<endl;liftDown(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静止在 "<&l

30、t;l.presentFloor <<" 层"<<endl;cout<<"是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出现?是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<<"电梯门已经关闭,电梯继续下降"<<endl;liftDown(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静止在 "<<l.presentFloor <<" 层"&l

33、t;<endl;cout<<"是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出现?是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、已经关闭,电梯上升"<<endl;liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静止在 "<<l.presentFloor <<" 层"<<endl;cout<<"是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0

36、)UserFace(l);cout<<"是否有新的乘客出现?是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<<"是否有新的乘客出现?是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<<"电梯内无乘客到该层,且该层没有呼叫电梯向上,电梯继续上升"<<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<<"电梯门已经关闭,电梯继续上升"<<endl;liftUp(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静止在 "<<l.presentFloor <<" 层"&l

43、t;<endl;cout<<"是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出现?是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<<"电梯门已经关闭,电梯继续上升"<<endl;liftUp(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静止在 "<<l.presen

46、tFloor <<" 层"<<endl;cout<<"是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出现?是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<<"电梯门已经关闭,电梯继续上升"<<endl;liftUp(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静止在 "<<l.presentFloor <<" 层"<<endl;cout<<&

49、quot;是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<"是否有新的乘客出现?是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<<"电梯门已经关闭,电梯下降"<<endl;l

51、iftDown(l);elseif(ergodicFloorUp(l)=1)liftUp(l);elseif(ergodicFloorDown(l)=1)liftDown(l);elsewhile(1)cout<<"现在无人使用电梯,电梯静止在 "<<l.presentFloor <<" 层"<<endl;cout<<"是否返回主菜单?是1否0"<<endl;int k;cin>>k;if(k!=0)UserFace(l);cout<<&q

52、uot;是否有新的乘客出现?是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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论