Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Queue.h----------------------------------------
- #include "Plane.h"
- #include "Utility.h"
- #pragma once
- const int maksique = 10;
- class Extended_Queue {
- public:
- Extended_Queue();
- bool empty() const;
- Error_code serve();
- Error_code append(const Plane &item);
- Error_code retrieve(Plane &item) const;
- bool full();
- Error_code clear(Extended_Queue &q);
- Error_code serveAndRetrieve(Plane &item);
- int size();
- protected:
- int count;
- int front, rear;
- Plane entry[maksique];
- };
- //-----------------------------------------------------------------------------------
- //Queue.cpp
- #include "Queue.h"
- Extended_Queue::Extended_Queue()
- /*
- Post: The Queue is initialized to be empty.
- */
- {
- count = 0;
- rear = maksique - 1;
- front = 0;
- }
- bool Extended_Queue::empty() const
- /*
- Post: Return true if the Queue is empty, otherwise return false.
- */
- {
- return count == 0;
- }
- Error_code Extended_Queue::append(const Plane &item)
- /*
- Post: item is added to the rear of the Queue. If the Queue is full
- return an Error_code of overflow and leave the Queue unchanged.
- */
- {
- if (count >= maksique) return overflow;
- count++;
- rear = ((rear + 1) == maksique) ? 0 : (rear + 1);
- entry[rear] = item;
- return success;
- }
- Error_code Extended_Queue::serve()
- /*
- Post: The front of the Queue is removed. If the Queue
- is empty return an Error_code of underflow.
- */
- {
- if (count <= 0) return underflow;
- count--;
- front = ((front + 1) == maksique) ? 0 : (front + 1);
- return success;
- }
- Error_code Extended_Queue::retrieve(Plane &item) const
- /*
- Post: The front of the Queue retrieved to the output
- parameter item. If the Queue is empty return an Error_code of underflow.
- */
- {
- if (count <= 0) return underflow;
- item = entry[front];
- return success;
- }
- Error_code Extended_Queue::serveAndRetrieve(Plane &item)
- {
- if (count <= 0) return underflow;
- count--;
- item=entry[front];
- front = ((front + 1) == maksique) ? 0 : (front + 1);
- return success;
- }
- Error_code Extended_Queue::clear(Extended_Queue &q) {
- while (!q.empty())
- q.serve();
- return success;
- }
- bool Extended_Queue::full()
- {
- if (count == maksique) {
- return true;
- } else {
- return false;
- }
- }
- int Extended_Queue::size() {
- return count;
- }
- //-------------------------------------------------------------------------
- //runway.h
- #pragma once
- #include "Plane.h"
- #include "Queue.h"
- enum Runway_activity {idle, land, takeofff};
- 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; // number of planes asking to land
- int num_takeoff_requests; // number of planes asking to take off
- int num_landings; // number of planes that have landed
- int num_takeoffs; // number of planes that have taken off
- int num_land_accepted; // number of planes queued to land
- int num_takeoff_accepted; // number of planes queued to take off
- int num_land_refused; // number of landing planes refused
- int num_takeoff_refused; // number of departing planes refused
- int land_wait; // total time of planes waiting to land
- int takeoff_wait; // total time of planes waiting to take off
- int idle_time; // total time runway is idle
- };
- //---------------------------------------------------------------------------------------------------
- //runway.cpp
- #include "Plane.h"
- #include "Runway.h"
- #include <iostream>
- Runway::Runway(int limit)
- /*
- Post: The Runway data members are initialized to record no
- prior Runway use and to record the limit on queue sizes.
- */
- {
- 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;
- }
- Error_code Runway::can_land(const Plane ¤t)
- /*
- Post: If possible, the Plane current is added to the
- landing Queue; otherwise, an Error_code of overflow is
- returned. The Runway statistics are updated.
- Uses: class Extended_queue.
- */
- {
- 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)
- /*
- Post: If possible, the Plane current is added to the
- takeoff Queue; otherwise, an Error_code of overflow is
- returned. The Runway statistics are updated.
- Uses: class Extended_queue.
- */
- {
- 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;
- }
- Runway_activity Runway::activity(int time, Plane &moving)
- /*
- Post: If the landing Queue has entries, its front
- Plane is copied to the parameter moving
- and a result land is returned. Otherwise,
- if the takeoff Queue has entries, its front
- Plane is copied to the parameter moving
- and a result takeoff is returned. Otherwise,
- idle is returned. Runway statistics are updated.
- Uses: class Extended_queue.
- */
- {
- 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 = takeofff;
- takeoff.serve();
- }
- else {
- idle_time++;
- in_progress = idle;
- }
- return in_progress;
- }
- void Runway::shut_down(int time) const
- /*
- Post: Runway usage statistics are summarized and printed.
- */
- {
- std::cout << "Simulation has concluded after " << time << " time units." << std::endl
- << "Total number of planes processed "
- << (num_land_requests + num_takeoff_requests) << std::endl
- << "Total number of planes asking to land "
- << num_land_requests << std::endl
- << "Total number of planes asking to take off "
- << num_takeoff_requests << std::endl
- << "Total number of planes accepted for landing "
- << num_land_accepted << std::endl
- << "Total number of planes accepted for takeoff "
- << num_takeoff_accepted << std::endl
- << "Total number of planes refused for landing "
- << num_land_refused << std::endl
- << "Total number of planes refused for takeoff "
- << num_takeoff_refused << std::endl
- << "Total number of planes that landed "
- << num_landings << std::endl
- << "Total number of planes that took off "
- << num_takeoffs << std::endl
- << "Total number of planes left in landing queue "
- << landing.size() << std::endl // ONGELMAONGELMAONGELMA
- << "Total number of planes left in takeoff queue "
- << takeoff.size() << std::endl // ONGELMAONGELMAONGELMA
- ;
- std::cout << "Percentage of time runway idle "
- << 100.0 * ((float)idle_time) / ((float)time) << "%" << std::endl;
- std::cout << "Average wait in landing queue "
- << ((float)land_wait) / ((float)num_landings) << " time units";
- std::cout << std::endl << "Average wait in takeoff queue "
- << ((float)takeoff_wait) / ((float)num_takeoffs)
- << " time units" << std::endl;
- std::cout << "Average observed rate of planes wanting to land "
- << ((float)num_land_requests) / ((float)time)
- << " per time unit" << std::endl;
- std::cout << "Average observed rate of planes wanting to take off "
- << ((float)num_takeoff_requests) / ((float)time)
- << " per time unit" << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement