Write an application that simulates a not-so-busy airport, where different kinds
ID: 3568219 • Letter: W
Question
Write an application that simulates a not-so-busy airport, where different kinds of aircrafts, such as military and civilian helicopters, passenger airplanes, and military aircrafts, can take off and land.
According to wikipedia, "An aircraft is a machine that is able to fly by gaining support from the air...", which means they are all flyable.
These aircrafts have lots of common attaributes and behavior; however, they differ in how they:
Write classes (both concrete and abstract) to implement the aircraft objects. Be sure to implement all necessary methods.
Write the Flyable interface.
The simulator class simulates (it is similar to a tester) the operation of this airport for one day.
It randomly creates different kinds of aircrafts and allows them to takeoff and land. The airport only handles two different helicopters, one passenger airplane, and one military aircraft every day.
Explanation / Answer
#include #include #include #include #include #include #include #define MAX 3 #define ARRIVE 0 #define DEPART 1 struct plane { int id ; int tm ; } ; struct queue { int count ; int front ; int rear ; struct plane p[MAX] ; } ; void initqueue ( struct queue * ) ; void addqueue ( struct queue *, struct plane ) ; struct plane delqueue ( struct queue * ) ; int size ( struct queue ) ; int empty ( struct queue ) ; int full ( struct queue ) ; void initqueue ( struct queue *pq ) { pq -> count = 0 ; pq -> front = 0 ; pq -> rear = -1 ; } void addqueue ( struct queue *pq, struct plane item ) { if ( pq -> count >= MAX ) { printf ( " Queue is full. " ) ; return ; } ( pq -> count )++ ; pq -> rear = ( pq -> rear + 1 ) % MAX ; pq -> p[pq -> rear] = item ; } struct plane delqueue ( struct queue *pq ) { struct plane p1 ; if ( pq -> count count )-- ; p1 = pq -> p[pq -> front] ; pq -> front = ( pq -> front + 1 ) % MAX ; } return p1 ; } int size ( struct queue q ) { return q.count ; } int empty ( struct queue q ) { return ( q.count = MAX ) ; } struct airport { struct queue landing ; struct queue takeoff ; struct queue *pl ; struct queue *pt ; int idletime ; int landwait, takeoffwait ; int nland, nplanes, nrefuse, ntakeoff ; struct plane pln ; } ; void initairport ( struct airport * ) ; void start ( int *, double *, double * ) ; void newplane ( struct airport *, int, int ) ; void refuse ( struct airport *, int ) ; void land ( struct airport *, struct plane, int ) ; void fly ( struct airport *, struct plane, int ) ; void idle ( struct airport *, int ) ; void conclude ( struct airport *, int ) ; int randomnumber ( double ) ; void apaddqueue ( struct airport *, char ) ; struct plane apdelqueue ( struct airport *, char ) ; int apsize ( struct airport, char ) ; int apfull ( struct airport, char ) ; int apempty ( struct airport, char ) ; void myrandomize ( ) ; void initairport ( struct airport *ap ) { initqueue ( &( ap-> landing ) ) ; initqueue ( &( ap -> takeoff ) ) ; ap -> pl = &( ap -> landing ) ; ap -> pt = &( ap -> takeoff ) ; ap -> nplanes = ap -> nland = ap -> ntakeoff = ap -> nrefuse = 0 ; ap -> landwait = ap -> takeoffwait = ap -> idletime = 0 ; } void start ( int *endtime, double *expectarrive, double *expectdepart ) { int flag = 0 ; char wish ; printf ( " Program that simulates an airport with only one runway. " ) ; printf ( "One plane can land or depart in each unit of time. " ) ; printf ( "Up to %d planes can be waiting to land or take off at any time. ", MAX ) ; printf ( "How many units of time will the simulation run?" ) ; scanf ( "%d", endtime ) ; myrandomize( ) ; do { printf ( " Expected number of arrivals per unit time? " ) ; scanf ( "%lf", expectarrive ) ; printf ( " Expected number of departures per unit time? " ) ; scanf ( "%lf", expectdepart ) ; if ( *expectarrive < 0.0 || *expectdepart < 0.0 ) { printf ( "These numbers must be nonnegative. " ) ; flag = 0 ; } else { if ( *expectarrive + *expectdepart > 1.0 ) { printf ( "The airport will become saturated. Read new numbers? " ) ; fflush ( stdin ) ; scanf ( "%c", &wish ) ; if ( tolower ( wish ) == 'y' ) flag = 0 ; else flag = 1 ; } else flag = 1 ; } } while ( flag == 0 ) ; } void newplane ( struct airport *ap, int curtime, int action ) { ( ap -> nplanes )++ ; ap -> pln.id = ap -> nplanes ; ap -> pln.tm = curtime ; switch ( action ) { case ARRIVE : printf ( " " ) ; printf ( "Plane %d ready to land. ", ap -> nplanes ) ; break ; case DEPART : printf ( " Plane %d ready to take off. ", ap -> nplanes ) ; break ; } } void refuse ( struct airport *ap, int action ) { switch ( action ) { case ARRIVE : printf ( " plane %d directed to another airport. ", ap -> pln.id ) ; break ; case DEPART : printf ( " plane %d told to try later. ", ap -> pln.id ) ; break ; } ( ap -> nrefuse )++ ; } void land ( struct airport *ap, struct plane pl, int curtime ) { int wait ; wait = curtime - pl.tm ; printf ( "%d: Plane %d landed ", curtime, pl.id ) ; printf ( "in queue %d units ", wait ) ; ( ap -> nland ) ++ ; ( ap -> landwait ) += wait ; } void fly ( struct airport *ap, struct plane pl, int curtime ) { int wait ; wait = curtime - pl.tm ; printf ( "%d: Plane %d took off ", curtime, pl.id ) ; printf ( "in queue %d units ", wait ) ; ( ap -> ntakeoff )++ ; ( ap -> takeoffwait ) += wait ; } void idle ( struct airport *ap, int curtime ) { printf ( "%d: Runway is idle. ", curtime ) ; ap -> idletime++ ; } void conclude ( struct airport *ap, int endtime ) { printf ( " Simulation has concluded after %d units. ", endtime ) ; printf ( " Total number of planes processed: %d ", ap -> nplanes ) ; printf ( " Number of planes landed: %d ", ap -> nland ) ; printf ( " Number of planes taken off: %d ", ap -> ntakeoff ) ; printf ( " Number of planes refused use: %d ", ap -> nrefuse ) ; printf ( " Number left ready to land: %d ", apsize ( *ap, 'l' ) ) ; printf ( " Number left ready to take off: %d ", apsize ( *ap, 't' ) ) ; if ( endtime > 0 ) printf ( " Percentage of time runway idle: %lf ", ( ( double ) ap -> idletime / endtime ) * 100.0 ) ; if ( ap -> nland > 0 ) printf ( " Average wait time to land: %lf ", ( ( double ) ap -> landwait / ap -> nland ) ) ; if ( ap -> ntakeoff > 0 ) printf ( " Average wait time to take off: %lf ", ( ( double ) ap -> takeoffwait / ap -> ntakeoff ) ) ; } int randomnumber ( double expectedvalue ) { int n = 0 ; double em ; double x ; em = exp ( -expectedvalue ) ; x = rand( ) / ( double ) INT_MAX ; while ( x > em ) { n++ ; x *= rand( ) / ( double ) INT_MAX ; } return n ; } void apaddqueue ( struct airport *ap, char type ) { switch ( tolower( type ) ) { case'l' : addqueue ( ap -> pl, ap -> pln ) ; break ; case't' : addqueue ( ap -> pt, ap -> pln ) ; break ; } } struct plane apdelqueue ( struct airport *ap, char type ) { struct plane p1 ; switch ( tolower ( type ) ) { case'l' : p1 = delqueue ( ap -> pl ) ; break ; case't' : p1 = delqueue ( ap -> pl ) ; break ; } return p1 ; } int apsize ( struct airport ap, char type ) { switch ( tolower ( type ) ) { case'l' : return ( size ( *( ap.pl ) ) ) ; case't' : return ( size ( *( ap.pt ) ) ) ; } return 0 ; } int apfull ( struct airport ap, char type ) { switch ( tolower ( type ) ) { case'l' : return ( full ( *( ap.pl ) ) ) ; case't' : return ( full ( *( ap.pt ) ) ) ; } return 0 ; } int apempty ( struct airport ap, char type ) { switch ( tolower ( type ) ) { case'l' : return ( empty ( *( ap.pl ) ) ) ; case't' : return ( empty ( *( ap.pt ) ) ) ; } return 0 ; } void myrandomize( ) { srand ( ( unsigned int ) ( time ( NULL ) % 10000 ) ) ; } void main( ) { struct airport a ; int i, pri, curtime, endtime ; double expectarrive, expectdepart ; struct plane temp ; clrscr( ) ; initairport ( &a ); start ( &endtime, &expectarrive, &expectdepart ) ; for ( curtime = 1 ; curtimeRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.