📄 airportsimulation.cpp
字号:
// airPortSimulation.cpp: implementation of the airPortSimulation class.
//
//////////////////////////////////////////////////////////////////////
#include "airPortSimulation.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
airPortSimulation::airPortSimulation()
{
landing.reSize(6);
takeoff.reSize(6);
bool ok;
cout<<"请输入模拟时间单元:";
cin>>endTime;
idleTime = landWait = takeOffWait = nLand = nPlanes = nRefuse = nTakeOff = nCrash = 0;
randomize();
do {
cout<<"请输入一个时间单元内期望到达的降落的飞机数:";
cin>> expectArrive;
cout<<"请输入一个时间单元内期望到达的起飞的飞机数:";
cin>> expectDepart;
if (expectArrive < 0 || expectDepart < 0 )
{
cout<<"这些数不能为负数, 请重新输入"<<endl;
ok = false;
}
else if ( expectDepart+expectArrive > 1)
{
cout<<"机场将饱和, 请重新输入"<<endl;
ok = false;
}
else
ok = true;
}
while (!ok);
// codes below are for test
// randomize();
// endTime = 120;
// idleTime = landWait = takeOffWait = nLand = nPlanes = nRefuse = nTakeOff = nCrash = 0;
// expectArrive = 0.47;
// expectDepart = 0.47;
}
airPortSimulation::~airPortSimulation()
{
}
void airPortSimulation::runSimulation()
{
int pri;
plane p;
element<plane> ep;
for (currentTime = 1 ; currentTime <= endTime ; currentTime ++)
{
cout<<"时间单元"<<currentTime << ":"<<endl;
pri = poissionRandom(expectArrive);
for (int i=1 ; i<= pri ; i++)
{
p = *newPlane(p , ARRIVE);
ep.evaluate(p, p.rt);
if ( !landing.Insert(ep) )
refuse(p , ARRIVE);
}
pri = poissionRandom (expectDepart);
for (int j = 1 ; j<=pri ; j++)
{
p = *newPlane (p, DEPART);
ep.evaluate(p, p.rt);
if ( !takeoff.Insert(ep) )
refuse (p , DEPART);
}
bool landsuccess = false;
while ( !landsuccess && !landing.isEmpty() )
{
landing.Delete(ep);
p = ep.data;
landsuccess = land(p);
}
if ( !landsuccess )
{
if( takeoff.Delete (ep) )
{
p = ep.data;
fly(p);
}
else
idle();
}
cout<<endl;
}
conclude();
}
void airPortSimulation::randomize()
{
srand( time(NULL) );
}
int airPortSimulation::poissionRandom(double &expectValue)
{
int n = 0;
double limit;
double x;
limit = exp(-expectValue);
x = (double)rand()/32768;
while ( x > limit )
{
n++;
x *= rand()/(double)32768;
}
return n;
}
plane* airPortSimulation::newPlane(plane &p, action kind)
{
nPlanes++;
p.id = nPlanes;
p.tm = currentTime;
p.rt = rand()%1;
if (kind==ARRIVE)
cout<<"飞机"<<nPlanes<<"准备降落"<<endl;
else
cout<<"飞机"<<nPlanes<<"准备起飞"<<endl;
return &p;
}
void airPortSimulation::refuse(plane &p, action kind)
{
if (kind==ARRIVE)
cout<<"引导飞机"<<p.id<<"到其他机场降落"<<endl;
else
cout<<"告诉飞机"<<p.tm<<"等一会儿在尝试降落"<<endl;
nRefuse++;
}
bool airPortSimulation::land(plane &p)
{
int wait = currentTime -p.tm;
if ( wait>p.rt)
{
cout<<"飞机"<<p.id<<"于时间单元"<<currentTime-wait+p.rt<<"燃油耗尽, 坠机......"<<endl;
nCrash++;
return false;
}
else
{
cout<<"飞机"<<p.id<<"降落, 该机等待时间:"<< wait <<endl;
nLand++;
landWait += wait;
return true;
}
}
void airPortSimulation::fly(plane &p)
{
int wait = currentTime - p.tm;
cout<<"飞机"<<p.id<<"起飞, 该机等待时间:"<<wait<<endl;
nTakeOff++;
takeOffWait += wait;
}
void airPortSimulation::idle()
{
cout<<"跑道空闲"<<endl;
idleTime++;
}
void airPortSimulation::conclude()
{
cout<<"总模拟时间单元:"<<endTime<<endl;
cout<<"总共处理飞机数:"<<nPlanes<<endl;
cout<<"降落飞机总数:"<< nLand <<endl;
cout<<"起飞飞机总数:"<< nTakeOff <<endl;
cout<<"拒绝服务的飞机总数:"<<nRefuse<<endl;
cout<<"燃油耗尽的飞机总数:"<<nCrash<<endl;
cout<<"队列中剩余的准备降落飞机数:"<<landing.curSize()<<endl;
cout<<"队列中剩余的准备起飞飞机数:"<<takeoff.curSize()<<endl;
if (endTime >0)
cout<<"跑道空闲时间百分比"<<( (double)idleTime/endTime )*100<<endl;
if (nLand > 0)
cout<<"降落平均等待时间"<<(double)landWait/nLand<<endl;
if (nTakeOff > 0)
cout<<"起飞平均等待时间"<<(double)takeOffWait/nTakeOff<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -