SHARE
TWEET

Untitled

a guest Nov 13th, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4. // ******************************* VERY IMPORTANT *************************************************
  5. // THE FOLLOWING DECLARATIONS SHOULD NOT BE DELETED OR CHANGED
  6. // REMOVING OR CHANGING ANY OF THEM WILL STOP YOUR PROGRAM FROM
  7. // COMPILING SUCCESSFULLY WITH THE TEST HARNESS
  8. // PLEASE USE THEM FOR THE PURPOSE FOR WHICH THEY WERE CREATED
  9. //*************************************************************************************************
  10.  
  11. #include "air.h"
  12. #include <stdio.h>
  13.  
  14. // This is the BookingsTable two dimensional array,
  15. // it is used to record information about all flight bookings,
  16. // the table has MaxCapacity rows because this is the maximum number of passengers who can have
  17. // valid bookings on this aircraft at any one time,
  18. // the table has four columns used as follows:
  19. // column 0 stores the flight booking number,
  20. // column 1 stores the travel class (1-4),
  21. // column 2 stores the number of passengers travelling together in this flight booking (1-9),
  22. // column 3 is a flag that indicates if seats have been reserved for this booking:
  23. // 0=seats not reserved, 1=seats reserved
  24. int BookingsTable[MaxCapacity][4];
  25. int NumberBookings; // the actual number of flight bookings made on this flight
  26.  
  27.  
  28. // This is the SeatMap two dimensional array,
  29. // it represents the seats of the aircraft,
  30. // it has NumberSeatRows (26) rows because there are 26 seat rows in the aircraft,
  31. // it has 7 columns:
  32. // column 0 contains the actual seat row number because there are missing row numbers
  33. // columns 1,2,3,4,5, and 6 store the booking numbers of the passengers
  34. // who reserved the seats of this row,
  35. // if a seat is free the cell contains -1
  36. // initially all seats are free hence all cells in columns 1-6 should be initialised to -1
  37. int SeatMap[NumberSeatRows][7];
  38.  
  39. int BookingNumberCounter; // a counter for creating unique booking numbers  
  40. int NumberFirst;        // a counter for the number of passengers who booked first class
  41. int NumberPremium;      // a counter for the number of passengers who booked premium class
  42. int NumberEconomy;      // a counter for the number of passengers who booked economy class
  43. int NumberBudget;       // a counter for the number of passengers who booked budget class
  44.  
  45. // YOU MAY ADD ANY ADDITIONAL GLOBAL VARIABLES THAT YOU THINK ARE NEEDED BELOW THIS LINE
  46. //--------------------------------------------------------------------------------------
  47.  
  48.  
  49.  
  50.  
  51. // ******************************* VERY IMPORTANT *************************************************
  52. // THE FOLLOWING FUNCTION SKELETONS SHOULD NOT BE DELETED OR THEIR SIGNATURES CHANGED
  53. // REMOVING ANY OF THEM OR CHANGING THEIR RETURN TYPE OR PARAMETER LIST WILL STOP YOUR PROGRAM FROM
  54. // COMPILING SUCCESSFULLY WITH THE TEST HARNESS
  55. //*************************************************************************************************
  56.  
  57. // This function initialises all counters and variables to their correct initial values
  58. // it should be called once at the beginning of the main function
  59. void InitialiseBookings ()
  60. {
  61.    
  62.     BookingNumberCounter = MinBookingReferenceNumber;
  63.     NumberBookings = 0; // initially there are no bookings
  64.     NumberFirst=0;      // obviously there are 0 passengers in this travel class
  65.     NumberPremium=0;    // ditto
  66.     NumberEconomy = 0;  // ditto
  67.     NumberBudget = 0;   // ditto
  68.     // YOU MAY ADD ANY OTHER INITIALISATIONS THAT YOU THINK ARE NECESSARY BELOW THIS LINE
  69. } // END OF THE InitialiseAll FUNCTION
  70.  
  71. // This function returns a new booking number every time it is called,
  72. // the first time this function is called it returns 1, the second time it returns 2, etc
  73. // the maximum booking number that can be returned is 999,
  74. // if all booking numbers from 1 to 999 have been used the function returns -1
  75. // as indication of inability to create more booking numbers
  76. //
  77. int newBookingNumber ()
  78. {
  79.     BookingNumberCounter++;
  80.     return BookingNumberCounter < 1001 ? BookingNumberCounter-1 : -1;
  81. }  // END OF THE newBookingNumber FUNCTION
  82.  
  83. // This function returns 1 (true) if there is enough capacity in a travel_class to seat n more passengers,
  84. // where n is the number of passengers who need to book a flight in this travel class
  85. // otherwise if this travel class is full it returns 0 (false),
  86. // if the travel class is not a valid value (i.e. not a value between 1-4) the function returns -1
  87. int hasCapacity (int travel_class, int n)
  88. {
  89.     switch(travel_class)
  90.     {
  91.         case 1 :
  92.             if ((n + NumberFirst) > MaxSeatsFirst)
  93.                 return 0;
  94.             else
  95.                 return 1;
  96.             break;
  97.         case 2 :
  98.             if ((n + NumberPremium) > MaxSeatsPremium)
  99.                 return 0;
  100.             else
  101.                 return 1;
  102.             break;
  103.         case 3:
  104.         case 4:
  105.             if ((n + NumberEconomy + NumberBudget) > MaxSeatsEconomy)
  106.                 return 0;
  107.             else
  108.                 return 1;
  109.             break;         
  110.         default :
  111.             return -1;
  112.     }
  113. } // END OF THE hasCapacity FUNCTION
  114.  
  115. // This function adds a new booking to the BookingsTable,
  116. // and update all relevant passenger counters,
  117. // when a booking is added no specific seats are reserved for this booking
  118. // the function returns -1 if something goes wrong (e.g. travel_class is an invalid number)
  119. // if successful, the function returns 1
  120. int addBooking(int booking_number, int travel_class, int number_passengers)
  121. {
  122.     if (booking_number < 1 || booking_number > 999 || travel_class < 1 || travel_class > 4 || number_passengers < 1 || number_passengers > 9)
  123.         return -1;
  124.     BookingsTable[NumberBookings][0] = booking_number;
  125.     BookingsTable[NumberBookings][1] = travel_class;
  126.     BookingsTable[NumberBookings][2] = number_passengers;
  127.     BookingsTable[NumberBookings][3] = 0;
  128.     NumberBookings++;
  129.     if (travel_class == 1)
  130.         NumberFirst += number_passengers;
  131.     if (travel_class == 2)
  132.         NumberPremium += number_passengers;
  133.     if (travel_class == 3)
  134.         NumberEconomy += number_passengers;
  135.     if (travel_class == 4)
  136.         NumberBudget += number_passengers;
  137.     return 1;
  138.  
  139. } // END OF THE hasCapacity FUNCTION
  140.  
  141. // This function searches for a booking in the BookingsTable
  142. // using the booking number as a search key
  143. // and returns the zero-based row index of this booking in the BookingsTable
  144. // if the booking cannot be found the function returns -1
  145. int findBooking (int booking_number)
  146. {
  147.     int i;
  148.     for (i = 0; i < sizeof(BookingsTable[0]); i++)
  149.         if (BookingsTable[i][0] == booking_number)
  150.             break;
  151.     if (i <  sizeof(BookingsTable[0]))
  152.         return i;
  153.     else
  154.         return -1;
  155. } // END OF THE findBooking FUNCTION
  156.  
  157.  
  158. // This function returns the number of passengers
  159. // who are travelling under one booking using information from the BookingsTable
  160. // or -1 if this booking cannot be found
  161. int numberPassengers (int booking_number)
  162. {
  163.     int i;
  164.     for (i = 0; i < sizeof(BookingsTable[0]); i++)
  165.         if (BookingsTable[i][0] == booking_number)
  166.             break;
  167.     if (i <  sizeof(BookingsTable[0]))
  168.         return BookingsTable[i][2];
  169.     else
  170.         return -1;
  171. } // END OF THE numberPassengers FUNCTION
  172.  
  173. // This function returns 1 if seats have been reserved for this booking
  174. // and returns 0 if no seats are reserved for this booking
  175. // -1 if this booking cannot be found
  176. int seatsAreReserved (int booking_number)
  177. {
  178.     int i;
  179.     for (i = 0; i < sizeof(BookingsTable[0]); i++)
  180.         if (BookingsTable[i][0] == booking_number)
  181.             break;
  182.     if (i <  sizeof(BookingsTable[0]))
  183.         return BookingsTable[i][3];
  184.     else
  185.         return -1;
  186. } // END OF THE seatsAreReserved FUNCTION
  187.  
  188.  
  189. // This function returns the travel class of a booking
  190. // and -1 if this booking cannot be found
  191. int travelClass (int booking_number)
  192. {
  193.     int i;
  194.     for (i = 0; i < sizeof(BookingsTable[0]); i++)
  195.         if (BookingsTable[i][0] == booking_number)
  196.             break;
  197.     if (i <  sizeof(BookingsTable[0]))
  198.         return BookingsTable[i][1];
  199.     else
  200.         return -1;
  201. } // END OF THE travelClass FUNCTION
  202.  
  203.  
  204. // always returns 1
  205. int showBookingsTable()
  206. {
  207.     int a, n;
  208.     printf("Booking Number   Travel Class   Number of Passengers   Seats? \n");
  209.     for (a = 0; a < 26; a++)
  210.         printf("       %i           %i           %i          %i \n" ,BookingsTable[a][0],BookingsTable[a][1],BookingsTable[a][2],BookingsTable[a][3]);
  211.     return 1;
  212. } // END OF THE showBookingsTable FUNCTION
  213.  
  214.  
  215. // This function returns 1 if row_number is missing from the seat plan (e.g. row number 13)
  216. // otherwise it returns 0
  217. int rowIsMissing (int row_number)
  218. {
  219.     if (row_number >= 4 && row_number <= 6 || row_number == 9 ||row_number >= 13 && row_number <= 19 || row_number == 33)
  220.         return 1;
  221.     return 0;
  222. } // END OF THE rowIsMissing FUNCTION
  223.  
  224.  
  225. // This function returns 1 if the seat number specified by the parameters 'row' and 'letter' is a valid seat
  226. // on the aircraft for the given travel_class,
  227. // if the seat number is not valid the function returns 0,
  228. // this function does not check whether the seat is taken (reserved for someone) or not,
  229. // The function returns -1 if travel_class invalid (not between 1-4)
  230. // since budget class passengers cannot reserve a seat, the function returns 0 if the travel class is budget (4)
  231. int isValidSeat (int travel_class, int row, char letter)
  232. {
  233.     switch(travel_class)
  234.     {
  235.         case 1:
  236.             if (row == 1 || row == 3 || row == 2)
  237.                 if (letter == 'A' || letter == 'B' || letter == 'E' || letter == 'F')
  238.                     return 1;
  239.             return 0;
  240.         case 2:
  241.             if (row >= 7 && row <= 21 && row != 9)
  242.                 if (row > 12 && row < 20)
  243.                     return 0;
  244.                 if (letter >= 'A' && letter <= 'F')
  245.                     return 1;
  246.         case 3:
  247.             if (row >= 22 && row <= 38 && row != 33 )
  248.                 if (letter >= 'A' && letter <= 'F')
  249.                     return 1;
  250.                 return 0;
  251.             break;
  252.         case 4:
  253.             return 0;
  254.         default:
  255.             return -1;
  256.     }
  257. } // END OF THE isValidSeat FUNCTION
  258.  
  259. // This function returns an integer between 1-6 corresponding to the letters A-F
  260. // it is useful for indexing the columns 1-6 of the SeatMap table
  261. int letterToColumnIndex (char letter)
  262. {
  263.     if (letter == 'A') return 1;
  264.     if (letter == 'B') return 2;
  265.     if (letter == 'C') return 3;
  266.     if (letter == 'D') return 4;
  267.     if (letter == 'E') return 5;
  268.     if (letter == 'F') return 6;
  269.     return 0;
  270. } // END OF THE letterToColumnIndex FUNCTION
  271.  
  272.  
  273. // This function initialises the SeatMap by initialising column 0 to the actual seat row number
  274. // and assigning -1 to all 'seats'
  275. // always returns 1
  276. int InitialiseSeatMap ()
  277. {
  278.     int n, a;
  279.     for (a = 0; a < 26; a++)
  280.         for (n = 1; n < 7; n++)
  281.             SeatMap[a][n] = -1;
  282.     for (a = 0; a < 3; a++)
  283.         SeatMap[a][0] = a+1;
  284.     SeatMap[3][0]=7;
  285.     SeatMap[4][0]=8;
  286.     for (a = 5; a < 8; a++)
  287.         SeatMap[a][0] = a+5;
  288.     for (a = 8; a < 21; a++)
  289.         SeatMap[a][0] = a+12;
  290.     for (a = 21; a < 26; a++)
  291.         SeatMap[a][0] = a+13;
  292.     return 1;
  293. }  // END OF THE InitialiseSeatMap FUNCTION
  294.  
  295. // This function returns the row index in the SeatMap array for a given aircraft seat row
  296. // returns -1 if this is row does not exist in the aircraft seat map
  297. // for example if row= 11 the function returns 6
  298. int rowIndex (int row)
  299. {
  300.     if (row >= 1 && row <= 3)
  301.         return row - 1;
  302.     if (row == 7 || row == 8)
  303.         return row  - 4;
  304.     if (row >= 10 && row <= 12)
  305.         return row - 5;
  306.     if (row >= 20 && row <= 32)
  307.         return row - 12;
  308.     if (row >= 34 && row <= 38)
  309.         return row - 13;
  310.     return -1;
  311. } // END OF THE rowIndex FUNCTION
  312.  
  313. // returns 1 if the seat specified by row and letter is reserved (for anyone)
  314. // and 0 if the seat is not reserved
  315. // the function returns -1 if the seat specified by row,letter is not valid
  316. int seatIsReserved (int travel_class , int row , char letter)
  317. {
  318.     if (isValidSeat == 0)
  319.         return -1;
  320.     if (SeatMap[rowIndex(row)][letterToColumnIndex(letter)] == -1)
  321.         return 0;
  322.     return 1;
  323. } // END OF THE seatIsReserved FUNCTION
  324.  
  325. // This function reserves the seat specified by row and letter to a booking_number
  326. // The function returns -1 if the seat specification is not valid or if the booking does not exist
  327. // otherwise it returns 1
  328. int reserveSeat (int booking_number, int travel_class , int row, char letter)
  329. {
  330.     if (findBooking(booking_number) == -1 || isValidSeat == 0 || isValidSeat == -1)
  331.         return -1;
  332.     BookingsTable[booking_number - 1][3] = 1;
  333.     SeatMap[rowIndex(row)][letterToColumnIndex(letter)] = booking_number;
  334.     return 1;
  335. } // END OF THE reserveSeat FUNCTION
  336.  
  337. int FindSymbolFirst(int seat, int booking_number)
  338. {
  339.     if (seat == -1)
  340.         return 'O';
  341.     if (seatsAreReserved(booking_number) == 1)
  342.         return '#';
  343.     return '*';
  344. }
  345.  
  346. int FindSymbolPremium(int seat, int booking_number)
  347. {
  348.     if (seat == -1)
  349.         return 'o';
  350.     if (seatsAreReserved(booking_number) == 1)
  351.         return '#';
  352.     return '*';
  353. }
  354.  
  355. int FindSymbolEconomy(int seat, int booking_number)
  356. {
  357.     if (seat == -1)
  358.         return '.';
  359.     if (seatsAreReserved(booking_number) == 1)
  360.         return '#';
  361.     return '*';
  362. }
  363.  
  364.  
  365. // This function displays the SeatMap of the aircraft
  366. // vacant first class seats are displayed as capital O
  367. // vacant premium class seats are shown as small o
  368. // vacant economy seats are shown as .
  369. // seats reserved to the given booking_number are shown as #
  370. // seats reserved for all other bookings are shown as *
  371. int ShowSeatMap (int booking_number)
  372. {
  373.     int a,n, Tclass;
  374.     Tclass = travelClass(booking_number);
  375.     printf("--- FRONT OF AIRCRAFT --- \n");
  376.     printf("First Class Cabin \n");
  377.     printf("    A   B      E   F  \n");
  378.     for (a = 0; a < 3; a++)
  379.     {
  380.         printf("%i  " ,SeatMap[a][0]);
  381.         printf("%c   %c      %c   %c   \n" ,FindSymbolFirst(SeatMap[a][1], booking_number), FindSymbolFirst(SeatMap[a][2], booking_number), FindSymbolFirst(SeatMap[a][5], booking_number), FindSymbolFirst(SeatMap[a][6], booking_number));
  382.     }
  383.     printf("--------------    ------\n");
  384.     printf("Main Cabin \n");
  385.     printf("    A  B  C  D  E  F  \n");
  386.     for (a = 3; a < 10; a++)
  387.     {
  388.         printf("%i  ",SeatMap[a][0]);
  389.         printf("%c  %c  %c   %c  %c  %c \n" ,FindSymbolPremium(SeatMap[a][1], booking_number),FindSymbolPremium(SeatMap[a][2], booking_number),FindSymbolPremium(SeatMap[a][3], booking_number),FindSymbolPremium(SeatMap[a][4], booking_number),FindSymbolPremium(SeatMap[a][5], booking_number),FindSymbolPremium(SeatMap[a][6], booking_number));
  390.     }  
  391.     for (a = 10; a < 26; a++)
  392.     {
  393.         printf("%i  ",SeatMap[a][0]);
  394.         printf("%c  %c  %c   %c  %c  %c \n" ,FindSymbolEconomy(SeatMap[a][1], booking_number),FindSymbolEconomy(SeatMap[a][2], booking_number),FindSymbolEconomy(SeatMap[a][3], booking_number),FindSymbolEconomy(SeatMap[a][4], booking_number),FindSymbolEconomy(SeatMap[a][5], booking_number),FindSymbolEconomy(SeatMap[a][6], booking_number));
  395.     }
  396.     printf("\n--- REAR OF AIRCRAFT ---");
  397. } // END OF ShowSeatMap FUNCTION
  398.  
  399. // This function cancels all seats reserved for a booking  
  400. // The function returns -1 if the booking does not exist
  401. // otherwise it returns 1
  402. int cancelSeatReservation (int booking_number)
  403. {
  404.     if (findBooking(booking_number) == -1)
  405.         return -1;
  406.     BookingsTable[findBooking(booking_number)][3] = 0;
  407.     for (int a = 0; a < 26; a++)
  408.         for (int i = 1; i < 7; i++)
  409.             if (SeatMap[a][i] == booking_number)
  410.                 SeatMap[a][i] = -1;
  411.     return 1;
  412. } // END OF cancelSeatReservation FUNCTION
  413.  
  414. // This function cancels a booking and all seats reserved for this booking  
  415. // The function returns -1 if the booking does not exist
  416. // otherwise it returns 1
  417. int cancelBooking (int booking_number)
  418. {
  419.     if (findBooking(booking_number) == -1)
  420.         return -1;
  421.     BookingsTable[booking_number - 1][3] = 0;
  422.     cancelSeatReservation(booking_number);
  423.     switch(travelClass(booking_number))
  424.     {
  425.         case 1:
  426.             NumberFirst -= numberPassengers(booking_number);
  427.             break;
  428.         case 2:
  429.             NumberPremium -= numberPassengers(booking_number);
  430.             break;
  431.         case 3:
  432.             NumberEconomy -= numberPassengers(booking_number);
  433.             break;
  434.         case 4:
  435.             NumberBudget -= numberPassengers(booking_number);
  436.             break;
  437.            
  438.     }
  439.     for (int i = findBooking(booking_number); i < 12; i++)     
  440.     {
  441.         BookingsTable[i][0] = BookingsTable[i+1][0];
  442.         BookingsTable[i][1] = BookingsTable[i+1][1];
  443.         BookingsTable[i][2] = BookingsTable[i+1][2];
  444.         BookingsTable[i][3] = BookingsTable[i+1][3];
  445.     }
  446.     NumberBookings--;
  447.     return 1;
  448. } // END OF cancelBooking FUNCTION
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top