📄 airportsimulation.h
字号:
//模拟程序
#ifndef AIRPORTSIMULATION_H
#define AIRPORTSIMULATION_H
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
#include <cmath>
#include <ctime>
#include "Queue.h"
struct plane //飞机描述
{
int id; //编号
int tm; //到达队列时间
};
enum action { ARRIVE , DEPART }; //飞机的动作
class AirportSimulation //机场模拟。一个时间单元=一架飞机起飞或降落的时间
{
public:
AirportSimulation( ); //构造函数
void RunSimulation( ); //模拟运行
private:
Queue<plane> landing; //等待降落的飞机队列,假设用环形队列
Queue<plane> takeoff; //等待起飞的飞机队列,假设用环形队列
double expectarrive; //一个时间单元内期望到达的降落的飞机数
double expectdepart; //一个时间单元内期望到达的起飞的飞机数
int curtime; //当前时间
int endtime; //模拟时间单元数
int Firstidletime; //第一跑道空闲时间单位数
int Secondidletime; //第二跑道空闲时间单位数
int Thirdidletime; //第三跑道空闲时间单位数
int landwait; //降落飞机的总等待时间
int takeoffwait; //起飞飞机的总等待时间
int nland; //降落的飞机数
int ntakeoff; //起飞的飞机数
int nplanes; //处理的飞机数
int nrefuse; //拒绝服务的飞机数
void Randomize(); //设置随机数种子
int PoissionRandom( double& expectvalue ); //根据泊松分布和个定期望值生成随机非负整数
plane* NewPlane( plane& p , action kind ); //建立新飞机的数据项
void Refuse( plane& p , action kind ); //拒绝服务
void Land( plane&, int ); //降落飞机
void Fly( plane&, int ); //起飞飞机
void Idle(int); //处理空闲时间单元
void Conclude(); //总结模拟结果
};
AirportSimulation::AirportSimulation( ) //构造函数
{
bool ok;
cout << "请输入模拟时间单元数:";
cin >> endtime;
Firstidletime = Secondidletime = Thirdidletime
= landwait = takeoffwait
= nland = nplanes = nrefuse = ntakeoff = 0; //初值
Randomize(); //设置随机数种子
do
{
cout << "请输入一个时间单元内期望到达降落的飞机数:";
cin >> expectarrive;
cout << "请输入一个时间单元内期望到达起飞的飞机数:";
cin >> expectdepart;
if( expectarrive < 0.0 || expectdepart < 0.0 )
{
cout << "这些数不能为负!请重新输入。" << endl;
ok = false;
}
else if( expectarrive > 2.0 || expectdepart > 2.0 || expectarrive + expectdepart > 3.0 )
{
cout << "机场将饱和!请重新输入。" << endl;
ok = false;
}
else
ok = true;
}
while( !ok );
}
void AirportSimulation::RunSimulation() //主控程序
{
int pri; //伪随机整数
plane p;
for( curtime = 1 ; curtime <= endtime ; curtime++ )
{
cout << "时间单元" << curtime << ":" << endl;
pri = PoissionRandom( expectarrive );
for( int i = 1 ; i <= pri ; i++ ) //处理新到达的准备降落的飞机
{
p = *NewPlane( p , ARRIVE ); //建立飞机对象
if( landing.GetSize() >= 6 )
Refuse( p , ARRIVE );
else
landing.Add( p );
}
pri = PoissionRandom( expectdepart );
for ( int j = 1 ; j <= pri ; j++ )
{
p = *NewPlane( p , DEPART );
if( takeoff.GetSize() >= 6 )
Refuse( p , DEPART );
else
takeoff.Add( p);
}
bool Thirdused = false; //只要降落队列不空,降落优先
if( landing.GetSize() > 0 )
{
p = *landing.Delete( p );
Land( p , 1 ); //若有飞机等待降落,第一降落专用跑道先用
if( landing.GetSize() > 0 ) //若还有飞机等待降落,第三跑道优先降落
{
p = *landing.Delete( p );
Land( p , 3 );
Thirdused = true; //第三跑道标志已使用
}
}
else
Idle( 1 );
if( takeoff.GetSize() > 0 ) //若有飞机等待起飞,第二起飞专用跑道先用
{
p = *takeoff.Delete( p );
Fly( p , 2 );
if( Thirdused == false && takeoff.GetSize() > 0 ) //第三跑道未用且有飞机等待起飞的情况下
{ //该跑道用于起飞
p = *takeoff.Delete( p );
Fly( p , 3 );
Thirdused = true;
}
}
else
Idle( 2 );
if( Thirdused != true ) //第三跑道空闲
Idle( 3 );
}
Conclude();
}
void AirportSimulation::Randomize() //用库函数srand和rand生成随机数.
{ //用时钟设置随机种子,以增强随机性。
srand( ( unsigned int ) ( time( NULL ) % 10000 ) );
}
int AirportSimulation::PoissionRandom( double& expectvalue )
{
int n = 0; //循环计数
double limit;
double x; //伪随机数
limit = exp( -expectvalue );
x = rand() / ( double ) 30000;
while ( x > limit )
{
n++;
x *= rand() / (double) 30000;
}
return n;
}
plane* AirportSimulation::NewPlane( plane& p, action kind ) //建立新飞机数据项
{
nplanes++; //飞机总数加1
p.id = nplanes;
p.tm = curtime;
switch ( kind )
{
case ARRIVE:
cout << " 飞机" << nplanes << "准备降落。" << endl;
break;
case DEPART:
cout << " 飞机" << nplanes << "准备起飞。" << endl;
break;
}
return &p;
}
void AirportSimulation::Refuse( plane& p , action kind ) //处理被拒绝的飞机
{
switch( kind )
{
case ARRIVE:
cout << " 引导飞机" << p.id << "到其它机场降落。" << endl;
break;
case DEPART:
cout << " 告诉飞机" << p.id << " 等一会儿再试。" << endl;
break;
}
nrefuse++;
}
void AirportSimulation::Land( plane& p , int n ) //处理飞机降落时同时传入使用跑道数
{
int wait;
wait = curtime - p.tm;
cout << " 飞机" << p.id << "降落" << n << "跑道,该机等待时间:" << wait << "。" << endl;
nland++; //降落飞机总数加1
landwait += wait; //累加总降落等待时间
}
void AirportSimulation::Fly( plane& p , int n ) //处理飞机起飞时同时传入使用跑道数
{
int wait;
wait = curtime - p.tm;
cout << " 飞机" << p.id << "起飞" << n << "跑道,该机等待时间:" << wait << "。" << endl;
ntakeoff++; //起飞飞机总数加1
takeoffwait += wait; //累加总起飞等待时间
}
void AirportSimulation::Idle( int n ) //处理机场空闲,分别处理
{
cout << " 跑道 " << n <<" 空闲。" << endl;
switch( n )
{
case 1:
Firstidletime++;
break;
case 2:
Secondidletime++;
break;
case 3:
Thirdidletime++;
break;
}
}
void AirportSimulation::Conclude()
{
cout << endl << endl << "总模拟时间单元数: " << endtime << endl;
cout << "总共处理的飞机数: " << nplanes << endl;
cout << "降落飞机总数: " << nland << endl;
cout << "起飞飞机总数: " << ntakeoff << endl;
cout << "拒绝服务的飞机总数: "<< nrefuse << endl;
cout << "队列中剩余的准备降落飞机数: " << landing.GetSize() << endl;
cout << "队列中剩余的准备起飞飞机数: " << takeoff.GetSize() << endl;
if( endtime > 0 )
cout << "跑道空闲时间百分比:" << endl
<< " First Track: " << ( ( double ) Firstidletime / endtime ) * 100.0 << endl
<< " Second Track: " << ( ( double ) Secondidletime / endtime ) * 100.0 << endl
<< " Third Track:" << ( ( double ) Thirdidletime / endtime ) * 100.0 << endl;
if( nland > 0 )
cout << "降落平均等待时间: " << ( double ) landwait / nland << endl;
if( ntakeoff > 0 )
cout << "起飞平均等待时间: " << ( double ) takeoffwait / ntakeoff << endl;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -