Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4.  
  5.  
  6. //Time struct used to represent a time of day or an amount of time that has elapsed.
  7. typedef struct {
  8. int hour;
  9. int minute;
  10. } Time;
  11.  
  12.  
  13. /*Car struct which has a car's plate number,
  14. its parking permit status, time entered a lot and what lot it's parked in */
  15. typedef struct {
  16. char *plateNumber;
  17. char permit;
  18. Time enteringTime;
  19. int lotParkedIn;
  20. } Car;
  21.  
  22. /*
  23. ParkingLot struct which contains:
  24. It's unique lot number
  25. Hourly rate for parking in the lot and the maximum amount you can be charged
  26. Max capacity of cars and current amount of cars
  27. Revenue made by the lot
  28. */
  29. typedef struct {
  30. int lotNumber;
  31. double hourlyRate;
  32. double maxCharge;
  33. int capacity;
  34. int currentCarCount;
  35. double revenue;
  36. } ParkingLot;
  37.  
  38. // Sets the hours and minutes amount for the given time t based
  39. // on the specified hours h. (e.g., 1.25 hours would be 1 hour
  40. // and 15 minutes)
  41. void setHours(Time *t, double h) {
  42. t->hour =(h - (fmod(h,1.0))); // subtracts the decimal portion(minutes) of the hour, obtained via floating point modulus
  43. t->minute= fmod(h,1.0)*60.0; //multiplies the decimal portion of the hour number provided by 60(num of mins in an hour)
  44. }
  45.  
  46. // Takes two Time objects (not pointers) and computes the difference
  47. // in time from t1 to t2 and then stores that difference in the diff
  48. // Time (which must be a pointer)
  49. // Converts both given times in terms of hours then uses setHours function to set given difference time pointer
  50. void difference(Time t1, Time t2, Time *diff) {
  51. double t1Hours = t1.hour + (t1.minute*60);
  52. double t2Hours = t2.hour + (t2.minute*60);
  53. setHours(diff,fabs(t1Hours-t2Hours));
  54. }
  55.  
  56.  
  57. // Initialize the car pointed to by c to have the given plate and
  58. // hasPermit status. The car should have it’s lotParkedIn set to
  59. // 0 and enteringTime to be -1 hours and -1 minutes.
  60. void initializeCar(Car *c, char *plate, char hasPermit) {
  61. strcpy(c->plateNumber, plate);
  62. c->permit = hasPermit;
  63. c->lotParkedIn = 0;
  64. c->enteringTime.hour = -1;
  65. c->enteringTime.minute = -1;
  66. }
  67.  
  68.  
  69. // Initialize the lot pointed to by p to have the given number,
  70. // capacity, hourly rate and max charge values. The currentCarCount
  71. // and revenue should be at 0.
  72. void initializeLot(ParkingLot *p, int num, int cap, double rate, double max) {
  73. p->lotNumber = num;
  74. p->capacity = cap;
  75. p->hourlyRate = rate;
  76. p->maxCharge = max;
  77. p->currentCarCount = 0;
  78. p->revenue = 0.0;
  79. }
  80.  
  81. // Print out the parking lot parameters so that is displays as
  82. // follows: Parking Lot #2 - rate = $3.00, capacity 6, current cars 5
  83. void printLotInfo(ParkingLot p) {
  84. printf("Parking Lot #%d - rate = $%-3.2f, capacity %d, current cars %d",p.lotNumber,p.hourlyRate,p.capacity,p.currentCarCount);
  85. }
  86.  
  87.  
  88. // Simulate a car entering the parking lot
  89. void carEnters(ParkingLot *lot,Car *car,int hour, int min){
  90.  
  91. }
  92.  
  93. // Simulate a car leaving the parking lot
  94. void carLeaves(ParkingLot *lot,Car *car,int hour, int min){
  95.  
  96. }
  97.  
  98. int main() {
  99. Car car1, car2, car3, car4, car5, car6, car7, car8, car9;
  100. ParkingLot p1, p2;
  101.  
  102. // Set up 9 cars
  103. initializeCar(&car1, "ABC 123", 0);
  104. initializeCar(&car2, "ABC 124", 0);
  105. initializeCar(&car3, "ABD 314", 0);
  106. initializeCar(&car4, "ADE 901", 0);
  107. initializeCar(&car5, "AFR 304", 0);
  108. initializeCar(&car6, "AGD 888", 0);
  109. initializeCar(&car7, "AAA 111", 0);
  110. initializeCar(&car8, "ABB 001", 0);
  111. initializeCar(&car9, "XYZ 678", 1);
  112.  
  113. // Set up two parking lots
  114. initializeLot(&p1, 1, 4, 5.5, 20.0);
  115. initializeLot(&p2, 2, 6, 3.0, 12.0);
  116.  
  117. printLotInfo(p1);
  118. printLotInfo(p2);
  119. printf("\n");
  120.  
  121. // Simulate cars entering the lots
  122. carEnters(&p1, &car1, 7, 15);
  123. carEnters(&p1, &car2, 7, 25);
  124. carEnters(&p2, &car3, 8, 0);
  125. carEnters(&p2, &car4, 8, 10);
  126. carEnters(&p1, &car5, 8, 15);
  127. carEnters(&p1, &car6, 8, 20);
  128. carEnters(&p1, &car7, 8, 30);
  129. carEnters(&p2, &car7, 8, 32);
  130. carEnters(&p2, &car8, 8, 50);
  131. carEnters(&p2, &car9, 8, 55);
  132.  
  133. printf("\n");
  134. printLotInfo(p1);
  135. printLotInfo(p2);
  136. printf("\n");
  137.  
  138. // Simulate cars leaving the lots
  139. carLeaves(&p2, &car4, 9, 0);
  140. carLeaves(&p1, &car2, 9, 5);
  141. carLeaves(&p1, &car6, 10, 0);
  142. carLeaves(&p1, &car1, 10, 30);
  143. carLeaves(&p2, &car8, 13, 0);
  144. carLeaves(&p2, &car9, 15, 15);
  145. carEnters(&p1, &car8, 17, 10);
  146. carLeaves(&p1, &car5, 17, 50);
  147. carLeaves(&p2, &car7, 18, 0);
  148. carLeaves(&p2, &car3, 18, 15);
  149. carLeaves(&p1, &car8, 20, 55);
  150.  
  151. printf("\n");
  152. printLotInfo(p1);
  153. printLotInfo(p2);
  154. printf("\n");
  155.  
  156. // Display the total revenue
  157. printf("Total revenue of Lot 1 is $%4.2f\n", p1.revenue);
  158. printf("Total revenue of Lot 2 is $%4.2f\n", p2.revenue);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement