Implementing a Parking Management System in C

Hello everyone, this is Xiao Zhang. Today, I am bringing you this article which mainly introduces how to implement a parking management system using C. The article provides detailed example code, which is of certain reference value for your learning or work. Friends in need can refer to it!

Below is a simple example code for a C language parking management system, which implements basic functions such as vehicle entry and exit management and displaying parking lot information:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the maximum capacity of the parking lot
#define MAX_CAPACITY 10

// Vehicle information structure
typedef struct {
    char licensePlate[20];  // License plate number
    int arrivalTime;        // Arrival time (assumed to be represented by an integer, e.g., minutes from a starting time)
} Car;

// Parking lot structure
typedef struct {
    Car cars[MAX_CAPACITY];
    int count;  // Current number of vehicles in the parking lot
} ParkingLot;

// Initialize the parking lot
void initParkingLot(ParkingLot* lot) {
    lot->count = 0;
}

// Vehicle enters the parking lot
void enterParkingLot(ParkingLot* lot, char* licensePlate, int arrivalTime) {
    if (lot->count < MAX_CAPACITY) {
        strcpy(lot->cars[lot->count].licensePlate, licensePlate);
        lot->cars[lot->count].arrivalTime = arrivalTime;
        lot->count++;
        printf("Vehicle %s successfully entered the parking lot, arrival time: %d\n", licensePlate, arrivalTime);
    } else {
        printf("The parking lot is full, vehicle %s cannot enter\n", licensePlate);
    }
}

// Vehicle leaves the parking lot
void leaveParkingLot(ParkingLot* lot, char* licensePlate, int departureTime) {
    int i;
    for (i = 0; i < lot->count; i++) {
        if (strcmp(lot->cars[i].licensePlate, licensePlate) == 0) {
            int parkingTime = departureTime - lot->cars[i].arrivalTime;
            printf("Vehicle %s leaves the parking lot, parking time: %d minutes\n", licensePlate, parkingTime);
            // Move the remaining vehicle information forward
            for (; i < lot->count - 1; i++) {
                lot->cars[i] = lot->cars[i + 1];
            }
            lot->count--;
            return;
        }
    }
    printf("Vehicle with license plate %s not found\n", licensePlate);
}

// Display vehicle information in the parking lot
void displayParkingLot(ParkingLot* lot) {
    if (lot->count == 0) {
        printf("The parking lot is empty\n");
    } else {
        printf("Vehicle information in the parking lot:\n");
        for (int i = 0; i < lot->count; i++) {
            printf("License plate: %s, arrival time: %d\n", lot->cars[i].licensePlate, lot->cars[i].arrivalTime);
        }
    }
}

int main() {
    ParkingLot parkingLot;
    initParkingLot(&parkingLot);

    enterParkingLot(&parkingLot, "京A12345", 10);
    enterParkingLot(&parkingLot, "京B67890", 20);
    displayParkingLot(&parkingLot);

    leaveParkingLot(&parkingLot, "京A12345", 40);
    displayParkingLot(&parkingLot);

    return 0;
}

This code implements a simple parking management system with the following features:

  • • Defined vehicle information structure Car and parking lot structure ParkingLot to store vehicle license plate numbers, arrival times, and information about the number of vehicles in the parking lot.

  • • The initParkingLot function initializes the parking lot by setting the vehicle count to 0.

  • • The enterParkingLot function implements the function of vehicles entering the parking lot. If the parking lot is not full, it stores vehicle information in the parking lot array and increases the vehicle count; if the parking lot is full, it prompts that the vehicle cannot enter.

  • • The leaveParkingLot function implements the function of vehicles leaving the parking lot. It finds the corresponding vehicle by license plate number, calculates the parking time, and outputs it. Then, it moves the remaining vehicle information forward and decreases the vehicle count. If the vehicle is not found, it prompts that it was not found.

  • • The displayParkingLot function displays the vehicle information in the parking lot. If the parking lot is empty, it prompts that the parking lot is empty.

In the main function, these functions are tested simply, simulating vehicle entry and exit operations and displaying parking lot information.

Please note that this is just a simple example. An actual parking management system may require more complex functions such as space reservation, fee calculation, vehicle search, etc. It may also need to interact with databases or other external systems, and require more complete error handling and user interfaces. You can further expand and improve this code based on actual needs.

Note: For reference only

Leave a Comment