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

📄 机场.cpp

📁 一个机场模拟的程序和实验报告,学习<数据结构>者必用,比较容易阅读
💻 CPP
字号:
#include<iostream>
#include<cstdlib>
#include<new>
#include<ctime>
#include<cmath>
using namespace std;




class plane{//plane类的定义
public:
	int id;
	int tm;
};





template<class Type>
class Queue{//通用的队列Queue类定义
public:
	Queue(int=6);
	void Add(const Type & item);
	Type * Delete(Type &);
	bool IsFull();
	bool IsEmpty();
	int Size();
	void QueueEmpty();
	void QueueFull();
private:
	int front,rear;
	Type*queue;
	const int MaxSize;
};

template<class Type> 
Queue<Type>::Queue(int Max):MaxSize(Max){
	queue=new Type[MaxSize];
	front=rear=0;
}

template<class Type>
Type * Queue<Type>::Delete(Type & x){
	if(IsEmpty()){QueueEmpty();return 0;}
	x=queue[++front%=MaxSize];
	return & x; 
}

template<class Type>
void Queue<Type>::Add(const Type & x){
	int k=(rear+1)%MaxSize;
	if(IsFull())QueueFull();
	else queue[rear=k]=x;
}

template<class Type>
bool Queue<Type>::IsFull(){
	int k=(rear+1)%MaxSize;
	if(front==k)return true;
	else return false;
}

template<class Type>
bool Queue<Type>::IsEmpty(){
	if(front==rear)return true;
	else return false;
}

template<class Type>
void Queue<Type>::QueueFull(){
	cout<<"(满)";
}

template<class Type>
void Queue<Type>::QueueEmpty(){
	cout<<"(空)";
}

template<class Type>
int Queue<Type>::Size(){
	if(rear>=front)return rear-front;
	else return rear-front+MaxSize;
}





enum action{ARRIVE,FLY};

class Airport{//机场类的定义
public:
	Airport();//构造函数
	void Run();//模拟运行
private:
	Queue<plane> land3;//合用跑道的降落队列
	Queue<plane> land1;//专用降落
	Queue<plane> fly3;//合用跑道的起飞队列
	Queue<plane> fly2;//专用起飞
	double expectarrive;//单位时间期望降落数
	double expectfly;////单位时间期望起飞数
	int nowtime;//目前时间
	int endtime;//运行总时间
	int notime;//空闲时间
	int landwait;//降落等待时间
	int flywait;//起飞等待时间
	int nland;//总降落数
	int nplanes;//总飞机数
	int nrefuse;//拒绝数
	int nfly;//起飞数
	void SetRandSeed();//设置种子
	int PRand(double &expectvalue);//泊松函数
	plane*NewPlane(plane & p,action kind);//新加飞机
	void Refuse(plane & p,action kind);//拒绝
	void Land(plane & p,int);//降落操作
	void Fly(plane & p,int);//起飞操作
	void NoPlane();//处理空闲
	void Conclude();//总结
};

Airport::Airport(){//如果不显式初使化对象成员,会自动调用默认构造函数
	bool ok;
	cout<<"输入模拟时间长度:";cin>>endtime;
	notime=landwait=nland=nplanes=nrefuse=nfly=flywait=0;
	SetRandSeed();
	do{
		cout<<"\n输入单位时间降落的期望数:";cin>>expectarrive;
		cout<<"\n输入单位时间起飞的期望数:";cin>>expectfly;
		if(expectarrive<0.0 || expectfly<0.0){cout<<"\n不能为负;再来一次.\n";ok=false;}
		else if(expectarrive+expectfly>3.0){cout<<"\n太多了;再来一次.\n";ok=false;}
		else ok=true;
	}while(ok==false);
}

void Airport::Run(){
	int pri;
	plane p;
	for(nowtime=1;nowtime<=endtime;nowtime++){
		cout<<"\n时间单位"<<nowtime<<":";
		pri=PRand(expectarrive);
		for(int i=1;i<=pri;i++){//准备降落的飞机优先排到专用跑道,如果专用跑道满就排到合用跑道
			p=*NewPlane(p,ARRIVE);
			if(!land1.IsFull()){land1.Add(p);if(1!=land1.Size())cout<<"排到第1跑道";}//各队列的上限都是6
			else if(land3.IsFull())Refuse(p,ARRIVE);else {land3.Add(p);if(1!=land3.Size())cout<<"排到第3跑道";}
		}

		pri=PRand(expectfly);
		for(i=1;i<=pri;i++){//准备起飞的飞机优先排到专用跑道,如果专用跑道满就排到合用跑道
			p=*NewPlane(p,FLY);
			if(!fly2.IsFull()){fly2.Add(p);if(1!=fly2.Size())cout<<"排到第2跑道";}
			else if(fly3.IsFull())Refuse(p,FLY);else {fly3.Add(p);if(1!=land3.Size()+fly3.Size())cout<<"排到第3跑道";}
		}

		if(land1.IsEmpty() & land3.IsEmpty() & fly2.IsEmpty() & fly3.IsEmpty())NoPlane();
		if(!land1.IsEmpty()){p=*land1.Delete(p);Land(p,1);}
		if(!fly2.IsEmpty()){p=*fly2.Delete(p);Fly(p,2);}
		if(!land3.IsEmpty()){p=*land3.Delete(p);Land(p,3);}
		else if(!fly3.IsEmpty()){p=*fly3.Delete(p);Fly(p,3);}
	}
	Conclude();
}

void Airport::SetRandSeed(){
	srand((unsigned int)(time(NULL)%10000));
}

int Airport::PRand(double &expectvalue){
	int n=0;
	double limit;
	double x;
	limit=exp(-expectvalue);
	x=(rand()%1000)/(double)1000;
	while(x>limit){
		n++;
		x*=(rand()%1000)/(double)1000;
	}
	return n;
}

plane*Airport::NewPlane(plane & p,action kind){
	nplanes++;
	p.id=nplanes;p.tm=nowtime;
	switch(kind){
		case ARRIVE:
			cout<<"\n          飞机"<<nplanes<<"准备降落.";
			break;
		case FLY:
			cout<<"\n          飞机"<<nplanes<<"准备起飞.";
			break;
	}
	return &p;
}

void Airport::Refuse(plane & p,action kind){
	switch(kind){
		case ARRIVE:
			cout<<""<<"被拒绝降落,到别处去.";
			break;
		case FLY:
			cout<<""<<"被取消起飞.";
			break;
	}
	++nrefuse;
}

void Airport::Land(plane & p,int num){
	int wait;
	wait=nowtime-p.tm;
	cout<<"\n          飞机"<<p.id<<"在第"<<num<<"号跑道降落,该机等待时间:"<<wait<<"";
	nland++;
	landwait+=wait;
}

void Airport::Fly(plane & p,int num){
	int wait;
	wait=nowtime-p.tm;
	cout<<"\n          飞机"<<p.id<<"在第"<<num<<"号跑道起飞,该机等待时间:"<<wait<<"";
	nfly++;
	flywait+=wait;
}

void Airport::NoPlane(){
	cout<<"\n          跑道空闲.";
	notime++;
}

void Airport::Conclude(){
	cout<<"\n**************************************************************\n\n总时间:"<<endtime;
	cout<<"\n总共处理的飞机数:"<<nplanes;
	cout<<"\n降落飞机总数:"<<nland;
	cout<<"\n起飞总数:"<<nfly;
	cout<<"\n拒绝服务:"<<nrefuse;
	cout<<"\n还剩下准备降落的:"<<land1.Size()+land3.Size();
	cout<<"\n还剩下准备起飞的:"<<fly2.Size()+fly3.Size()<<"\n";
	if(endtime>0)cout<<"\n跑道空闲时间百分比:"<<((double)notime/endtime)*100.0;
	if(nland>0)cout<<"\n降落平均等待时间为:"<<(double)landwait/nland;
	if(nfly>0)cout<<"\n起飞平均等待时间为:"<<(double)flywait/nfly;
}





int main()//主函数
{
	Airport HuangHua;
	HuangHua.Run();
	cout<<"\n结束\n";
	int a;cin>>a;
	return 0;
}

⌨️ 快捷键说明

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