}
Task2:
Assembletheappropriatedeclarationsfromthetextintothefilesstack.handstack.cpp.Makeanynecessarychangesasre-compilingtheaboveapplicationReversingaListbyreplacingtheuseofSTLclassstackwiththeuseofuserdefinedclassStack.
//Section2.2:
constintmaxstack=10;//smallvaluefortesting
classStack{
public:
Stack();
boolempty()const;
Error_codepop();
Error_codetop(Stack_entry&item)const;
Error_codepush(constStack_entry&item);
private:
intcount;
Stack_entryentry[maxstack];
};
Error_codeStack:
:
push(constStack_entry&item)
/*
Pre:
None.
Post:
IftheStackisnotfull,itemisaddedtothetop
oftheStack.IftheStackisfull,
anError_codeofoverflowisreturnedandtheStackisleftunchanged.
*/
{
Error_codeoutcome=success;
if(count>=maxstack)
outcome=overflow;
else
entry[count++]=item;
returnoutcome;
}
Error_codeStack:
:
pop()
/*
Pre:
None.
Post:
IftheStackisnotempty,thetopof
theStackisremoved.IftheStack
isempty,anError_codeofunderflowisreturned.
*/
{
Error_codeoutcome=success;
if(count==0)
outcome=underflow;
else--count;
returnoutcome;
}
Error_codeStack:
:
top(Stack_entry&item)const
/*
Pre:
None.
Post:
IftheStackisnotempty,thetopof
theStackisreturnedinitem.IftheStack
isemptyanError_codeofunderflowisreturned.
*/
{
Error_codeoutcome=success;
if(count==0)
outcome=underflow;
else
item=entry[count-1];
returnoutcome;
}
boolStack:
:
empty()const
/*
Pre:
None.
Post:
IftheStackisempty,trueisreturned.
Otherwisefalseisreturned.
*/
{
booloutcome=true;
if(count>0)outcome=false;
returnoutcome;
}
Stack:
:
Stack()
/*
Pre:
None.
Post:
Thestackisinitializedtobeempty.
*/
{
count=0;
}
Task3:
Assumethefollowingdefinitionfileforanextendedstackdatastructure.
classExtended_stack{
public:
Extended_stack();
Error_codepop();
Error_codepush(Stack_entryitem);
Error_codetop(Stack_entry&item)const;
boolempty()const;
voidclear();//Resetthestacktobeempty.
boolfull()const;//Ifthestackisfull,returntrue;elsereturnfalse.
intsize()const;//Returnthenumberofentriesinthestack.
private:
intcount;
Stack_entryentry[maxstack];
};
Implementthefollowingmethods:
(a)clear(b)full(c)size
UsemethodsizeoftheclassExtended_stackinyourapplicationReversingaListtodisplaythenumberofdecimalnumbersyouimputed.
Task4:
Combineallthefunctionsandmethodsfortheairportsimulationintoacompleteprogram.Experimentwithseveralsamplerumsoftheairportsimulation,adjustingthevaluesfortheexpectednumbersofplanesreadytolandandtakeoff.Findapproximatevaluesfortheseexpectednumbersthatareaslargeaspossiblesubjecttotheconditionthatitisveryunlikelythataplanemustberefusedservice.Whathappenstothesevaluesifthemaximumsizeofthequeuesisincreasedordecreased?
/*ProgramextractsfromChapter3of
"DataStructuresandProgramDesigninC++"
byRobertL.KruseandAlexanderJ.Ryba
Copyright(C)1999byPrentice-Hall,Inc.Allrightsreserved.
Extractsfromthisfilemaybeusedintheconstructionofotherprograms,
butthiscodewillnotcompileorexecuteasgivenhere.*/
//Section3.3:
constintmaxqueue=10;//smallvaluefortesting
classQueue{
public:
Queue();
boolempty()const;
Error_codeserve();
Error_codeappend(constQueue_entry&item);
Error_coderetrieve(Queue_entry&item)const;
protected:
intcount;
intfront,rear;
Queue_entryentry[maxqueue];
};
Queue:
:
Queue()
/*
Post:
TheQueueisinitializedtobeempty.
*/
{
count=0;
rear=maxqueue-1;
front=0;
}
boolQueue:
:
empty()const
/*
Post:
ReturntrueiftheQueueisempty,otherwisereturnfalse.
*/
{
returncount==0;
}
Error_codeQueue:
:
append(constQueue_entry&item)
/*
Post:
itemisaddedtotherearoftheQueue.IftheQueueisfull
returnanError_codeofoverflowandleavetheQueueunchanged.
*/
{
if(count>=maxqueue)returnoverflow;
count++;
rear=((rear+1)==maxqueue)?
0:
(rear+1);
entry[rear]=item;
returnsuccess;
}
Error_codeQueue:
:
serve()
/*
Post:
ThefrontoftheQueueisremoved.IftheQueue
isemptyreturnanError_codeofunderflow.
*/
{
if(count<=0)returnunderflow;
count--;
front=((front+1)==maxqueue)?
0:
(front+1);
returnsuccess;
}
Error_codeQueue:
:
retrieve(Queue_entry&item)const
/*
Post:
ThefrontoftheQueueretrievedtotheoutput
parameteritem.IftheQueueisemptyreturnanError_codeofunderflow.
*/
{
if(count<=0)returnunderflow;
item=entry[front];
returnsuccess;
}
intExtended_queue:
:
size()const
/*
Post:
ReturnthenumberofentriesintheExtended_queue.
*/
{
returncount;
}
//Section3.5:
intmain()//Airportsimulationprogram
/*
Pre:
Theusermustsupplythenumberoftimeintervalsthesimulationisto
run,theexpectednumberofplanesarriving,theexpectednumber
ofplanesdepartingpertimeinterval,andthe
maximumallowedsizeforrunwayqueues.
Post:
Theprogramperformsarandomsimulationoftheairport,showing
thestatusoftherunwayateachtimeinterval,andprintsouta
summaryofairportoperationattheconclusion.
Uses:
ClassesRunway,Plane,Randomandfunctionsrun_idle,initialize.
*/
{
intend_time;//timetorunsimulation
intqueue_limit;//sizeofRunwayqueues
intflight_number=0;
doublearrival_rate,departure_rate;
initialize(end_time,queue_limit,arrival_rate,departure_rate);
Randomvariable;
Runwaysmall_airport(queue_limit);
for(intcurrent_time=0;current_timeintnumber_arrivals=variable.poisson(arrival_rate);//currentarrivalrequests
for(inti=0;iPlanecurrent_plane(flight_number++,current_time,arriving);
if(small_airport.can_land(current_plane)!
=success)
current_plane.refuse();
}
intnumber_departures=variable.poisson(departure_rate);//currentdeparturerequests
for(intj=0;jPlanecurrent_plane(flight_number++,current_time,departing);
if(small_airport.can_depart(current_plane)!
=success)
current_plane.refuse();
}
Planemoving_plane;
switch(small_airport.activity(current_time,moving_plane)){
//LetatmostonePlaneontotheRunwayatcurrent_time.
caseland:
moving_plane.land(current_time);
break;
casetakeoff:
moving_plane.fly(current_time);
break;
caseidle:
run_idle(current_time);
}
}
small_airport.shut_down(end_time);
}
enumRunway_activity{idle,land,takeoff};
classRunway{
public:
Runway(intlimit);
Error_codecan_land(constPlane¤t);
Error_codecan_depart(constPlane¤t);
Runway_activityactivity(inttime,Plane&moving);
voidshut_down(inttime)const;
private:
Extended_queuelanding;
Extended_queuetakeoff;
intqueue_limit;
intnum_land_requests;//numberofplanesaskingtoland
intnum_takeoff_requests;//numberofplanesaskingtotakeoff
intnum_landings;//numberofplanesthathavelanded
intnum_takeoffs;//numberofplanesthathavetakenoff
intnum_land_accepted;//numberofplanesqueuedtoland
intnum_takeoff_accepted;//numberofplanesqueuedtotakeoff
intnum_land_refused;//numberoflandingplanesrefused
intnum_takeoff_refused;//numberofdepartingplanesrefused
intland_wait;//totaltimeofplaneswaitingtoland
inttakeoff_wait;//totaltimeofplaneswaitingtotakeoff
intidle_time;//totaltimerunwayisidle
};
enumPlane_status{null,arriving,departing};
classPlane{
public:
Plane();
Plane(intflt,inttime,Plane_statusstatus);
voidrefuse()const;
voidland(inttime)const;
voidfly(inttime)const;
intstarted()const;
private:
intflt_num;
intclock_start;
Plane_statusstate;
};
voidinitialize(int&end_time,int&queue_limit,
double&arrival_rate,double&departure_rate)
/*
Pre:
Theuserspecifiesthenumberoftimeunitsinthesimulation,
themaximalqueuesizespermitted,
andtheexpectedarrivalanddepartureratesfortheairport.
Post:
Theprogramprintsinstructionsandinitializestheparameters