【数据结构】机场模拟大程序

utility.h

#pragma once
//枚举
enum Error_code
{
	success, fail, range_error, underflow, overflow, fatal,
	not_present, duplicate_error, entry_inserted, entry_found,
	internal_error
};

 queue.h

#pragma once
#include "utility.h"
#include "Plane.h"
const int maxqueue = 10;
typedef Plane Queue_entry;
class Queue
{
public:
	Queue();
	bool Empty() const;
	Error_code serve();
	Error_code append(const Queue_entry& item);
	Error_code retrieve(Queue_entry& item)const;
protected:
	int count;
	int front, rear;
	Queue_entry entry[maxqueue];

};
class Extended_queue :public Queue
{
public:
	bool full()const;
	void clear();
	int size()const;
	Error_code serve_and_retrieve(Queue_entry& item);
};

 queue.cpp

#include "Queue.h"
//构造函数初始化,队列置为空
Queue::Queue()  
{
	count = 0;
	rear = maxqueue - 1;
	front = 0;
}
//队列为空返回true。不为空返回false
bool Queue::Empty() const  
{
	return count == 0;
}
//如果队列不为空,将count-- front向前一格(若向前一格队列满了,则将front置为0)
Error_code Queue::serve()   
{
	if (count <= 0)
		return underflow;
	count--;
	front = ((front + 1) == maxqueue) ? 0 : (front + 1);
	return success;
}
//如果队列没有满,将count++ rear向前一格(若超出队列 则将rear置为0)
Error_code Queue::append(const Queue_entry& item)  
{
	if (count >= maxqueue)
		return overflow;
	count++;
	rear = ((rear + 1) == maxqueue) ? 0 : (rear + 1);
	entry[rear] = item;
	return success;
}
//访问队列首元素
Error_code Queue::retrieve(Queue_entry& item)const  
{
	if (count <= 0)
		return underflow;
	item = entry[front];
	return success;
}
bool Extended_queue::full()const
{
	return count == maxqueue;
}
void Extended_queue::clear()
{
	count = 0;
	front = 0;
	rear = maxqueue - 1;
}
int Extended_queue::size()const
{
	return count;
}
Error_code Extended_queue::serve_and_retrieve(Queue_entry& item)
{
	if (count == 0)
		return underflow;
	else
	{
		count--;
		item = entry[front];
		front = ((front + 1) == maxqueue) ? 0 : (front + 1);
		return success;
	}
}

Plane.h

#pragma once
enum Plane_status{null,arriving,departing};
class Plane
{
public:
	Plane();
	Plane(int flt, int time, Plane_status status);
	void refuse() const;
	void land(int time)const;
	void fly(int time)const;
	int started()const;
	
private:
	int flt_num;  //飞机编号
	int clock_start;  //等待时间
	Plane_status state;  //状态
};

Plane.cpp

#include <iostream>
using namespace std;
#include "Plane.h"
Plane::Plane()  //构造函数
{
	flt_num = -1;
	clock_start = -1;
	state = null;
}
Plane::Plane(int flt, int time, Plane_status status)  //有参构造函数初始化飞机的编号 等待时间 状态
{
	flt_num = flt;
	clock_start = time;
	state = status;
	cout << "Plane number" << flt << "ready to";
	if (status == arriving)
		cout << "land." << endl;
	else
		cout << "take off." << endl;
}
void Plane::refuse() const  //队列已满,拒绝飞机在此跑道上降落
{

	cout << "Plane number" << flt_num;
	if (state == arriving)
		cout << "directed to another airport" << flt_num;
	else
		cout << "told to try to takeoff again later" << endl;
}
void Plane::land(int time)const  //着陆,计算飞机等待时间 输出相关信息
{
	int wait = time - clock_start;
	cout << time << ":Plane number" << flt_num << "landed after"
		<< wait << "time unit" << ((wait == 1) ? "" : "s")
		<< "in the takeoff queue." << endl;
}
void Plane::fly(int time)const  //起飞,计算飞机等待时间 输出相关信息
{
	int wait = time - clock_start;
	cout << time << ":Plane number" << flt_num << "take off after"
		<< wait << "time unit" << ((wait == 1) ? "" : "s")
		<< "in the takeoff queue." << endl;
}
int Plane::started()const  
{
	return clock_start;
}

Random.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <math.h>
#include <time.h>

using namespace std;

class Random {
public:
	Random(bool pseudo = true);
	double random_real();
	int poisson(double mean);
private:
	int reseed();
	int seed,
		multiplier, add_on;
};

int Random::reseed()
{
	seed = seed * multiplier + add_on;
	return seed;
}

Random::Random(bool pseudo)
{
	if (pseudo)
		seed = 1;
	else
		seed = time(NULL) % INT_MAX;
	multiplier = 2743;
	add_on = 5923;
}

double Random::random_real()
{
	double max = INT_MAX + 1.0;
	double temp = reseed();
	if (temp < 0)
		temp = temp + max;
	return temp / max;
}

int Random::poisson(double mean)
{
	double limit = exp(-mean);
	double product = random_real();
	int count = 0;
	while (product > limit) {
		count++;
		product *= random_real();
	}
	return count;
}

Runway.h

#pragma once
#include "utility.h"
#include "Plane.h"
#include "Queue.h"
enum Runway_activity{idle,land,takeoff};
class Runway
{
public:
	Runway(int limit);
	Error_code can_land(const Plane& current);
	Error_code can_depart(const Plane& current);
	Runway_activity activity(int time, Plane& moving);
	void shut_down(int time)const;
	
private:
	Extended_queue landing;
	Extended_queue takeoffing;
	int queue_limit;
	int num_land_requests;
	int num_takeoff_requests;
	int num_landings;
	int num_takeoffs;
	int num_land_accepted;
	int num_takeoff_accepted;
	int num_land_refused;
	int num_takeoff_refused;
	int land_wait;
	int takeoff_wait;
	int idle_time;
};

Runway.cpp

#include "Runway.h"
#include <iostream>
using namespace std;
Runway::Runway(int limit)
{
	queue_limit = limit;
	num_land_requests = num_takeoff_requests = 0;
	num_landings = num_takeoffs = 0;
	num_land_refused = num_takeoff_refused = 0;
	num_land_accepted = num_takeoff_accepted = 0;
	land_wait = takeoff_wait = idle_time = 0;
}
Error_code Runway::can_land(const Plane& current)
{
	Error_code result;
	if (landing.size() < queue_limit)
		result = landing.append(current);
	else
		result = fail;
	num_land_requests++;
	if (result != success)
		num_land_refused++;
	else
		num_land_accepted++;
	return result;
}
Error_code Runway::can_depart(const Plane& current)
{
	Error_code result;
	if (takeoffing.size() < queue_limit)
		result = takeoffing.append(current);
	else
		result = fail;
	num_land_requests++;
	if (result != success)
		num_land_refused++;
	else
		num_land_accepted++;
	return result;
}
//处理对Runway的访问,如果landing队列不满 飞机着陆 返回结果land;如果takeoff队列不满 飞机起飞 返回结果takeoff;否则 返回idle
Runway_activity Runway::activity(int time, Plane& moving)
{
	Runway_activity in_progress;
	if (!landing.Empty())
	{
		//飞机可以着陆
		landing.retrieve(moving);  //访问着陆队列队首飞机
		land_wait += time - moving.started();  //计算着陆时间
		num_landings++;  //着陆飞机数量++
		in_progress = land;
		landing.serve();  //删除已经着陆的飞机
	}
	else if(!takeoffing.Empty())
	{
		//飞机可以起飞
		takeoffing.retrieve(moving);  //访问起飞队列队首飞机
		takeoff_wait += time - moving.started();  //计算起飞时间
		num_takeoffs++;  //起飞飞机数量++
		in_progress = takeoff ;
		takeoffing.serve();  //删除已经起飞的飞机
	}
	else
	{
		idle_time++;
		in_progress = idle;
	}
	return in_progress;
}

void Runway::shut_down(int time)const
{
	{
		cout << "Simulation has concluded after " << time << " time units." << endl
			<< "Total number of planes processed "
			<< (num_land_requests + num_takeoff_requests) << endl
			<< "Total number of planes asking to land "
			<< num_land_requests << endl
			<< "Total number of planes asking to take off "
			<< num_takeoff_requests << endl
			<< "Total number of planes accepted for landing "
			<< num_land_accepted << endl
			<< "Total number of planes accepted for takeoff "
			<< num_takeoff_accepted << endl
			<< "Total number of planes refused for landing "
			<< num_land_refused << endl
			<< "Total number of planes refused for takeoff "
			<< num_takeoff_refused << endl
			<< "Total number of planes that landed "
			<< num_landings << endl
			<< "Total number of planes that took off "
			<< num_takeoffs << endl
			<< "Total number of planes left in landing queue "
			<< landing.size() << endl
			<< "Total number of planes left in takeoff queue "
			<< takeoffing.size() << endl;
		cout << "Percentage of time runway idle "
			<< 100.0 * ((float)idle_time) / ((float)time) << "%" << endl;
		cout << "Average wait in landing queue "
			<< ((float)land_wait) / ((float)num_landings) << " time units";
		cout << endl << "Average wait in takeoff queue "
			<< ((float)takeoff_wait) / ((float)num_takeoffs)
			<< " time units" << endl;
		cout << "Average observed rate of planes wanting to land "
			<< ((float)num_land_requests) / ((float)time)
			<< " per time unit" << endl;
		cout << "Average observed rate of planes wanting to take off "
			<< ((float)num_takeoff_requests) / ((float)time)
			<< " per time unit" << endl;
	}

}

main.cpp

#include "Queue.h"
#include "Plane.h"
#include "Runway.h"
#include "Random.h"
#include <iostream>
using namespace std;
void run_idle(int time)
{
	cout << time << ":Runway is idle." << endl;
}
void initialize(int& end_time, int& queue_limit, double& arrival_rate, double& departure_rate)
{

	{
		cout << "This program simulates an airport with only one runway." << endl
			<< "One plane can land or depart in each unit of time." << endl;
		cout << "Up to what number of planes can be waiting to land "
			<< "or take off at any time? " << flush;
		cin >> queue_limit;

		cout << "How many units of time will the simulation run?" << flush;
		cin >> end_time;

		bool acceptable;
		do {
			cout << "Expected number of arrivals per unit time?" << flush;
			cin >> arrival_rate;
			cout << "Expected number of departures per unit time?" << flush;
			cin >> departure_rate;
			if (arrival_rate < 0.0 || departure_rate < 0.0)
				cerr << "These rates must be nonnegative." << endl;
			else
				acceptable = true;

			if (acceptable && arrival_rate + departure_rate > 1.0)
				cerr << "Safety Warning: This airport will become saturated. " << endl;

		} while (!acceptable);
	}
}
int main()
{
	int end_time;  //模拟次数
	int queue_limit;  //队列长度
	int flight_number = 0;  //飞机数量
	double arrival_rate, departure_rate;  //到达率、起飞率
	initialize(end_time, queue_limit, arrival_rate, departure_rate);  //初始化数据
	Random variable;
	Runway small_airport(queue_limit);
	for (int current_time = 0; current_time < end_time; current_time++)
	{
		int number_arrivals = variable.poisson(arrival_rate);
		for (int i = 0; i < number_arrivals; i++)
		{
             Plane current_plane(flight_number++, current_time, arriving);
		     if (small_airport.can_land(current_plane) != success)
			current_plane.refuse();
		}
		int number_departures = variable.poisson(departure_rate);
		for (int j = 0; j < number_departures; j++)
		{
			Plane current_plane(flight_number++, current_time, departing);
			if (small_airport.can_depart(current_plane) != success)
				current_plane.refuse();
		}
		Plane moving_plane;
		switch (small_airport.activity(current_time, moving_plane))
		{
		case land:  //飞机着陆
			moving_plane.land(current_time);
			break;
		case takeoff:  //飞机起飞
			moving_plane.fly(current_time);
			break;
		case idle:  //空闲状态
			run_idle(current_time);

		}
	}
	small_airport.shut_down(end_time);
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
华为OD机场航班调度程序是华为公司开发的一款用于机场航班调度的软件程序。该程序利用华为公司的技术优势,结合航空运输行业的实际需求,提供了高效、准确的航班调度解决方案。 首先,华为OD机场航班调度程序拥有强大的数据处理和分析能力。它能够自动从各个航空公司和机场获取大量的航班数据,并进行实时的数据筛选、整理和分析,以便快速准确地生成航班计划。通过该程序,航空公司和机场可以更好地掌握航班动态,合理安排航班资源,提升运行效率。 其次,华为OD机场航班调度程序支持智能决策和优化功能。它基于华为公司的人工智能技术,能够根据多种因素,如航班时间、乘客需求、机组人员安排等,进行智能调度和优化。这样能够有效降低航班延误率和拥堵情况,提高机场的运行能力和乘客满意度。 此外,华为OD机场航班调度程序还具备良好的用户界面和操作体验。它采用了友好的图形界面,操作简便,无需复杂的培训即可上手使用。航空公司和机场工作人员可以通过该程序实时查看航班状态、提前预警问题,并进行合理的调整和应对。 总的来说,华为OD机场航班调度程序通过集成华为公司的技术优势和航空运输行业的需求,能够实现高效、准确的航班调度。它不仅能够提升机场的运行效率和乘客满意度,还能够帮助航空公司和机场更好地应对突发情况,提升整体运营水平。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_47504614

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值