帮忙用C语言写一个代码
时间: 2023-12-29 18:19:28 浏览: 71
好的,我可以给你提供一个简单的停车场管理系统的C语言代码,你可以根据需要进行修改和扩展。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define CAR_NUM 10 // 停车场车位数
#define WAIT_NUM 5 // 等待队列长度
// 车辆结构体
typedef struct car {
char plate_num[10]; // 车牌号
time_t arrival_time; // 到达时间
int park_time; // 停留时间
} Car;
// 停车场结构体
typedef struct parking_lot {
Car *cars[CAR_NUM]; // 车辆指针数组
int count; // 当前停车量
} ParkingLot;
// 等待队列结构体
typedef struct wait_queue {
Car *cars[WAIT_NUM]; // 车辆指针数组
int count; // 当前等待量
int head; // 队头
int tail; // 队尾
} WaitQueue;
// 初始化停车场
void init_parking_lot(ParkingLot *parking_lot) {
parking_lot->count = 0;
for (int i = 0; i < CAR_NUM; i++) {
parking_lot->cars[i] = NULL;
}
}
// 初始化等待队列
void init_wait_queue(WaitQueue *wait_queue) {
wait_queue->count = 0;
wait_queue->head = 0;
wait_queue->tail = 0;
for (int i = 0; i < WAIT_NUM; i++) {
wait_queue->cars[i] = NULL;
}
}
// 停车
void park_car(ParkingLot *parking_lot, WaitQueue *wait_queue, Car *car) {
if (parking_lot->count < CAR_NUM) { // 停车场未满
// 找到空位停车
for (int i = 0; i < CAR_NUM; i++) {
if (parking_lot->cars[i] == NULL) {
parking_lot->cars[i] = car;
parking_lot->count++;
printf("车牌号为 %s 的车辆已停入停车场,停车位为 %d\n", car->plate_num, i);
return;
}
}
} else { // 停车场已满
if (wait_queue->count < WAIT_NUM) { // 等待队列未满
wait_queue->cars[wait_queue->tail] = car;
wait_queue->count++;
wait_queue->tail = (wait_queue->tail + 1) % WAIT_NUM;
printf("车牌号为 %s 的车辆已停入等待队列,等待序号为 %d\n", car->plate_num, wait_queue->count);
return;
} else { // 等待队列已满
printf("车牌号为 %s 的车辆无法停入停车场,也无法进入等待队列\n", car->plate_num);
return;
}
}
}
// 取车
void take_car(ParkingLot *parking_lot, WaitQueue *wait_queue, char *plate_num) {
// 在停车场中查找车辆
for (int i = 0; i < CAR_NUM; i++) {
if (parking_lot->cars[i] != NULL && strcmp(parking_lot->cars[i]->plate_num, plate_num) == 0) {
// 计算停车费用
time_t now = time(NULL);
int park_time = (int)(now - parking_lot->cars[i]->arrival_time);
int fee = park_time / 60 * 5;
printf("车牌号为 %s 的车辆已从停车场取出,停留时间为 %d 分钟,停车费用为 %d 元\n", plate_num, park_time, fee);
// 从停车场中移除该车辆
free(parking_lot->cars[i]);
parking_lot->cars[i] = NULL;
parking_lot->count--;
// 从等待队列中取出一辆车进入停车场
if (wait_queue->count > 0) {
int head = wait_queue->head;
park_car(parking_lot, wait_queue, wait_queue->cars[head]);
wait_queue->cars[head] = NULL;
wait_queue->count--;
wait_queue->head = (head + 1) % WAIT_NUM;
}
return;
}
}
// 在等待队列中查找车辆
for (int i = 0; i < wait_queue->count; i++) {
int index = (wait_queue->head + i) % WAIT_NUM;
if (wait_queue->cars[index] != NULL && strcmp(wait_queue->cars[index]->plate_num, plate_num) == 0) {
printf("车牌号为 %s 的车辆已从等待队列中移除\n", plate_num);
free(wait_queue->cars[index]);
wait_queue->cars[index] = NULL;
wait_queue->count--;
return;
}
}
// 没有找到该车辆
printf("未找到车牌号为 %s 的车辆\n", plate_num);
}
// 查询停车场状态
void query_parking_lot(ParkingLot *parking_lot) {
printf("当前停车场状态:已停车辆数:%d,空闲车位数:%d\n", parking_lot->count, CAR_NUM - parking_lot->count);
}
// 查询等待队列状态
void query_wait_queue(WaitQueue *wait_queue) {
printf("当前等待队列状态:等待车辆数:%d\n", wait_queue->count);
}
// 测试程序
int main() {
ParkingLot parking_lot;
WaitQueue wait_queue;
init_parking_lot(&parking_lot);
init_wait_queue(&wait_queue);
// 停车
Car car1 = {"京A12345", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car1);
Car car2 = {"京B67890", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car2);
Car car3 = {"京C24680", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car3);
Car car4 = {"京D13579", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car4);
Car car5 = {"京E97531", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car5);
Car car6 = {"京F86420", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car6);
Car car7 = {"京G75319", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car7);
// 查询停车场状态和等待队列状态
query_parking_lot(&parking_lot);
query_wait_queue(&wait_queue);
// 取车
take_car(&parking_lot, &wait_queue, "京D13579");
// 查询停车场状态和等待队列状态
query_parking_lot(&parking_lot);
query_wait_queue(&wait_queue);
// 停车
Car car8 = {"京H24680", time(NULL), 0};
park_car(&parking_lot, &wait_queue, &car8);
// 查询停车场状态和等待队列状态
query_parking_lot(&parking_lot);
query_wait_queue(&wait_queue);
// 取车
take_car(&parking_lot, &wait_queue, "京C24680");
// 查询停车场状态和等待队列状态
query_parking_lot(&parking_lot);
query_wait_queue(&wait_queue);
return 0;
}
```
这个代码实现了停车、取车、查询停车场状态和等待队列状态等基本功能。你可以根据需要进行修改和扩展。
阅读全文