根据此内容用C语言写代码
时间: 2024-03-26 21:38:39 浏览: 21
好的,下面是基本的代码框架,您可以根据具体需求进行修改:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 定义车辆结构体
typedef struct Car {
char license[10]; // 车牌号
time_t enter_time; // 入场时间
time_t exit_time; // 出场时间
float cost; // 费用
} Car;
// 定义停车场结构体
typedef struct ParkingLot {
int capacity; // 停车位总数
int available; // 剩余停车位数
int occupied; // 已占用停车位数
int small_car_num; // 小型车库数量
Car **small_car_lot; // 小型车库
Car *car_queue[10000]; // 车辆排队队列
int queue_front; // 队首指针
int queue_rear; // 队尾指针
} ParkingLot;
// 初始化停车场
void init_parking_lot(ParkingLot *p, int capacity, int small_car_num);
// 车辆入场
void enter_parking_lot(ParkingLot *p, Car *car);
// 车辆出场
void exit_parking_lot(ParkingLot *p, Car *car);
// 计算停车费用
float calc_cost(Car *car);
// 打印车辆信息
void print_car_info(Car *car);
// 打印停车场信息
void print_parking_lot_info(ParkingLot *p);
int main() {
// 初始化停车场
ParkingLot p;
init_parking_lot(&p, 100, 20);
// 模拟车辆入场
Car car1 = {"京A12345", time(NULL), 0, 0};
enter_parking_lot(&p, &car1);
print_parking_lot_info(&p);
// 模拟车辆出场
exit_parking_lot(&p, &car1);
print_parking_lot_info(&p);
return 0;
}
void init_parking_lot(ParkingLot *p, int capacity, int small_car_num) {
// 初始化停车场
p->capacity = capacity;
p->available = capacity;
p->occupied = 0;
p->small_car_num = small_car_num;
p->small_car_lot = (Car**)malloc(sizeof(Car*) * small_car_num);
for (int i = 0; i < small_car_num; i++) {
p->small_car_lot[i] = NULL;
}
p->queue_front = 0;
p->queue_rear = 0;
}
void enter_parking_lot(ParkingLot *p, Car *car) {
// 车辆入场
if (p->available > 0) {
// 停车场有空位,直接入场
time_t enter_time = time(NULL);
car->enter_time = enter_time;
p->car_queue[p->queue_rear++] = car;
p->occupied++;
p->available--;
} else {
// 停车场已满,车辆排队等待
p->car_queue[p->queue_rear++] = car;
}
}
void exit_parking_lot(ParkingLot *p, Car *car) {
// 车辆出场
if (car->exit_time == 0) {
// 记录出场时间
time_t exit_time = time(NULL);
car->exit_time = exit_time;
// 计算费用
car->cost = calc_cost(car);
// 将车辆从停车场中移除
if (car->license[7] == '0' && car->license[8] == '1') {
// 如果是小型车,从小型车库中移除
for (int i = 0; i < p->small_car_num; i++) {
if (p->small_car_lot[i] == car) {
p->small_car_lot[i] = NULL;
break;
}
}
} else {
// 如果是普通车,从停车场中移除
p->occupied--;
p->available++;
}
// 从排队队列中取出下一辆车
if (p->queue_front < p->queue_rear) {
Car *next_car = p->car_queue[p->queue_front];
time_t enter_time = next_car->enter_time;
next_car->enter_time = 0;
next_car->exit_time = 0;
next_car->cost = 0;
p->queue_front++;
enter_parking_lot(p, next_car);
}
}
}
float calc_cost(Car *car) {
// 计算停车费用
float cost = 0;
int minutes = (int)(car->exit_time - car->enter_time) / 60;
if (minutes <= 15) {
// 15分钟以下免费
cost = 0;
} else {
// 超过15分钟按0.5元/15分钟计费
cost = (minutes / 15) * 0.5;
}
if (car->exit_time % 86400 > 79200 || car->exit_time % 86400 < 21600) {
// 夜间停车1元/小时
cost += ((car->exit_time - car->enter_time) / 3600) * 1;
}
return cost;
}
void print_car_info(Car *car) {
// 打印车辆信息
printf("车牌号:%s\n", car->license);
printf("入场时间:%s", ctime(&car->enter_time));
printf("出场时间:%s", ctime(&car->exit_time));
printf("停车费用:%f元\n", car->cost);
}
void print_parking_lot_info(ParkingLot *p) {
// 打印停车场信息
printf("停车场总共有%d个车位,剩余%d个车位,已占用%d个车位,小型车库数量为%d\n", p->capacity, p->available, p->occupied, p->small_car_num);
printf("排队等待的车辆数量为%d\n", p->queue_rear - p->queue_front);
printf("停车场中的车辆信息如下:\n");
for (int i = 0; i < p->small_car_num; i++) {
if (p->small_car_lot[i] != NULL) {
printf("小型车库%d中的车辆信息如下:\n", i+1);
print_car_info(p->small_car_lot[i]);
}
}
for (int i = 0; i < p->queue_rear - p->queue_front; i++) {
printf("排队等待的第%d辆车的车牌号为:%s\n", i+1, p->car_queue[p->queue_front+i]->license);
}
}
```
这是一个基本的停车场管理系统,可以进行车辆入场、出场、计费等操作。您可以根据实际需求进行修改和完善。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)