停车场管理系统代码c语言
时间: 2024-06-13 21:01:25 浏览: 24
停车场管理系统是一个比较常见的项目,它主要涉及到车辆进入停车场、车辆离开停车场、计费等功能。以下是停车场管理系统代码C语言的基本实现:
1.车辆信息结构体定义:
```
struct Car {
char licensePlate[10]; // 车牌号
int inHour; // 进入停车场的小时数
int inMinute; // 进入停车场的分钟数
int outHour; // 离开停车场的小时数
int outMinute; // 离开停车场的分钟数
float cost; // 停车费用
};
```
2.计算停车费用函数:
```
float calculateCost(struct Car car) {
float cost = 0;
int time = (car.outHour - car.inHour) * 60 + (car.outMinute - car.inMinute);
if (time <= 30) {
cost = 0;
} else if (time <= 120) {
cost = (time - 30) / 30 * 2;
} else {
cost = 4 + (time - 120) / 60 * 3;
}
return cost;
}
```
3.添加车辆信息函数:
```
void addCar(struct Car car, struct Car cars[], int *numCars) {
cars[*numCars] = car;
(*numCars)++;
}
```
4.查找车辆信息函数:
```
void searchCar(char licensePlate[], struct Car cars[], int numCars) {
for (int i = 0; i < numCars; i++) {
if (strcmp(cars[i].licensePlate, licensePlate) == 0) {
printf("车牌号:%s\n", cars[i].licensePlate);
printf("进入时间:%02d:%02d\n", cars[i].inHour, cars[i].inMinute);
printf("离开时间:%02d:%02d\n", cars[i].outHour, cars[i].outMinute);
printf("停车费用:%.2f元\n", cars[i].cost);
return;
}
}
printf("没有找到该车辆信息!\n");
}
```
5.主函数:
```
int main() {
struct Car cars[100];
int numCars = 0;
int choice;
do {
printf("\n");
printf("1. 添加车辆信息\n");
printf("2. 查找车辆信息\n");
printf("3. 退出程序\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
struct Car car;
printf("请输入车牌号:");
scanf("%s", car.licensePlate);
printf("请输入进入停车场的小时数:");
scanf("%d", &car.inHour);
printf("请输入进入停车场的分钟数:");
scanf("%d", &car.inMinute);
printf("请输入离开停车场的小时数:");
scanf("%d", &car.outHour);
printf("请输入离开停车场的分钟数:");
scanf("%d", &car.outMinute);
car.cost = calculateCost(car);
addCar(car, cars, &numCars);
break;
case 2:
char licensePlate[10];
printf("请输入要查找的车牌号:");
scanf("%s", licensePlate);
searchCar(licensePlate, cars, numCars);
break;
case 3:
printf("程序已退出!\n");
break;
default:
printf("无效操作!\n");
}
} while (choice != 3);
return 0;
}
```
相关推荐
![cpp](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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)