模拟飞机场程序设计:队列策略与优先级实现

需积分: 21 9 下载量 140 浏览量 更新于2024-07-16 收藏 281KB DOCX 举报
"该文档是关于数据结构课程设计的一个项目,模拟飞机场的运作。项目要求使用队列或优先队列来实现飞机的着陆和起飞管理,模拟不同时间段的飞行高峰和空闲期,以及变化的着陆和起飞时间。" 在数据结构的综合应用中,模拟飞机场的运作是一个典型的例子,它涉及到队列数据结构和随机事件的处理。此项目的核心是设计一个程序,该程序能够模拟一条跑道上的飞机起降过程,考虑了飞机的着陆和起飞时间以及优先级。 首先,问题描述明确了机场有两个队列,分别是等待着陆的飞机队列和等待起飞的飞机队列。等待着陆的飞机在同等等待时间下具有更高的优先级,这意味着如果同时有飞机准备着陆和起飞,应优先处理着陆的飞机。 在基本要求部分,项目需要使用队列或优先队列来实现飞机的调度。队列是一种先进先出(FIFO)的数据结构,而优先队列则允许根据优先级进行元素的出队,这非常适合处理飞机的优先级问题。此外,程序需要能动态调整起飞和着陆的频率,以模拟一天中不同的飞行状况,比如早晚高峰和深夜空闲时段。同时,着陆和起飞的时间间隔也应该可变,以增加模拟的多样性。 算法思想方面,设计了一个模拟时钟,按照每分钟生成随机数的方式来决定是否添加飞机到着陆或起飞队列。随机数与设定的着陆和起飞频率相比较,符合条件则创建节点并插入相应队列。接着,通过三个指针q、s和pre来处理队列中的飞机,按照优先级规则决定处理哪个队列的飞机,并输出相关信息。处理过程中,要确保pre指针始终指向已处理的节点,以避免在循环开始前为空的情况。最后,循环结束后处理剩余队列中符合输出条件的飞机节点。 模块划分部分,定义了几个关键函数,如`land`用于向着陆队列插入节点,`takeoff`用于向起飞队列插入节点,`init`用于创建空队列,`input`用于输入飞机场的基本数据,`output_A`和`output_B`分别输出着陆和起飞队列中的节点数据,而`run`则是整个模拟时钟的运行函数。 这个项目结合了数据结构的理论知识和实际问题的解决,提供了一个很好的平台来实践队列和优先队列的运用,同时也锻炼了处理随机事件和优先级问题的能力。通过这样的模拟,学生能够深入理解数据结构在实际问题中的应用,并提高编程解决问题的技巧。
2011-04-26 上传
/数据结构课程设计--飞机场模拟--初始化及主程序 //舞动青春--张佳贵 //2009年6月 #include #include #include"Extended_queue.h" #include"Random.h" #include"Runway.h" #include"Plane.h" 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 is 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?"<>queue_limit; //输入用户期望的跑道最大容纳量 cout<<"How many uints of time will the simulation run?"<>end_time; bool acceptable; do { cout<<"Expected number of arrivals per uint time?"<>arrival_rate; cout<<"Expected number of departures per uint time?"<>departure_rate; if(arrival_rate<0.0||departure_rate<0.0) cerr<<"These rates must be nonnegative."<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; double 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(fligh