📄 airport.h
字号:
enum Error_code{success,fail,overflow,underflow};
const int maxqueue=40;
//Plane 的定义
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;
int flt_num;
int clock_start;
Plane_status state;
};
//queue 的定义
class Queue{
public:
Queue();
int size()const;
bool empty()const;
Error_code serve();
Error_code append(const Plane &item);
Error_code retrieve(Plane &item)const;
protected:
int count;
int front;
int rear;
Plane entry[maxqueue];
};
Queue::Queue(){
count=0;
rear=maxqueue-1;
front=0;
}
bool Queue::empty()const
{
return count==0;
}
Error_code Queue::append(const Plane &item)
{
if(count>=maxqueue)return overflow;
count++;
rear=((rear+1)==maxqueue)?0:(rear+1);
entry[rear]=item;
return success;
}
Error_code Queue::serve()
{
if(count<=0)return underflow;
count--;
front=((front+1)==maxqueue)?0:(front+1);
return success;
}
Error_code Queue::retrieve(Plane &item)const
{
if(count<=0)return underflow;
item=entry[front];
return success;
}
class Extended_queue:public Queue{
public:
int size()const;
};
int Extended_queue::size ()const
{
return count;
}
//Random类的定义
class Random{
public:
Random();
Random(bool pseudo);
int poisson(double mean);
double random_real();
int reseed();
int seed,multiplier,add_on;
};
//Random类初始化
int Random::reseed (){
seed=seed*multiplier*(time(NULL))+add_on;
return seed;
}
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;
}
Random::Random(bool pseudo)
{
if(pseudo) seed=1;
else seed=time(NULL)%INT_MAX;
multiplier=2743;
add_on=5923;
}
Random::Random()
{}
//Runway 的定义
enum Runway_activity{idle,land,takesoff};
class Runway {
public:
Runway(int limit);
Error_code can_land(const Plane ¤t);
Error_code can_depart(const Plane ¤t);
Runway_activity activity(int time, Plane &moving);
void shut_down(int time) const;
private:
Extended_queue landing;
Extended_queue takeoff;
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的初始化
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;
}
//处理对Runway的访问
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(!takeoff.empty()){
takeoff.retrieve(moving);
takeoff_wait+=time-moving.started();
num_takeoffs++;
in_progress=takesoff;
takeoff.serve();
}
else{
idle_time++;
in_progress=idle;
}
return in_progress;
}
//模板的初始化
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 deparures_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);
}
//Plane的初始化-----------------------------------------------------------
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;
}
Plane::Plane()
{
flt_num=-1;
clock_start=-1;
state=null;
}
//接受新的Plane到达Runway队列
Error_code Runway::can_land(const Plane ¤t)
{
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 ¤t)
{
Error_code result;
if(takeoff.size()<queue_limit)
result=takeoff.append(current);
else
result=fail;
num_takeoff_requests++;
if(result!=success)
num_takeoff_refused++;
else
num_takeoff_accepted++;
return result;
}
//拒绝一架Plane
void Plane::refuse()const
{
cout<<"Plane number"<<flt_num;
if(state==arriving)
cout<<"directed to another airport"<<endl;
else
cout<<" tolt to try to takeoff again later"<<endl;
}
//处理到达的Plane
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;
}
//处理一架离开的Plane
void Plane::fly(int time)const
{
int wait=time-clock_start;
cout<<time<<":Plane number"<<flt_num<<"took off after"
<<wait<<"time unit"<<((wait==1)?"":"s")
<<"in the takeoff queue."<<endl;
}
//沟通Plane的到达数据
int Plane::started()const
{
return clock_start;
}
//标记空闲的时间单元
void run_idle(int time)
{
cout<<time<<":Runway is idle."<<endl;
}
//完成模拟
void Runway::shut_down(int time)const
{
cout<<"Simulation has conluded 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"
<<takeoff.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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -