数据结构与算法报告Read.docx
《数据结构与算法报告Read.docx》由会员分享,可在线阅读,更多相关《数据结构与算法报告Read.docx(14页珍藏版)》请在冰豆网上搜索。
数据结构与算法报告Read
数据结构与算法实验报告
实验题目
对于单跑道飞机场的模拟
班级1050310521
姓名黄林峰
Ⅰ设计要求:
提出问题:
1)程序目的:
(1)模拟飞机在飞机场起飞和降落问题
(2)统计跑道上飞机工作情况,包括起飞或降落飞机的数量及等待时间,以及没有起飞或降
落的飞机数量、跑道的空闲时间
2).程序功能:
模拟飞机在飞机场起飞和降落问题,以及统计跑道上飞机工作情况,包括起
飞或降落飞机的数量及等待时间
3)输入:
机场运行的时间,和单位时间内起飞和降落的飞机的架数
4)输出:
机场模拟的最终结果,包括用户输入的信息和模拟结束后统计得到的信息详情见
Conclude函数
Ⅱ问题分析:
1).程序中的数据结构:
1)structPlane:
其中有两个变量,其中一个是飞机的编号,编号与飞机为一一对应的关系,另外一个变量记录飞机进入队列的时间
2)Queue*pl,*pt:
这是指向控制飞机降落和起飞的两个队列的两个指针,其中包括四个变量,队首front,队尾rear,记录队中元素个数从而知道等待起飞和等待降落的飞机的数量变量count,最后是用于储存降落和起飞飞机的两个队列,大小由事先定义的宏MAXQUEUE指定。
3)kindAction枚举变量用于标定飞机的状态,ARRIVE为降落,DEPART为起飞。
2.)程序中的相关函数及其作用:
1)NewPlane:
为一架新的飞机家里相关的数据,并将nplanes加1。
2)Refuse:
拒绝一架飞机提出的请求,由kind变量决定拒绝飞机的请求是降落还是起飞。
3)Land:
处理一架降落的飞机,并记录相应的信息,包括现在的时间,降落飞机的架数和等待的时间。
4)Fly:
处理一架起飞的飞机,并记录相应的信息,包括现在的时间,降落飞机的架数和等待的时间。
5)Start:
程序的初始化函数,同时包括了用户输入的必要信息,模拟的时间和单位时间内可以起飞和降落飞机的架数。
6)Idle:
记录机场为空,并记录下当前的时间,将机场为空的次数加一。
7)Conclude:
在模拟结束后,对相关信息进行统计,并打印相关的信息:
包括飞机处理飞机的架数,降落飞机的架数,起飞飞机的架数,拒绝请求的飞机的架数,起飞飞机等待的时间,降落飞机等待的时间,空闲的时间,结束的时间等模拟的信息
8)Randomize:
产生一个伪随机数。
9)PoissonRandom:
模拟Poisson分布。
返回Poisson分布下,一定概率下所对应的整数值。
10)UserSayYes:
确定用户是否继续运行程序,继续,则返回1,不继续,返回0。
队列系列函数:
10)CreateQueue:
创建一个队列,并初始化为空。
11)Queuefull:
用来判断队列是否为满,若队列满,返回1;否则返回0。
12)Queueempty:
用来判断队列是否为空,若队列空,返回1;否则返回0。
13)Queuesize:
返回队列中存储的元素的个数。
14)Add:
将一架飞机加入相应的队列。
15)Remove:
将一架飞机移除出队列。
3).程序流程:
1)初始化,给出了程序的作用,并给出了用户需要输入的相关的信息,
包括单位时间内可以起飞或者降落的飞机的数量expecteddepart,expectedarrive,还
有模拟机场运行的时间endtime。
还有一个用于产生伪随机数的程序。
2.)令当前的时间curtime为1,根据输入的endtime,做for循环:
(1)首先根据expecteddepart,expectedarrive,调用Randomize函数和PoissonRandom函数,用在
(1)中产生的伪随机数返回Poisson分布下对应的数值pri(这里只取整数),分别在控制两个for循环,每次循环都是根据飞机的状态是ARRIVE还是DEPART(由枚举变量kind随机决定),分别向起飞队列和降落队列提出请求,如果队列已满,则拒绝请求(Refuse函数),如队列未满则加入队列(Add函数)。
(2)判断降落队列pl是否为空,如不空,说明有飞机等待降落,将飞机从队列pl中清除Remove函数,同时飞机降落,记录下相关的信息:
包括飞机的编号,降落时的时间,等待时间(Land函数)。
如没有飞机等待降落,则处理起飞队列pt。
过程与处理飞机降落类似,由Remove函数和Fly函数共同完成。
因为题中要求降落的飞机的优先级高于起飞的飞机,所以首先判断是否有飞机等待降落,也就是首先判断队列pl是否为空
(3)如果既没有飞机降落,也没有飞机起飞,则说明跑到此时刻为空。
此时调用Idletime函数记录下当前为空的时间(curtime),同时记录跑道空的次数加1(idletime++)。
同时curtime++,表示本模拟单元已经过去,进入下一次模拟
3)总结相关信息,打印模拟的结果。
由Conclude函数完成。
Ⅲ计算机实现:
程序代码:
#defineMAXQUEUE5
#include
#include
#include
#include
#include
typedefenumaction{ARRIVE,LAND}Action;
typedefstructplane{
intid;/*identificationnumbeerofairplane*/
inttm;/*timeofarrivalinqueue*/
}Plane;
typedefPlaneQueueEntry;
typedefstructqueue{
intcount;/*numberjofairplanesinthequeue*/
intfront;/*frontofthequeue*/
intrear;/*rearofthequeue*/
QueueEntryentry[MAXQUEUE];
}Queue;
voidNewPlane(Plane*p,int*nplanes,intcurtime,Actionkind);
voidRefuse(PlaneP,int*nrefuse,Actionkind);
voidLand(Planep,intcurtime,int*nland,int*landwait);
voidFly(Planep,intcurtime,int*ntakeoff,int*takeoffwait);
voidStart(int*endtime,double*expectarrive,double*expectdepart);
voidIdle(intcurtime,int*idletime);
voidConclude(intnplanes,intnland,intntakeoff,intnrefuse,
intlandwait,inttakeoffwait,intidletime,intendtime,
Queue*pt,Queue*pl);
voidRandomize(void);/*Generateapseudo-randominteger*/
intPoissonRandom(doubleexpectedvalue);/*SimulatethedistributionofPoisson*/
boolUserSayYes(void);
/*TheseriesfunctionsofQueue*/
voidCreateQueue(Queue*q);
intQueuefull(Queue*q);
intQueueempty(Queue*q);
intQueuesize(Queue*q);
voidAdd(QueueEntryx,Queue*q);
voidRemove(QueueEntry*x,Queue*q);
intmain(void)/*themainprogram*/
{
Queuelanding,takeoff;
Queue*pl=&landing;
Queue*pt=&takeoff;
Planeplane;
intcurtime;/*currenttime,oneunit=timefortakeofforlanding*/
intendtime;/*totalnumberoftimeunitstorun*/
doubleexpectarrive;/*numberofplanesarrivinginoneunit*/
doubleexpectdepart;/*numberofplanesnewlyreadytotakeoff*/
inti;/*loopcontrolvariable*/
intidletime;/*numberofunitswhenrunwayisidle*/
intlandwait;/*totalwaitingtimeforplaneslanded*/
intnland;/*numberofplaneslanded*/
intnplanes;/*numberofplanesprocessedsofar*/
intnrefuse;/*numberofplanesrefuseduseofairport*/
intntakeoff;/*numberofplanestakenoff*/
intpri;/*pseudo-randominteger*/
inttakeoffwait;/*totalwaitingtimefortakeoff*/
CreateQueue(pl);
CreateQueue(pt);
nplanes=nland=ntakeoff=nrefuse=0;
landwait=takeoffwait=idletime=0;
Start(&endtime,&expectarrive,&expectdepart);
for(curtime=1;curtime<=endtime;curtime++)
{
pri=PoissonRandom(expectarrive);
for(i=1;i<=pri;i++)/*Addtolandingqueue*/
{
NewPlane(&plane,&nplanes,curtime,ARRIVE);
if(Queuefull(pl))
Refuse(plane,&nrefuse,ARRIVE);
else
Add(plane,pl);
}
pri=PoissonRandom(expectdepart);
for(i=1;i<=pri;i++)/*Addtotakeoffqueue*/
{
NewPlane(&plane,&nplanes,curtime,LAND);
if(Queuefull(pt))
Refuse(plane,&nrefuse,LAND);//Ifthequeueoflandingisfull,refusetherequest.
else
Add(plane,pt);
}
if(!
Queueempty(pl))/*Bringplanestoland*/
{
Remove(&plane,pl);
Land(plane,curtime,&nland,&landwait);
}
elseif(!
Queueempty(pt))/*Allowplanestotakeoff*/
{
Remove(&plane,pt);
Fly(plane,curtime,&ntakeoff,&takeoffwait);
}
else
Idle(curtime,&idletime);
}
Conclude(nplanes,nland,ntakeoff,nrefuse,landwait,
takeoffwait,idletime,endtime,pt,pl);
return0;
}
/*Start:
Printmessagesandinitializetheparameters.
Pre:
none
Post:
Askusersforresponsesandinitializesallvariables,
specifiedasparameters.
Uses:
Usessayyes*/
voidStart(int*endtime,double*expectarrive,double*expectdepart)
{
boolok;
printf("Thisprogramsimulatesanairportwithonlyonerunway.\n"
"Oneplanecanlandortakeoffineachunitoftime.\n"
"Upto%dplanescanbewaitingtolandortakeoff"
"atanytime\n",MAXQUEUE);
printf("Howmanyunitsoftimewillbethesimulationrun?
\n");
scanf("%d",endtime);
Randomize();/*Initializerandomnumbergeneraton*/
do
{
printf("Expectednumberofarrivalsineachunittime"
"?
\n");
scanf("%lf",expectarrive);
printf("Expectednumberofdeparturesineachunittime?
\n");
scanf("%lf",expectdepart);
if(*expectarrive<0.0||*expectdepart<0.0)
{
printf("Thesenumbersmustbenonnegative.\n");
ok=1;
}
elseif(*expectarrive+*expectdepart>1.0)
{
printf("Theairportwillbecomesatarated."
"Readnewmembers?
");
ok=!
UserSayYes();/*ifusersayyes,repeatloop*/
}
else
ok=1;
}while(ok==0);
}
/*NewPlane:
Makeanewrecordforaplane,andupdatenplanes.
Pre:
None
Post:
Makesanewstructureforaplaneandupdatesnplanes.*/
voidNewPlane(Plane*p,int*nplanes,intcurtime,Actionkind)
{
(*nplanes)++;
p->id=*nplanes;
switch(kind)//Judgetheplaneisreadytolandortakeoff.
{
caseARRIVE:
printf("Plane%3dreadytoland.\n",*nplanes);
break;
caseLAND:
printf("Plane%3dreadytotakeoff.\n",*nplanes);
break;
}
}
/*Refuse:
Processaplanewhenthequeueisfull.
Pro:
None.
Post:
Processaplanewaitingtousetherunway,butthequeueisfull.*/
voidRefuse(Planep,int*nrefuse,Actionkind)
{
switch(kind)
{
caseARRIVE:
printf("Plane%3ddirectedtoanotherairport.\n",p.id);
break;
caseLAND:
printf("Plane%3dtoldtotrylater.\n",p.id);
break;
}
(*nrefuse)++;
}
/*Land:
Processaplanethatisactuallylanding.
Pre:
None.
Post:
ProcessaPlanepthatisactuallylanding*/
voidLand(Planep,intcurtime,int*nland,int*landwait)
{
intwait;
wait=curtime-p.tm;
printf("%3dplane%3dlanded;inqueue%3dunits.\n",curtime,p.id,wait);
(*nland)++;
*landwait+=wait;
}
/*Fly:
Processaplanethatisactuallytakingoff.
Pre:
None
Post:
Processaplanethatisactuallytakingoff.*/
voidFly(Planep,intcurtime,int*ntakeoff,int*takeoffwait)
{
intwait;
wait=curtime-p.tm;
printf("%3dplane%3dtookoff;inqueue%3dunits.\n",curtime,p.id,wait);
(*ntakeoff)++;
*takeoffwait+=wait;//Addallthetakeofftimetogether,andgetthetotalwaitingtime.
}
/*Idle:
Updatesvariablesforidlerunway.
Pre:
None.
Post:
Updatesvariablesforatimeunitwhentherunwayisidle.*/
voidIdle(intcurtime,int*idletime)
{
printf("%2d:
Runwayisidle.\n",curtime);
(*idletime)++;
}
/*Conclude:
Writeoutstatisticsandconcludethesimulation.
Pre:
None
Post:
Writeoutallthestatisticsandconcludethesimulation.*/
voidConclude(intnplanes,intnland,intntakeoff,intnrefuse,
intlandwait,inttakeoffwait,intidletime,intendtime,
Queue*pt,Queue*pl)
{
printf("Simulationhsaconcludedafter%dunits.\n",endtime);
printf("Totalnumberofplanesprocessed:
%3d\n",nplanes);
printf("Numberofplaneslanded:
%3d\n",nland);
printf("Numberofplanestakenoff:
%3d\n",ntakeoff);
printf("Numberofplanesrefuseduse:
%3d\n",nrefuse);
printf("Numberofplanesleftreadytoland:
%3d\n",Queuesize(pl));
printf("Numberofplanesleftreadytotakeoff:
%3d\n",Queuesize(pt));
if(endtime>0)
printf("Percentageoftimerunwayidle:
%6.2f\n",((double)idletime/endtime)*100.0);
if(nland>0)
printf("Averagewaittimetoland:
%6.2f\n",(double)landwait/nland);
if(ntakeoff>0)
printf("Averagewaittimetotakeoff:
%6.2f\n",(double)takeoffwait/ntakeoff);
}
/*Randomize:
Setstartingpointforpreudorandominteger*/
voidRandomize(void)
{
srand((unsignedint)time(NULL)%10000);
}
/*PoissonRandom:
GenerateapreudorandomintegeraccordingtothePoissondistribution
Pre:
None.
Post:
GenerateapreudorandomintegeraccordingtothePoissondistributionwiththe
valuegivenastheparameter*/
intPoissonRandom(doubleexpectedvalue)
{
intn=0;/*forloop*/