停车场管理系统数据结构C语言
时间: 2023-08-31 15:09:25 浏览: 175
停车场管理系统数据结构可以用C语言实现,以下是一个简单的示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CARS 50
struct Car {
char license_plate[10];
int entry_time;
int exit_time;
};
struct ParkingLot {
struct Car cars[MAX_CARS];
int num_cars;
};
void add_car(struct ParkingLot *parking_lot, char *license_plate, int entry_time) {
if (parking_lot->num_cars >= MAX_CARS) {
printf("Parking lot is full.\n");
return;
}
struct Car car;
strcpy(car.license_plate, license_plate);
car.entry_time = entry_time;
car.exit_time = -1;
parking_lot->cars[parking_lot->num_cars] = car;
parking_lot->num_cars++;
printf("Car with license plate %s added to the parking lot.\n", license_plate);
}
void remove_car(struct ParkingLot *parking_lot, char *license_plate, int exit_time) {
int i;
for (i = 0; i < parking_lot->num_cars; i++) {
if (strcmp(parking_lot->cars[i].license_plate, license_plate) == 0) {
if (parking_lot->cars[i].exit_time != -1) {
printf("Car with license plate %s has already left the parking lot.\n", license_plate);
} else {
parking_lot->cars[i].exit_time = exit_time;
printf("Car with license plate %s has left the parking lot.\n", license_plate);
}
return;
}
}
printf("Car with license plate %s not found in the parking lot.\n", license_plate);
}
void print_parking_lot(struct ParkingLot *parking_lot) {
int i;
for (i = 0; i < parking_lot->num_cars; i++) {
printf("Car with license plate %s entered at %d", parking_lot->cars[i].license_plate, parking_lot->cars[i].entry_time);
if (parking_lot->cars[i].exit_time != -1) {
printf(" and exited at %d", parking_lot->cars[i].exit_time);
}
printf("\n");
}
}
int main() {
struct ParkingLot parking_lot;
parking_lot.num_cars = 0;
add_car(&parking_lot, "ABC123", 1000);
add_car(&parking_lot, "DEF456", 1030);
add_car(&parking_lot, "GHI789", 1100);
remove_car(&parking_lot, "DEF456", 1200);
print_parking_lot(&parking_lot);
return 0;
}
```
这个示例中,我们使用了两个结构体:`Car` 和 `ParkingLot`。`Car` 结构体包含车牌号、进入时间和离开时间。`ParkingLot` 结构体包含一个 `Car` 数组和当前停车场中汽车的数量。
我们定义了三个函数:`add_car`、`remove_car` 和 `print_parking_lot`。`add_car` 函数用于添加车辆到停车场,`remove_car` 函数用于移除停车场中的车辆,`print_parking_lot` 函数用于打印当前停车场中的所有车辆信息。
在 `main` 函数中,我们创建了一个 `ParkingLot` 结构体,并使用 `add_car` 函数添加了三辆车。然后,我们使用 `remove_car` 函数将一辆车移除,并使用 `print_parking_lot` 函数打印当前停车场中所有车辆的信息。
这个示例只是一个简单的停车场管理系统的数据结构,实际应用中可能需要更复杂的功能和更复杂的数据结构。
阅读全文