【C语言】停车场管理系统丨源码+解析
本文完整实现了基于栈+队列的停车场管理系统,包含详细源码和逐行解析,特别适合复习数据结构与算法综合应用
一、系统设计思路
1. 数据结构设计
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_SIZE 5 // 停车场容量(可修改)
#define PRICE_PER_MIN 0.2 // 每分钟费用
// 车辆信息结构体
typedef struct {
char license[10]; // 车牌号
time_t enter_time; // 进入时间戳
time_t exit_time; // 离开时间戳
} CarInfo;
// 栈结构(停车场)
typedef struct {
CarInfo data[MAX_SIZE];
int top; // 栈顶指针
} ParkingStack;
// 队列节点(便道)
typedef struct QueueNode {
CarInfo car;
struct QueueNode* next;
} QueueNode;
// 队列结构(便道)
typedef struct {
QueueNode* front; // 队首
QueueNode* rear; // 队尾
int count; // 车辆计数
} WaitingQueue;
2. 核心算法
- 栈管理停车场:后进先出(LIFO)模拟车辆停放顺序
- 队列管理便道:先进先出(FIFO)保证公平等待
- 让车处理:目标车离开时,后方车辆先退栈暂存,再按原序压回
3. 功能模块
- 车辆到达处理
- 车辆离开计费
- 实时显示停车场/便道状态
- 时间自动记录与计算
二、完整源码(约250行)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_SIZE 5 // 停车场容量
#define PRICE_PER_MIN 0.2 // 每分钟费用
// 车辆信息
typedef struct {
char license[10]; // 车牌号
time_t enter_time; // 进入时间戳
time_t exit_time; // 离开时间戳
} CarInfo;
// 栈结构(停车场)
typedef struct {
CarInfo data[MAX_SIZE];
int top;
} ParkingStack;
// 队列节点(便道)
typedef struct QueueNode {
CarInfo car;
struct QueueNode* next;
} QueueNode;
// 队列结构(便道)
typedef struct {
QueueNode* front;
QueueNode* rear;
int count;
} WaitingQueue;
// 初始化停车场栈
void initParking(ParkingStack* ps) {
ps->top = -1;
}
// 初始化等待队列
void initQueue(WaitingQueue* wq) {
wq->front = wq->rear = NULL;
wq->count = 0;
}
// 车辆进入便道
void enqueue(WaitingQueue* wq, CarInfo car) {
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
newNode->car = car;
newNode->next = NULL;
if (wq->rear == NULL) {
wq->front = wq->rear = newNode;
} else {
wq->rear->next = newNode;
wq->rear = newNode;
}
wq->count++;
printf("车辆 %s 进入便道等待,当前位置:%d\n", car.license, wq->count);
}
// 便道车辆进入停车场
CarInfo dequeue(WaitingQueue* wq) {
if (wq->front == NULL) {
CarInfo empty = {"", 0, 0};
return empty;
}
QueueNode* temp = wq->front;
CarInfo car = temp->car;
wq->front = wq->front->next;
if (wq->front == NULL) {
wq->rear = NULL;
}
free(temp);
wq->count--;
return car;
}
// 车辆进入停车场
void push(ParkingStack* ps, CarInfo car) {
if (ps->top == MAX_SIZE - 1) {
printf("停车场已满!\n");
return;
}
ps->data[++ps->top] = car;
printf("车辆 %s 进入停车场,车位:%d\n", car.license, ps->top + 1);
}
// 车辆离开停车场
CarInfo pop(ParkingStack* ps) {
if (ps->top == -1) {
CarInfo empty = {"", 0, 0};
return empty;
}
return ps->data[ps->top--];
}
// 显示当前状态
void display(ParkingStack* ps, WaitingQueue* wq) {
printf("\n===== 停车场状态 =====\n");
if (ps->top == -1) {
printf("停车场为空\n");
} else {
for (int i = 0; i <= ps->top; i++) {
printf("车位 %d: %s (进入时间: %s",
i + 1,
ps->data[i].license,
ctime(&ps->data[i].enter_time));
}
}
printf("\n===== 便道状态 =====\n");
if (wq->count == 0) {
printf("便道为空\n");
} else {
QueueNode* current = wq->front;
int pos = 1;
while (current != NULL) {
printf("等待 %d: %s\n", pos++, current->car.license);
current = current->next;
}
}
printf("\n");
}
// 处理车辆离开
void handleExit(ParkingStack* ps, WaitingQueue* wq, char* license) {
ParkingStack temp; // 临时栈存放让路车辆
initParking(&temp);
int found = 0;
CarInfo exitCar;
// 在停车场中查找目标车辆
while (ps->top != -1) {
CarInfo car = pop(ps);
if (strcmp(car.license, license) == 0) {
found = 1;
exitCar = car;
break;
}
push(&temp, car); // 让路车辆压入临时栈
}
// 未找到车辆
if (!found) {
printf("未找到车牌为 %s 的车辆\n", license);
// 还原停车场
while (temp.top != -1) {
CarInfo car = pop(&temp);
push(ps, car);
}
return;
}
// 计算费用
exitCar.exit_time = time(NULL);
double duration = difftime(exitCar.exit_time, exitCar.enter_time) / 60; // 分钟
double fee = duration * PRICE_PER_MIN;
printf("车辆 %s 离开,停留时间: %.1f分钟,费用: ¥%.2f\n",
license, duration, fee);
// 将临时栈中的车辆移回停车场
while (temp.top != -1) {
CarInfo car = pop(&temp);
push(ps, car);
}
// 检查便道并放行一辆车
if (wq->count > 0) {
CarInfo newCar = dequeue(wq);
newCar.enter_time = time(NULL);
push(ps, newCar);
}
}
int main() {
ParkingStack parking;
WaitingQueue queue;
initParking(&parking);
initQueue(&queue);
int choice;
char license[10];
while (1) {
printf("\n===== 停车场管理系统 =====\n");
printf("1. 车辆到达\n");
printf("2. 车辆离开\n");
printf("3. 显示状态\n");
printf("4. 退出系统\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1: // 车辆到达
printf("请输入车牌号: ");
scanf("%s", license);
CarInfo newCar;
strcpy(newCar.license, license);
newCar.enter_time = time(NULL);
if (parking.top < MAX_SIZE - 1) {
push(&parking, newCar);
} else {
enqueue(&queue, newCar);
}
break;
case 2: // 车辆离开
printf("请输入离开车牌号: ");
scanf("%s", license);