Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- //Time struct used to represent a time of day or an amount of time that has elapsed.
- typedef struct {
- int hour;
- int minute;
- } Time;
- /*Car struct which has a car's plate number,
- its parking permit status, time entered a lot and what lot it's parked in */
- typedef struct {
- char *plateNumber;
- char permit;
- Time enteringTime;
- int lotParkedIn;
- } Car;
- /*
- ParkingLot struct which contains:
- It's unique lot number
- Hourly rate for parking in the lot and the maximum amount you can be charged
- Max capacity of cars and current amount of cars
- Revenue made by the lot
- */
- typedef struct {
- int lotNumber;
- double hourlyRate;
- double maxCharge;
- int capacity;
- int currentCarCount;
- double revenue;
- } ParkingLot;
- // Sets the hours and minutes amount for the given time t based
- // on the specified hours h. (e.g., 1.25 hours would be 1 hour
- // and 15 minutes)
- void setHours(Time *t, double h) {
- t->hour =(h - (fmod(h,1.0))); // subtracts the decimal portion(minutes) of the hour, obtained via floating point modulus
- 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)
- }
- // Takes two Time objects (not pointers) and computes the difference
- // in time from t1 to t2 and then stores that difference in the diff
- // Time (which must be a pointer)
- // Converts both given times in terms of hours then uses setHours function to set given difference time pointer
- void difference(Time t1, Time t2, Time *diff) {
- double t1Hours = t1.hour + (t1.minute*60);
- double t2Hours = t2.hour + (t2.minute*60);
- setHours(diff,fabs(t1Hours-t2Hours));
- }
- // Initialize the car pointed to by c to have the given plate and
- // hasPermit status. The car should have it’s lotParkedIn set to
- // 0 and enteringTime to be -1 hours and -1 minutes.
- void initializeCar(Car *c, char *plate, char hasPermit) {
- strcpy(c->plateNumber, plate);
- c->permit = hasPermit;
- c->lotParkedIn = 0;
- c->enteringTime.hour = -1;
- c->enteringTime.minute = -1;
- }
- // Initialize the lot pointed to by p to have the given number,
- // capacity, hourly rate and max charge values. The currentCarCount
- // and revenue should be at 0.
- void initializeLot(ParkingLot *p, int num, int cap, double rate, double max) {
- p->lotNumber = num;
- p->capacity = cap;
- p->hourlyRate = rate;
- p->maxCharge = max;
- p->currentCarCount = 0;
- p->revenue = 0.0;
- }
- // Print out the parking lot parameters so that is displays as
- // follows: Parking Lot #2 - rate = $3.00, capacity 6, current cars 5
- void printLotInfo(ParkingLot p) {
- printf("Parking Lot #%d - rate = $%-3.2f, capacity %d, current cars %d",p.lotNumber,p.hourlyRate,p.capacity,p.currentCarCount);
- }
- // Simulate a car entering the parking lot
- void carEnters(ParkingLot *lot,Car *car,int hour, int min){
- }
- // Simulate a car leaving the parking lot
- void carLeaves(ParkingLot *lot,Car *car,int hour, int min){
- }
- int main() {
- Car car1, car2, car3, car4, car5, car6, car7, car8, car9;
- ParkingLot p1, p2;
- // Set up 9 cars
- initializeCar(&car1, "ABC 123", 0);
- initializeCar(&car2, "ABC 124", 0);
- initializeCar(&car3, "ABD 314", 0);
- initializeCar(&car4, "ADE 901", 0);
- initializeCar(&car5, "AFR 304", 0);
- initializeCar(&car6, "AGD 888", 0);
- initializeCar(&car7, "AAA 111", 0);
- initializeCar(&car8, "ABB 001", 0);
- initializeCar(&car9, "XYZ 678", 1);
- // Set up two parking lots
- initializeLot(&p1, 1, 4, 5.5, 20.0);
- initializeLot(&p2, 2, 6, 3.0, 12.0);
- printLotInfo(p1);
- printLotInfo(p2);
- printf("\n");
- // Simulate cars entering the lots
- carEnters(&p1, &car1, 7, 15);
- carEnters(&p1, &car2, 7, 25);
- carEnters(&p2, &car3, 8, 0);
- carEnters(&p2, &car4, 8, 10);
- carEnters(&p1, &car5, 8, 15);
- carEnters(&p1, &car6, 8, 20);
- carEnters(&p1, &car7, 8, 30);
- carEnters(&p2, &car7, 8, 32);
- carEnters(&p2, &car8, 8, 50);
- carEnters(&p2, &car9, 8, 55);
- printf("\n");
- printLotInfo(p1);
- printLotInfo(p2);
- printf("\n");
- // Simulate cars leaving the lots
- carLeaves(&p2, &car4, 9, 0);
- carLeaves(&p1, &car2, 9, 5);
- carLeaves(&p1, &car6, 10, 0);
- carLeaves(&p1, &car1, 10, 30);
- carLeaves(&p2, &car8, 13, 0);
- carLeaves(&p2, &car9, 15, 15);
- carEnters(&p1, &car8, 17, 10);
- carLeaves(&p1, &car5, 17, 50);
- carLeaves(&p2, &car7, 18, 0);
- carLeaves(&p2, &car3, 18, 15);
- carLeaves(&p1, &car8, 20, 55);
- printf("\n");
- printLotInfo(p1);
- printLotInfo(p2);
- printf("\n");
- // Display the total revenue
- printf("Total revenue of Lot 1 is $%4.2f\n", p1.revenue);
- printf("Total revenue of Lot 2 is $%4.2f\n", p2.revenue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement