Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.28 KB | None | 0 0
  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) //Check if there are enough seats in first class
  93. return 0; //when the number of passengers are added
  94. else
  95. return 1;
  96. break;
  97. case 2 :
  98. if ((n + NumberPremium) > MaxSeatsPremium) //Check if there are enough seats in premium
  99. return 0; //when the number of passengers are added
  100. else
  101. return 1;
  102. break;
  103. case 3:
  104. case 4:
  105. if ((n + NumberEconomy + NumberBudget) > MaxSeatsEconomy) //Check if there are enough seats in budget/economy
  106. return 0; //when the number of passengers are added
  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. //Validation to check booking number, travel class, and number of passengers are all valid
  123. if (booking_number < 1 || booking_number > 999 || travel_class < 1 || travel_class > 4 || number_passengers < 1 || number_passengers > 9)
  124. return -1;
  125. BookingsTable[NumberBookings][0] = booking_number; //Adds booking number to table
  126. BookingsTable[NumberBookings][1] = travel_class; //Adds travel class to table
  127. BookingsTable[NumberBookings][2] = number_passengers;//Adds number of passengers to table
  128. BookingsTable[NumberBookings][3] = 0; //Assigns 0 to seats reserved flag as a default
  129. NumberBookings++; //Increments number of bookings
  130. if (travel_class == 1)
  131. NumberFirst += number_passengers;
  132. if (travel_class == 2) //Updates all relevant
  133. NumberPremium += number_passengers; //passenger counters
  134. if (travel_class == 3)
  135. NumberEconomy += number_passengers;
  136. if (travel_class == 4)
  137. NumberBudget += number_passengers;
  138. return 1;
  139.  
  140. } // END OF THE hasCapacity FUNCTION
  141.  
  142. // This function searches for a booking in the BookingsTable
  143. // using the booking number as a search key
  144. // and returns the zero-based row index of this booking in the BookingsTable
  145. // if the booking cannot be found the function returns -1
  146. int findBooking (int booking_number)
  147. {
  148. int i;
  149. for (i = 0; i < sizeof(BookingsTable[0]); i++)
  150. if (BookingsTable[i][0] == booking_number) //Linear search
  151. break; //of bookings table
  152. if (i < sizeof(BookingsTable[0])) //to find this booking
  153. return i; //returns 0-based index of booking
  154. else
  155. return -1; //returns -1 if booking is not in table
  156. } // END OF THE findBooking FUNCTION
  157.  
  158.  
  159. // This function returns the number of passengers
  160. // who are travelling under one booking using information from the BookingsTable
  161. // or -1 if this booking cannot be found
  162. int numberPassengers (int booking_number)
  163. {
  164. int i;
  165. for (i = 0; i < sizeof(BookingsTable[0]); i++)
  166. if (BookingsTable[i][0] == booking_number) //Linear search
  167. break; //of bookings table
  168. if (i < sizeof(BookingsTable[0])) //to find this booking
  169. return BookingsTable[i][2]; //returns number of passenegers
  170. else
  171. return -1; //returns -1 if booking is not in table
  172. } // END OF THE numberPassengers FUNCTION
  173.  
  174. // This function returns 1 if seats have been reserved for this booking
  175. // and returns 0 if no seats are reserved for this booking
  176. // -1 if this booking cannot be found
  177. int seatsAreReserved (int booking_number)
  178. {
  179. int i;
  180. for (i = 0; i < sizeof(BookingsTable[0]); i++)
  181. if (BookingsTable[i][0] == booking_number) //Linear search
  182. break; //of bookings table
  183. if (i < sizeof(BookingsTable[0])) //to find this booking
  184. return BookingsTable[i][3]; //returns seat reserved flag
  185. else
  186. return -1; //returns -1 if booking is not in table
  187. } // END OF THE seatsAreReserved FUNCTION
  188.  
  189.  
  190. // This function returns the travel class of a booking
  191. // and -1 if this booking cannot be found
  192. int travelClass (int booking_number)
  193. {
  194. int i;
  195. for (i = 0; i < sizeof(BookingsTable[0]); i++) //Linear search
  196. break; //of bookings table
  197. if (i < sizeof(BookingsTable[0])) //to find this booking
  198. return BookingsTable[i][1]; //returns travel class
  199. else
  200. return -1; //returns -1 if booking is not in table
  201. } // END OF THE travelClass FUNCTION
  202.  
  203.  
  204. // always returns 1
  205. int showBookingsTable()
  206. {
  207. printf("Booking Number Travel Class Number of Passengers Seats? \n"); //Headings for table
  208. for (int a = 0; a < sizeof(BookingsTable[0]); a++)
  209. if (BookingsTable[a][0] != 0) //Will not print any rows where booking number is 0
  210. //Prints all rows of BookingsTable
  211. printf(" %i %i %i %i \n" ,BookingsTable[a][0],BookingsTable[a][1],BookingsTable[a][2],BookingsTable[a][3]);
  212.  
  213. return 1;
  214. } // END OF THE showBookingsTable FUNCTION
  215.  
  216.  
  217. // This function returns 1 if row_number is missing from the seat plan (e.g. row number 13)
  218. // otherwise it returns 0
  219. int rowIsMissing (int row_number)
  220. {
  221. if (row_number >= 4 && row_number <= 6 || row_number == 9 ||row_number >= 13 && row_number <= 19 || row_number == 33) //Checks if row number is in the seat plan
  222. return 1;
  223. return 0;
  224. } // END OF THE rowIsMissing FUNCTION
  225.  
  226.  
  227. // This function returns 1 if the seat number specified by the parameters 'row' and 'letter' is a valid seat
  228. // on the aircraft for the given travel_class,
  229. // if the seat number is not valid the function returns 0,
  230. // this function does not check whether the seat is taken (reserved for someone) or not,
  231. // The function returns -1 if travel_class invalid (not between 1-4)
  232. // since budget class passengers cannot reserve a seat, the function returns 0 if the travel class is budget (4)
  233. int isValidSeat (int travel_class, int row, char letter)
  234. {
  235. switch(travel_class)
  236. {
  237. case 1:
  238. if (row == 1 || row == 3 || row == 2) //Checks if row is valid for first class
  239. if (letter == 'A' || letter == 'B' || letter == 'E' || letter == 'F') //Checks if letter is valid for first class
  240. return 1;
  241. return 0;
  242. case 2:
  243. if (row >= 7 && row <= 21 && row != 9)
  244. if (row > 12 && row < 20) //Checks if row is valid for premium
  245. return 0;
  246. if (letter >= 'A' && letter <= 'F') //Checks if letter is valid for premium
  247. return 1;
  248. case 3:
  249. if (row >= 22 && row <= 38 && row != 33 ) //Checks if row is valid for economy
  250. if (letter >= 'A' && letter <= 'F') //Checks if letter is valid for economy
  251. return 1;
  252. return 0;
  253. break;
  254. case 4: //Returns 0 as budget class cannot reserve seats
  255. return 0;
  256. default:
  257. return -1;
  258. }
  259. } // END OF THE isValidSeat FUNCTION
  260.  
  261. // This function returns an integer between 1-6 corresponding to the letters A-F
  262. // it is useful for indexing the columns 1-6 of the SeatMap table
  263. int letterToColumnIndex (char letter)
  264. {
  265. return letter - 64; //returns ASCII value of letter minus 64 to get values between 1 and 6
  266. } // END OF THE letterToColumnIndex FUNCTION
  267.  
  268.  
  269. // This function initialises the SeatMap by initialising column 0 to the actual seat row number
  270. // and assigning -1 to all 'seats'
  271. // always returns 1
  272. int InitialiseSeatMap ()
  273. {
  274. int n, a;
  275. for (a = 0; a < 26; a++)
  276. for (n = 1; n < 7; n++)
  277. SeatMap[a][n] = -1; //Initialises every seat to -1
  278. for (a = 0; a < 3; a++)
  279. SeatMap[a][0] = a+1;
  280. SeatMap[3][0]=7;
  281. SeatMap[4][0]=8;
  282. for (a = 5; a < 8; a++)
  283. SeatMap[a][0] = a+5; //Assigns column 0 for every row to the correct
  284. for (a = 8; a < 21; a++) //seat row numbers
  285. SeatMap[a][0] = a+12;
  286. for (a = 21; a < 26; a++)
  287. SeatMap[a][0] = a+13;
  288. return 1;
  289. } // END OF THE InitialiseSeatMap FUNCTION
  290.  
  291. // This function returns the row index in the SeatMap array for a given aircraft seat row
  292. // returns -1 if this is row does not exist in the aircraft seat map
  293. // for example if row= 11 the function returns 6
  294. int rowIndex (int row)
  295. {
  296. if (row >= 1 && row <= 3)
  297. return row - 1;
  298. if (row == 7 || row == 8)
  299. return row - 4;
  300. if (row >= 10 && row <= 12) //Returns the correct row
  301. return row - 5; //index for the row inputted
  302. if (row >= 20 && row <= 32) //into the function
  303. return row - 12;
  304. if (row >= 34 && row <= 38)
  305. return row - 13;
  306. return -1;
  307. } // END OF THE rowIndex FUNCTION
  308.  
  309. // returns 1 if the seat specified by row and letter is reserved (for anyone)
  310. // and 0 if the seat is not reserved
  311. // the function returns -1 if the seat specified by row,letter is not valid
  312. int seatIsReserved (int travel_class , int row , char letter)
  313. {
  314. if (isValidSeat == 0)
  315. return -1; //Validation for validity of seat
  316. if (SeatMap[rowIndex(row)][letterToColumnIndex(letter)] == -1) //Checks if seat is -1 in the
  317. return 0; //SeatMap, and therefore reserved
  318. return 1;
  319. } // END OF THE seatIsReserved FUNCTION
  320.  
  321. // This function reserves the seat specified by row and letter to a booking_number
  322. // The function returns -1 if the seat specification is not valid or if the booking does not exist
  323. // otherwise it returns 1
  324. int reserveSeat (int booking_number, int travel_class , int row, char letter)
  325. {
  326. if (findBooking(booking_number) == -1 || isValidSeat == 0 || isValidSeat == -1) //Validation to check booking number and seat are valid
  327. return -1;
  328. BookingsTable[findBooking(booking_number)][3] = 1; //Sets the seat reserved flag in bookings table to 1
  329. SeatMap[rowIndex(row)][letterToColumnIndex(letter)] = booking_number; //Sets the seat in seat map
  330. return 1; //to the booking number inputted
  331. } // END OF THE reserveSeat FUNCTION
  332.  
  333. // Function to return the symbol
  334. // that the seat should be displayed by
  335. // in the seat map
  336. // for first class seats
  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. } // END OF THE FindSymbolFirst FUNCTION
  345.  
  346. // Function to return the symbol
  347. // that the seat should be displayed by
  348. // in the seat map
  349. // for premium seats
  350. int FindSymbolPremium(int seat, int booking_number)
  351. {
  352. if (seat == -1)
  353. return 'o';
  354. if (seatsAreReserved(booking_number) == 1)
  355. return '#';
  356. return '*';
  357. } // END OF THE FindSymbolPremium FUNCTION
  358.  
  359. // Function to return the symbol
  360. // that the seat should be displayed by
  361. // in the seat map
  362. // for economy seats
  363. int FindSymbolEconomy(int seat, int booking_number)
  364. {
  365. if (seat == -1)
  366. return '.';
  367. if (seatsAreReserved(booking_number) == 1)
  368. return '#';
  369. return '*';
  370. } // END OF THE FindSymbolEconomy FUNCTION
  371.  
  372. // This function displays the SeatMap of the aircraft
  373. // vacant first class seats are displayed as capital O
  374. // vacant premium class seats are shown as small o
  375. // vacant economy seats are shown as .
  376. // seats reserved to the given booking_number are shown as #
  377. // seats reserved for all other bookings are shown as *
  378. int ShowSeatMap (int booking_number)
  379. {
  380. int a,n, Tclass;
  381. Tclass = travelClass(booking_number);
  382. printf("--- FRONT OF AIRCRAFT --- \n");
  383. printf("First Class Cabin \n");
  384. printf(" A B E F \n");
  385. for (a = 0; a < 3; a++)
  386. {
  387. printf("%i " ,SeatMap[a][0]);
  388. 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));
  389. }
  390. printf("-------------- ------\n");
  391. printf("Main Cabin \n");
  392. printf(" A B C D E F \n");
  393. for (a = 3; a < 10; a++)
  394. {
  395. printf("%i ",SeatMap[a][0]);
  396. 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));
  397. }
  398. for (a = 10; a < 26; a++)
  399. {
  400. printf("%i ",SeatMap[a][0]);
  401. 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));
  402. }
  403. printf("\n--- REAR OF AIRCRAFT ---");
  404. } // END OF ShowSeatMap FUNCTION
  405.  
  406. // This function cancels all seats reserved for a booking
  407. // The function returns -1 if the booking does not exist
  408. // otherwise it returns 1
  409. int cancelSeatReservation (int booking_number)
  410. {
  411. if (findBooking(booking_number) == -1)
  412. return -1;
  413. BookingsTable[findBooking(booking_number)][3] = 0;
  414. for (int a = 0; a < 26; a++)
  415. for (int i = 1; i < 7; i++)
  416. if (SeatMap[a][i] == booking_number)
  417. SeatMap[a][i] = -1;
  418. return 1;
  419. } // END OF cancelSeatReservation FUNCTION
  420.  
  421. // This function cancels a booking and all seats reserved for this booking
  422. // The function returns -1 if the booking does not exist
  423. // otherwise it returns 1
  424. int cancelBooking (int booking_number)
  425. {
  426. if (findBooking(booking_number) == -1)
  427. return -1;
  428. BookingsTable[findBooking(booking_number)][3] = 0;
  429. cancelSeatReservation(booking_number);
  430. switch(travelClass(booking_number))
  431. {
  432. case 1:
  433. NumberFirst -= numberPassengers(booking_number);
  434. break;
  435. case 2:
  436. NumberPremium -= numberPassengers(booking_number);
  437. break;
  438. case 3:
  439. NumberEconomy -= numberPassengers(booking_number);
  440. break;
  441. case 4:
  442. NumberBudget -= numberPassengers(booking_number);
  443. break;
  444.  
  445. }
  446. for (int i = findBooking(booking_number); i < 12; i++)
  447. {
  448. BookingsTable[i][0] = BookingsTable[i+1][0];
  449. BookingsTable[i][1] = BookingsTable[i+1][1];
  450. BookingsTable[i][2] = BookingsTable[i+1][2];
  451. BookingsTable[i][3] = BookingsTable[i+1][3];
  452. }
  453. NumberBookings--;
  454. return 1;
  455. } // END OF cancelBooking FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement