模拟飞机场调度问题的程序
发布网友
发布时间:2022-04-24 13:26
我来回答
共3个回答
热心网友
时间:2023-10-14 09:33
#include "airportsimulation.h"
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
AirportSimulation::AirportSimulation( ) {// 构造函数
bool ok;
cout << "请输入模拟时间单元数:";
cin >> endtime;
idletime = landwait = nland = nplanes = 0;
nrefuse = ntakeoff =takeoffwait= counter = 0;// 初值
Randomize( ); // 设置随机数种子
do {
cout << "请输入一个时间单元内期望到达降落飞机数:";
cin >> expectarrive;
cout << "请输入一个时间单元内期望到达起飞飞机数:";
cin >> expectdepart;
if (expectarrive < 0.0 || expectdepart < 0.0) {
cout << "这些数不能为负!请重新输入。"<< endl;
ok = false;
} else if (expectarrive + expectdepart > 1.0) {
cout << "机场将饱和!请重新输入。"<< endl;
ok = false;
} else ok = true;
} while (ok == false);
}
void AirportSimulation::RunSimulation( ) {
int pri; // 伪随机整数
Plane p;
for (curtime = 1; curtime <= endtime; curtime++) {
cout << "时间单元"<< curtime << ":" ;
pri = PoissionRandom(expectarrive);
for (int i =1; i <= pri; i++) { //处理新到达准备降落的飞机
p = *NewPlane(p, ARRIVE);
if (landing.full()) Refuse(p, ARRIVE);
else if(p.avaliablewaittim == 0)
{
Land(p);
break;
}
else landing.appension(p);
}
pri =PoissionRandom(expectarrive);
for (int i =1; i <= pri; i++) { //处理新到达准备起飞的飞机
p = *NewPlane(p, DEPART);
if (takeoff.full()) Refuse(p, DEPART);
else takeoff.appension(p);
}
for (int i=landing.getfront();i<=landing.getrear();i++)// 降落飞机,计算所有准备降落飞机的所剩燃油量
{
int oilleft = landing.getplaneoil(i)-curtime+landing.getplanearrivtim(i);
if (oilleft== 0)
counter++;
}
if(counter == 1)//只有一架飞机需要紧急迫降
{
if(!landing.empty())
Land(p);
break;
}
else if(counter>1)//有多架飞机需要紧急迫降
{
if(!landing.empty())//只降落一架飞机
Land(p);
cout<<"有多架飞机需要紧急降落,机场无法*,模拟结束!"<<endl;
return;
}
else if (!landing.empty()) { // 降落飞机
if(landing.remove(p)==true)
//Land(p);
relandplane(p);//-----------------------------------------------
}
else if (!takeoff.empty()) {// 起飞飞机
if(takeoff.remove(p) == true)
Fly(p);
}
else Idle( );// 处理空闲时间单元
}
Conclude( );// 总结模拟结果
}
void AirportSimulation::Randomize( ) {
srand((unsigned int) (time(NULL)%10000));
}
int AirportSimulation::PoissionRandom(double& expectvalue) {
int n = 0;// 循环计数
double limit; // e-v, 其中,v是期望值
double x; // 伪随机数
limit = exp(-expectvalue);
x = rand( )/(double)RAND_MAX;
// rand( )生成0到INT_MAX之间的整数, x在0和1之间
while (x > limit) {
n++;
x *= rand( )/(double)RAND_MAX;
}
return n;
}
Plane* AirportSimulation::NewPlane(Plane& p, action kind) {
nplanes++;// 飞机总数加1
p.id = nplanes; p.arrivetim = curtime;
Randomize( );
p.avaliablewaittim = rand()%10+1;
switch (kind) {
case ARRIVE:
cout << "飞机"<< nplanes << "准备降落。"
<<"所剩余的燃油可坚持"<<p.avaliablewaittim<<"个时间单位"<<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++;// 被拒绝飞机总数加1
}
void AirportSimulation::Land(Plane& p) {
//int wait;
//wait = curtime - p.arrivetim;
// cout << "飞机" << p.id << "降落,该机等待时间:"
//<< wait << "。"<< endl;
cout<<"飞机"<<p.id<<"的所剩燃油量为 0 ,紧急迫降。"<<endl;
nland++;// 降落飞机总数加1
// landwait += wait;// 累加总降落等待时间
}
//----------------------------------------------------
//重新处理飞机的降落
void AirportSimulation::relandplane(Plane & p)
{
//if(landing.remove(p)==true)
//Land(p);
int wait;
wait = curtime - p.arrivetim;
cout << "飞机" << p.id << "降落,该机等待时间:"
<< wait << "。"<< endl;
nland++;// 降落飞机总数加1
landwait += wait;// 累加总降落等待时间
}
//-----------------------------------------------------
void AirportSimulation::Fly(Plane& p) {
int wait = curtime - p.arrivetim;
cout << "飞机" << p.id << "起飞,该机等待时间:"
<< wait << "。"<< endl;
ntakeoff++;// 起飞飞机总数加1
takeoffwait += wait;// 累加总起飞等待时间
}
void AirportSimulation::Idle( ) {
cout << "跑道空闲。" << endl;
idletime++;// 跑道空闲时间加1
}
void AirportSimulation::Conclude( ) {
cout << "总模拟时间单元数:" << endtime << endl;
cout << "总共处理的飞机数:" << nplanes << endl;
cout << "降落飞机总数: "<< nland << endl;
cout << "起飞飞机总数: "<< ntakeoff << endl;
cout << "拒绝服务的飞机总数:"<< nrefuse << endl;
cout << "队列中剩余的准备降落飞机数:"
<< landing.Size( ) << endl;
// 假设队列成员函数Size( )返回队列中元素个数
cout << "队列中剩余的准备起飞飞机数:"
<< takeoff.Size( ) << endl;
if (endtime > 0) cout << "跑道空闲时间百分比:"
<< ((double) idletime / endtime) * 100.0 << endl;
if (nland > 0) cout << "降落平均等待时间:"
<< (double) landwait / nland << endl;
if (ntakeoff > 0) cout << "起飞平均等待时间:"
<< (double) takeoffwait / ntakeoff << endl;
热心网友
时间:2023-10-14 09:34
你就是给500分都没人写
热心网友
时间:2023-10-14 09:34
你就是给500分都没人给你写...