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);
}