C语言停车场管理系统原理及示例代码
C语言停车场管理系统原理及示例代码
停车场管理系统可以利用C语言实现,以下介绍其基本原理和示例代码。
1. 定义数据结构
首先需要定义两个结构体:车辆结构体和停车场结构体。
车辆结构体
struct Car {
char plateNum[10];
int type;
time_t enterTime;
time_t exitTime;
float fee;
};
其中:
plateNum:车牌号码,字符串类型type:车辆类型,整型,例如1表示小型车,2表示大型车enterTime:入场时间,时间戳类型exitTime:出场时间,时间戳类型fee:停车费用,浮点型
停车场结构体
struct ParkingLot {
char name[20];
int totalSpaces;
int remainingSpaces;
float feeRate;
struct Car records[100];
int numCars;
};
其中:
name:停车场名称,字符串类型totalSpaces:总车位数,整型remainingSpaces:剩余车位数,整型feeRate:收费标准,浮点型,单位为元/分钟records:停车场记录,数组类型,每个元素为一个Car结构体,用于存储车辆信息numCars:当前停车场内的车辆数量,整型
2. 初始化停车场
在程序开始时,需要初始化停车场,设置总车位数、收费标准等信息。
void initParkingLot(struct ParkingLot* pl, char* name, int totalSpaces, float feeRate) {
strcpy(pl->name, name);
pl->totalSpaces = totalSpaces;
pl->remainingSpaces = totalSpaces;
pl->feeRate = feeRate;
pl->numCars = 0;
}
3. 进入停车场
当车辆进入停车场时,需要输入车辆信息,判断是否有空余车位,如果有,则将车辆信息存入停车场记录中,并更新剩余车位数。
void enterParkingLot(struct ParkingLot* pl) {
//判断是否有空余车位
if (pl->remainingSpaces > 0) {
struct Car car;
printf('请输入车牌号码:');
scanf('%s', car.plateNum);
printf('请输入车辆类型(1-小型车,2-大型车):');
scanf('%d', &car.type);
car.enterTime = time(NULL);
printf('停车成功!\n');
printf('车牌号码:%s\n', car.plateNum);
printf('车辆类型:%s\n', car.type == 1 ? '小型车' : '大型车');
printf('入场时间:%s\n', ctime(&car.enterTime));
pl->records[pl->numCars] = car;
pl->numCars++;
pl->remainingSpaces--;
} else {
printf('停车失败,车位已满!\n');
}
}
4. 出停车场
当车辆离开停车场时,需要输入车牌号码,查找停车场记录,计算停车费用,更新停车场记录,并更新剩余车位数。
void exitParkingLot(struct ParkingLot* pl) {
char plateNum[10];
printf('请输入车牌号码:');
scanf('%s', plateNum);
int found = 0;
for (int i = 0; i < pl->numCars; i++) {
if (strcmp(pl->records[i].plateNum, plateNum) == 0) {
found = 1;
pl->records[i].exitTime = time(NULL);
int duration = (int)(pl->records[i].exitTime - pl->records[i].enterTime) / 60;
pl->records[i].fee = duration * pl->feeRate;
printf('离开停车场!\n');
printf('车牌号码:%s\n', pl->records[i].plateNum);
printf('车辆类型:%s\n', pl->records[i].type == 1 ? '小型车' : '大型车');
printf('入场时间:%s', ctime(&pl->records[i].enterTime));
printf('出场时间:%s', ctime(&pl->records[i].exitTime));
printf('停车费用:%.2f元\n', pl->records[i].fee);
for (int j = i; j < pl->numCars - 1; j++) {
pl->records[j] = pl->records[j + 1];
}
pl->numCars--;
pl->remainingSpaces++;
break;
}
}
if (!found) {
printf('未找到该车辆的停车记录!\n');
}
}
5. 查询停车场信息
可以查询停车场名称、总车位数、剩余车位数、收费标准等信息。
void queryParkingLot(struct ParkingLot* pl) {
printf('停车场名称:%s\n', pl->name);
printf('总车位数:%d\n', pl->totalSpaces);
printf('剩余车位数:%d\n', pl->remainingSpaces);
printf('收费标准:%.2f元/分钟\n', pl->feeRate);
}
6. 完整代码
#include <stdio.h>
#include <string.h>
#include <time.h>
//定义车辆结构体
struct Car {
char plateNum[10];
int type;
time_t enterTime;
time_t exitTime;
float fee;
};
//定义停车场结构体
struct ParkingLot {
char name[20];
int totalSpaces;
int remainingSpaces;
float feeRate;
struct Car records[100];
int numCars;
};
//初始化停车场
void initParkingLot(struct ParkingLot* pl, char* name, int totalSpaces, float feeRate) {
strcpy(pl->name, name);
pl->totalSpaces = totalSpaces;
pl->remainingSpaces = totalSpaces;
pl->feeRate = feeRate;
pl->numCars = 0;
}
//进入停车场
void enterParkingLot(struct ParkingLot* pl) {
//判断是否有空余车位
if (pl->remainingSpaces > 0) {
struct Car car;
printf('请输入车牌号码:');
scanf('%s', car.plateNum);
printf('请输入车辆类型(1-小型车,2-大型车):');
scanf('%d', &car.type);
car.enterTime = time(NULL);
printf('停车成功!\n');
printf('车牌号码:%s\n', car.plateNum);
printf('车辆类型:%s\n', car.type == 1 ? '小型车' : '大型车');
printf('入场时间:%s\n', ctime(&car.enterTime));
pl->records[pl->numCars] = car;
pl->numCars++;
pl->remainingSpaces--;
} else {
printf('停车失败,车位已满!\n');
}
}
//出停车场
void exitParkingLot(struct ParkingLot* pl) {
char plateNum[10];
printf('请输入车牌号码:');
scanf('%s', plateNum);
int found = 0;
for (int i = 0; i < pl->numCars; i++) {
if (strcmp(pl->records[i].plateNum, plateNum) == 0) {
found = 1;
pl->records[i].exitTime = time(NULL);
int duration = (int)(pl->records[i].exitTime - pl->records[i].enterTime) / 60;
pl->records[i].fee = duration * pl->feeRate;
printf('离开停车场!\n');
printf('车牌号码:%s\n', pl->records[i].plateNum);
printf('车辆类型:%s\n', pl->records[i].type == 1 ? '小型车' : '大型车');
printf('入场时间:%s', ctime(&pl->records[i].enterTime));
printf('出场时间:%s', ctime(&pl->records[i].exitTime));
printf('停车费用:%.2f元\n', pl->records[i].fee);
for (int j = i; j < pl->numCars - 1; j++) {
pl->records[j] = pl->records[j + 1];
}
pl->numCars--;
pl->remainingSpaces++;
break;
}
}
if (!found) {
printf('未找到该车辆的停车记录!\n');
}
}
//查询停车场信息
void queryParkingLot(struct ParkingLot* pl) {
printf('停车场名称:%s\n', pl->name);
printf('总车位数:%d\n', pl->totalSpaces);
printf('剩余车位数:%d\n', pl->remainingSpaces);
printf('收费标准:%.2f元/分钟\n', pl->feeRate);
}
int main() {
struct ParkingLot pl;
initParkingLot(&pl, 'ABC停车场', 50, 0.1);
while (1) {
printf('请选择操作:\n');
printf('1.进入停车场\n');
printf('2.出停车场\n');
printf('3.查询停车场信息\n');
printf('4.退出程序\n');
int choice;
scanf('%d', &choice);
switch (choice) {
case 1:
enterParkingLot(&pl);
break;
case 2:
exitParkingLot(&pl);
break;
case 3:
queryParkingLot(&pl);
break;
case 4:
printf('程序结束!\n');
return 0;
default:
printf('无效的操作!\n');
break;
}
}
}
7. 总结
本文介绍了用C语言实现停车场管理系统的基本原理和示例代码,包括数据结构定义、初始化、进入停车场、出停车场和查询停车场信息等功能。实际应用中,还可以根据需要添加其他功能,例如预约停车、违章记录等。
原文地址: https://www.cveoy.top/t/topic/ok0w 著作权归作者所有。请勿转载和采集!