您的位置:首页 > 其它

简易电梯模拟程序

2006-10-16 17:43 871 查看
问题描述:公司要建立一幢两层的办公大楼并装上“最新”的电梯。公司要求开发一个面向对象的软件模拟程序,仿真电梯的操作,确定这个电梯能否满足需要。 这个电梯只限乘载一人,为了省电,只在需要时才使用,电梯每天在一楼关门等待。模拟程序包括一个时钟,每天从时间0开始,每秒滴答一次。模拟程序的调度器组件随机设置每一层第一个人到来的时间。当时钟的时间等于第一个人到来的时间时,模拟程序对指定层生成一个新到的人并将人放在这一层。然后这个人按下该层的按钮,请求电梯开门。这个人的目的地楼层不能与他上电梯时所在的楼层相同。如果第1个人到达第1层,则他可以按下按钮和等待电梯开门之后立即进入电梯。如果第1个人在第2层,则电梯要升至第2层去接这个人。电梯从1层移至2层需要5秒钟。电梯到达一层时,打开该层的电梯门上的灯,并在电梯内发出铃声。该层的按钮和电梯中中表示该层的按钮复位,电梯门打开,乘客(如果有人)走出电梯,另一乘客(如果有人等待)进入电梯并按下目的地楼层的按钮,电梯门关上。如果电梯要开始移动,则要确定移动的方向(对只有两层的电梯很简单)并移到下一层,为了简单起见,假设电梯到达一层时发生所有事件

部分代码:

Building类,电梯、楼层等都是大楼的实体


#ifndef BUILDING_H


#define BUILDING_H




#include "Floor.h"


#include "Scheduler.h"


#include "Elevator.h"


#include "Clock.h"




class Building




...{


public:


Building();


virtual ~Building();


void run(int);


private:


Floor floor1;


Floor floor2;


Scheduler scheduler;


Elevator elevator;


Clock clock;


};




#endif

Building.cpp


// Building.cpp: implementation of the Building class.


//




/**///////////////////////////////////////////////////////////////////////




#include "Building.h"


#include <iostream>


#include <ctime>




using std::cout;


using std::endl;






/**///////////////////////////////////////////////////////////////////////


// Construction/Destruction




/**///////////////////////////////////////////////////////////////////////




Building::Building():floor1(1,elevator),floor2(2,elevator),scheduler(floor1,floor2),elevator(floor1,floor2)




...{




}




Building::~Building()




...{




}






void Building::run(int runTime/**//*模拟运行时间*/)


//模拟程序运行




...{




int currentClockTime = 0;




cout<<" ******start simulation*******"<<endl;




while(currentClockTime < runTime)




...{


clock.tick();


currentClockTime = clock.getTime();


scheduler.proccessTime(currentClockTime);


elevator.processTime(currentClockTime);




cout<<endl;




}


cout<<" *******time over*******"<<endl;


}



电梯问题始终是一个复杂的问题,我只做了简单的模拟实现,希望CSDN的网友多多指点。

程序源代码下载

欢迎大家评论!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: