⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 airportsimulation.cpp

📁 一个模拟机场控制飞机起飞降落及查询功能的程序。
💻 CPP
字号:
#include "AirportSimulation.h"
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using std::setw;

AirportSimulation::AirportSimulation()               //构造函数
{
	bool ok;
	cout<<"请输入模拟时间单元数: ";
	cin>>endtime;
	idletime = landwait = nland = nplanes = nrefuse = ntakeoff = takeoffwait = nprang = 0;
	Randomize();                                     //设置种子
	do {
		cout<<"请输入一个时间单元内期望到达的降落的飞机数: ";
		cin>>expectarrive;
		cout<<"请输入一个时间单元内期望到达的起飞的飞机数: ";
		cin>>expectdepart;
		
		if (expectarrive < 0.0 || expectdepart < 0.0)
		{
			cout<<"这些数不能为负!请重新输入."<<endl;
			ok = 0;
		}
		else if (expectarrive + expectdepart > 1.0)
		{
			cout<<"机场将饱和!请重新输入."<<endl;
			ok = 0;
		}
		else
			ok = 1;
	}while(ok = 0);
}

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.IsFull() )
				 Refuse( p, ARRIVE);
			 else
				 landing.Insert(p);
        }
        pri = PoissionRandom(expectdepart);

        for (int j = 1;j <= pri; j++)         //处理新到达的准备起飞的飞机
		{
             p = *NewPlane( p, DEPART);
             if (takeoff.IsFull())
				 Refuse( p, DEPART);
			 else
				 takeoff.Insert(p);
        }
        if ( !landing.IsEmpty() )            //降落飞机
		{
             p = *landing.Delete();
             Land(p);
        }
		else if ( !takeoff.IsEmpty() )       //起飞飞机
		{
             p = *takeoff.Delete();
             Fly(p);
        }
		else 
			Idle();                          //处理空闲时间单元
        }

        Conclude();                         //总结模拟结果
}

void AirportSimulation::Randomize()
{
	srand((unsigned int) (time(NULL)%10000));
}

int AirportSimulation::PoissionRandom(double& expectvalue)
{
	int n = 0;
	double limit;
	double x;
	limit = exp(-expectvalue);
	x = rand() / (double)RAND_MAX;           //生成从0到INT_MAX之间的整数
	
	while(x > limit) {
		n++;
		x *= rand() / (double)RAND_MAX;
	}
	return n;
}

plane* AirportSimulation::NewPlane(plane& p, action kind)
{
	nplanes++;                                 //飞机总数加1
	p.id = nplanes;
	p.tm = curtime;
//	p.fuel = rand()%10;

	switch (kind)
	{
	case ARRIVE:
		p.fuel = rand()%10 +p.tm;             //随机数生成准备降落飞机此时的燃油数,而fuel则存储
		                                      //该飞机在时间为0时的“折合燃油数”。
		cout<<setw(12)<<"飞机"<<nplanes<<"准备降落。"<<"    该飞机燃油数为:"<<p.fuel - p.tm<<"。"<<endl;
		break;
	case DEPART:
		p.fuel = 100;                         //准备起飞的飞机燃油数设为最大100.
		cout<<setw(12)<<"飞机"<<nplanes<<"准备起飞。"<<"    该飞机燃油数为最大100。"<<endl;
		break;
	}
	return &p;
}

void AirportSimulation::Refuse(plane& p, action kind)
{
	switch(kind) {
	case ARRIVE:
		cout<<setw(12)<<"引导飞机"<<p.id<<"到其他机场降落."<<endl;
		break;
	case DEPART:
		cout<<setw(12)<<"告诉飞机"<<p.id<<"等一会儿再尝试起飞."<<endl;
		break;
	}
	nrefuse++;
}

void AirportSimulation::Land(plane& p)
{
	int wait;
	wait = curtime - p.tm;
	cout<<setw(12)<<"飞机"<<p.id<<"降落,该机等待时间:"<<wait<<"。 该机还剩燃油数:"<<p.fuel - curtime<<"。"<<endl;
	nland++;                                       //降落飞机总数加1
	landwait += wait;                              //累加总降落等待时间
//判断飞机燃油及是否坠毁情况
	while(!landing.IsEmpty())
	{
		plane p = *landing.Root();
		if(p.fuel == curtime)
			Destory(p);
		else
			break;
	}
}

void AirportSimulation::Fly(plane& p)
{
	int wait;
	wait = curtime - p.tm;
	cout<<setw(12)<<"飞机"<<p.id<<"起飞,该机等待时间:"<<wait<<endl;
	ntakeoff++;                                       //降落飞机总数加1
	takeoffwait += wait;                              //累加总降落等待时间
}

void AirportSimulation::Idle()
{
	cout<<setw(20)<<"跑道空闲."<<endl;
	idletime++;
}

void AirportSimulation::Destory(plane& p)
{
	cout<<setw(12)<<"飞机"<<p.id<<"由于燃油耗尽而坠毁! "<<endl;
	p = *landing.Delete();
	nprang++;                                         //坠毁飞机数加1
}

void AirportSimulation::Conclude()
{
	cout<<"总模拟时间单元数: "<<endtime<<endl;
	cout<<"总共处理的飞机数: "<<nplanes<<endl;
	cout<<"降落飞机总数: "<<nland<<endl;
	cout<<"起飞飞机总数: "<<ntakeoff<<endl;
	cout<<"拒绝服务的飞机总数: "<<nrefuse<<endl;
	cout<<"坠毁飞机总数: "<<nprang<<endl;
	cout<<"剩余的准备降落的飞机数: "<<landing.Size()<<endl;
	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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -