Advertisement
Guest User

Kikkel

a guest
Nov 23rd, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.91 KB | None | 0 0
  1. //Queue.h----------------------------------------
  2.  
  3. #include "Plane.h"
  4. #include "Utility.h"
  5. #pragma once
  6. const int maksique = 10;
  7.  
  8.  
  9.  
  10. class Extended_Queue {
  11. public:
  12.  Extended_Queue();
  13.    bool empty() const;
  14.    Error_code serve();
  15.    Error_code append(const Plane &item);
  16.    Error_code retrieve(Plane &item) const;
  17.    bool full();
  18.    Error_code clear(Extended_Queue &q);
  19.    Error_code serveAndRetrieve(Plane &item);
  20.    int size();
  21.  
  22.  
  23.  
  24. protected:
  25.    int count;
  26.    int front, rear;
  27.    Plane entry[maksique];
  28. };
  29.  
  30.  
  31. //-----------------------------------------------------------------------------------
  32. //Queue.cpp
  33.  
  34.  
  35. #include "Queue.h"
  36.  
  37. Extended_Queue::Extended_Queue()
  38. /*
  39. Post: The Queue is initialized to be empty.
  40. */
  41. {
  42.    count = 0;
  43.    rear = maksique - 1;
  44.    front = 0;
  45. }
  46.  
  47.  
  48. bool Extended_Queue::empty() const
  49. /*
  50. Post: Return true if the Queue is empty, otherwise return false.
  51. */
  52. {
  53.    return count == 0;
  54. }
  55.  
  56.  
  57. Error_code Extended_Queue::append(const Plane &item)
  58. /*
  59. Post: item is added to the rear of the Queue. If the Queue is full
  60. return an Error_code of overflow and leave the Queue unchanged.
  61. */
  62.  
  63. {
  64.    if (count >= maksique) return overflow;
  65.    count++;
  66.    rear = ((rear + 1) == maksique) ? 0 : (rear + 1);
  67.    entry[rear] = item;
  68.    return success;
  69. }
  70.  
  71.  
  72. Error_code Extended_Queue::serve()
  73. /*
  74. Post: The front of the Queue is removed. If the Queue
  75. is empty return an Error_code of underflow.
  76. */
  77.  
  78. {
  79.    if (count <= 0) return underflow;
  80.    count--;
  81.    front = ((front + 1) == maksique) ? 0 : (front + 1);
  82.    return success;
  83. }
  84.  
  85.  
  86. Error_code Extended_Queue::retrieve(Plane &item) const
  87. /*
  88. Post: The front of the Queue retrieved to the output
  89.       parameter item. If the Queue is empty return an Error_code of underflow.
  90. */
  91.  
  92. {
  93.    if (count <= 0) return underflow;
  94.    item = entry[front];
  95.    return success;
  96. }
  97.  
  98.  
  99.  
  100. Error_code Extended_Queue::serveAndRetrieve(Plane &item)
  101.  
  102.  
  103. {
  104.    if (count <= 0) return underflow;
  105.    count--;
  106.    item=entry[front];
  107.    front = ((front + 1) == maksique) ? 0 : (front + 1);
  108.    return success;
  109. }
  110.  
  111. Error_code Extended_Queue::clear(Extended_Queue &q) {
  112.  
  113. while (!q.empty())
  114. q.serve();
  115. return success;
  116.  
  117. }
  118.  
  119. bool Extended_Queue::full()
  120.  
  121.  
  122. {
  123.    if (count == maksique) {
  124.        return true;
  125.    } else {
  126.        return false;
  127.    }
  128.  
  129. }
  130.  
  131. int Extended_Queue::size()  {
  132.  
  133.     return count;
  134.  
  135. }
  136.  
  137.  
  138. //-------------------------------------------------------------------------
  139. //runway.h
  140.  
  141. #pragma once
  142. #include "Plane.h"
  143. #include "Queue.h"
  144. enum Runway_activity {idle, land, takeofff};
  145.  
  146. class Runway {
  147. public:
  148.     Runway(int limit);
  149.     Error_code can_land(const Plane &current);
  150.     Error_code can_depart(const Plane &current);
  151.     Runway_activity activity(int time, Plane &moving);
  152.     void shut_down(int time) const;
  153.  
  154. private:
  155.     Extended_Queue landing;
  156.     Extended_Queue takeoff;
  157.     int queue_limit;
  158.     int num_land_requests;        //  number of planes asking to land
  159.     int num_takeoff_requests;     //  number of planes asking to take off
  160.     int num_landings;             //  number of planes that have landed
  161.     int num_takeoffs;             //  number of planes that have taken off
  162.     int num_land_accepted;        //  number of planes queued to land
  163.     int num_takeoff_accepted;     //  number of planes queued to take off
  164.     int num_land_refused;         //  number of landing planes refused
  165.     int num_takeoff_refused;      //  number of departing planes refused
  166.     int land_wait;                //  total time of planes waiting to land
  167.     int takeoff_wait;             //  total time of planes waiting to take off
  168.     int idle_time;                //  total time runway is idle
  169. };
  170.  
  171.  
  172.  
  173. //---------------------------------------------------------------------------------------------------
  174. //runway.cpp
  175.  
  176. #include "Plane.h"
  177. #include "Runway.h"
  178. #include <iostream>
  179.  
  180.  
  181. Runway::Runway(int limit)
  182. /*
  183. Post:  The Runway data members are initialized to record no
  184. prior Runway use and to record the limit on queue sizes.
  185. */
  186.  
  187. {
  188.     queue_limit = limit;
  189.     num_land_requests = num_takeoff_requests = 0;
  190.     num_landings = num_takeoffs = 0;
  191.     num_land_refused = num_takeoff_refused = 0;
  192.     num_land_accepted = num_takeoff_accepted = 0;
  193.     land_wait = takeoff_wait = idle_time = 0;
  194. }
  195.  
  196.  
  197. Error_code Runway::can_land(const Plane &current)
  198. /*
  199. Post:  If possible, the Plane current is added to the
  200. landing Queue; otherwise, an Error_code of overflow is
  201. returned. The Runway statistics are updated.
  202. Uses:  class Extended_queue.
  203. */
  204.  
  205. {
  206.     Error_code result;
  207.     if (landing.size() < queue_limit)
  208.         result = landing.append(current);
  209.     else
  210.         result = fail;
  211.     num_land_requests++;
  212.  
  213.     if (result != success)
  214.         num_land_refused++;
  215.     else
  216.         num_land_accepted++;
  217.  
  218.     return result;
  219. }
  220.  
  221.  
  222. Error_code Runway::can_depart(const Plane &current)
  223. /*
  224. Post:  If possible, the Plane current is added to the
  225. takeoff Queue; otherwise, an Error_code of overflow is
  226. returned. The Runway statistics are updated.
  227. Uses:  class Extended_queue.
  228. */
  229.  
  230. {
  231.     Error_code result;
  232.     if (takeoff.size() < queue_limit)
  233.         result = takeoff.append(current);
  234.     else
  235.         result = fail;
  236.     num_takeoff_requests++;
  237.     if (result != success)
  238.         num_takeoff_refused++;
  239.     else
  240.         num_takeoff_accepted++;
  241.  
  242.     return result;
  243. }
  244.  
  245.  
  246. Runway_activity Runway::activity(int time, Plane &moving)
  247. /*
  248. Post:  If the landing Queue has entries, its front
  249. Plane is copied to the parameter moving
  250. and a result  land is returned. Otherwise,
  251. if the takeoff Queue has entries, its front
  252. Plane is copied to the parameter moving
  253. and a result  takeoff is returned. Otherwise,
  254. idle is returned. Runway statistics are updated.
  255. Uses:  class Extended_queue.
  256. */
  257.  
  258. {
  259.     Runway_activity in_progress;
  260.     if (!landing.empty()) {
  261.         landing.retrieve(moving);
  262.         land_wait += time - moving.started();
  263.         num_landings++;
  264.         in_progress = land;
  265.         landing.serve();
  266.     }
  267.  
  268.     else if (!takeoff.empty()) {
  269.         takeoff.retrieve(moving);
  270.         takeoff_wait += time - moving.started();
  271.         num_takeoffs++;
  272.         in_progress = takeofff;
  273.         takeoff.serve();
  274.     }
  275.  
  276.     else {
  277.         idle_time++;
  278.         in_progress = idle;
  279.     }
  280.     return in_progress;
  281. }
  282.  
  283. void Runway::shut_down(int time) const
  284. /*
  285. Post: Runway usage statistics are summarized and printed.
  286. */
  287.  
  288. {
  289.     std::cout << "Simulation has concluded after " << time << " time units." << std::endl
  290.         << "Total number of planes processed "
  291.         << (num_land_requests + num_takeoff_requests) << std::endl
  292.         << "Total number of planes asking to land "
  293.         << num_land_requests << std::endl
  294.         << "Total number of planes asking to take off "
  295.         << num_takeoff_requests << std::endl
  296.         << "Total number of planes accepted for landing "
  297.         << num_land_accepted << std::endl
  298.         << "Total number of planes accepted for takeoff "
  299.         << num_takeoff_accepted << std::endl
  300.         << "Total number of planes refused for landing "
  301.         << num_land_refused << std::endl
  302.         << "Total number of planes refused for takeoff "
  303.         << num_takeoff_refused << std::endl
  304.         << "Total number of planes that landed "
  305.         << num_landings << std::endl
  306.         << "Total number of planes that took off "
  307.         << num_takeoffs << std::endl
  308.         << "Total number of planes left in landing queue "
  309.         << landing.size() << std::endl // ONGELMAONGELMAONGELMA
  310.         << "Total number of planes left in takeoff queue "
  311.         << takeoff.size() << std::endl // ONGELMAONGELMAONGELMA
  312. ;
  313.     std::cout << "Percentage of time runway idle "
  314.         << 100.0 * ((float)idle_time) / ((float)time) << "%" << std::endl;
  315.     std::cout << "Average wait in landing queue "
  316.         << ((float)land_wait) / ((float)num_landings) << " time units";
  317.     std::cout << std::endl << "Average wait in takeoff queue "
  318.         << ((float)takeoff_wait) / ((float)num_takeoffs)
  319.         << " time units" << std::endl;
  320.     std::cout << "Average observed rate of planes wanting to land "
  321.         << ((float)num_land_requests) / ((float)time)
  322.         << " per time unit" << std::endl;
  323.     std::cout << "Average observed rate of planes wanting to take off "
  324.         << ((float)num_takeoff_requests) / ((float)time)
  325.         << " per time unit" << std::endl;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement