这个代码实现了一个基本的停车场管理系统,包括停车、结算费用和显示停车记录等功能。你可以根据自己的需求修改代码,添加更多功能。
#include #include #include #define MAX_CAPACITY 100typedef struct {int id;char licensePlate[10];int timeIn;int timeOut;float cost;} ParkingRecord;ParkingRecord parkingLot[MAX_CAPACITY];int currentCapacity = 0;void showMenu() {printf("Welcome to the Parking Lot Management System!\n");printf("1. Park a car\n");printf("2. Check out a car\n");printf("3. Show parking records\n");printf("4. Exit\n");printf("Please enter your choice: ");}void parkCar() {if (currentCapacity >= MAX_CAPACITY) {printf("Sorry, the parking lot is full.\n");return;}ParkingRecord newRecord;printf("Please enter the license plate number (up to 9 characters): ");scanf("%s", newRecord.licensePlate);printf("Please enter the time in (in minutes since 00:00): ");scanf("%d", &newRecord.timeIn);newRecord.id = currentCapacity + 1;parkingLot[currentCapacity] = newRecord;currentCapacity++;printf("The car is parked at spot %d.\n", newRecord.id);}void checkOutCar() {int id;printf("Please enter the parking spot number: ");scanf("%d", &id);if (id currentCapacity) {printf("Invalid parking spot number.\n");return;}ParkingRecord *record = &parkingLot[id-1];if (record->timeOut > 0) {printf("The car has already been checked out.\n");return;}printf("Please enter the time out (in minutes since 00:00): ");scanf("%d", &record->timeOut);float hours = (record->timeOut - record->timeIn) / 60.0;record->cost = hours * 1.5;printf("The cost for parking is $%.2f.\n", record->cost);}void showRecords() {printf("Parking Records:\n");printf("ID\tLicense Plate\tTime In\tTime Out\tCost\n");for (int i = 0; i < currentCapacity; i++) {ParkingRecord record = parkingLot[i];printf("%d\t%s\t%d\t%d\t$%.2f\n", record.id, record.licensePlate, record.timeIn, record.timeOut, record.cost);}}int main() {int choice;do {showMenu();scanf("%d", &choice);switch (choice) {case 1:parkCar();break;case 2:checkOutCar();break;case 3:showRecords();break;case 4:printf("Thank you for using the Parking Lot Management System!\n");break;default:printf("Invalid choice.\n");}} while (choice != 4);return 0;}
#########################分隔符########################################
接下来来解释下代码
这个代码实现了一个简单的停车场管理系统,包括停车、结算费用和显示停车记录等功能。下面是代码的主要部分:
定义结构体 ParkingRecord
typedef struct {int id;char licensePlate[10];int timeIn;int timeOut;float cost;} ParkingRecord;
这个结构体用来表示一条停车记录,包括停车位编号、车牌号、进入时间、离开时间和停车费用。
定义数组 parkingLot 和变量 currentCapacity
ParkingRecord parkingLot[MAX_CAPACITY];int currentCapacity = 0;
这个数组表示停车场的所有停车位,最多可以容纳 MAX_CAPACITY 辆车。变量 currentCapacity 表示当前停车场中已停车辆的数量。
定义函数 showMenu
void showMenu() {printf("Welcome to the Parking Lot Management System!\n");printf("1. Park a car\n");printf("2. Check out a car\n");printf("3. Show parking records\n");printf("4. Exit\n");printf("Please enter your choice: ");}
这个函数用来显示菜单,让用户选择相应的操作。
定义函数 parkCar
void parkCar() {if (currentCapacity >= MAX_CAPACITY) {printf("Sorry, the parking lot is full.\n");return;}ParkingRecord newRecord;printf("Please enter the license plate number (up to 9 characters): ");scanf("%s", newRecord.licensePlate);printf("Please enter the time in (in minutes since 00:00): ");scanf("%d", &newRecord.timeIn);newRecord.id = currentCapacity + 1;parkingLot[currentCapacity] = newRecord;currentCapacity++;printf("The car is parked at spot %d.\n", newRecord.id);}
这个函数用来实现停车功能。如果停车场已经满了,则输出提示信息并返回。否则,让用户输入车牌号和进入时间,创建一条停车记录,并将其添加到 parkingLot 数组中。最后,输出停车位编号。
定义函数 checkOutCar
void checkOutCar() {int id;printf("Please enter the parking spot number: ");scanf("%d", &id);if (id currentCapacity) {printf("Invalid parking spot number.\n");return;}ParkingRecord *record = &parkingLot[id-1];if (record->timeOut > 0) {printf("The car has already been checked out.\n");return;}printf("Please enter the time out (in minutes since 00:00): ");scanf("%d", &record->timeOut);float hours = (record->timeOut - record->timeIn) / 60.0;record->cost = hours * 1.5;printf("The cost for parking is $%.2f.\n", record->cost);}``
定义函数 showParkingRecords
void showParkingRecords() {if (currentCapacity == 0) {printf("There is no car parked in the parking lot.\n");return;}printf("Parking records:\n");printf("ID\tLicense Plate\tTime In\tTime Out\tCost\n");for (int i = 0; i < currentCapacity; i++) {ParkingRecord record = parkingLot[i];printf("%d\t%s\t\t%d\t%d\t\t%.2f\n", record.id, record.licensePlate, record.timeIn, record.timeOut, record.cost);}}
这个函数用来显示停车记录。如果当前停车场中没有车辆,则输出提示信息并返回。否则,按照指定格式输出所有停车记录。
定义函数 main
int main() {int choice;do {showMenu();scanf("%d", &choice);switch (choice) {case 1:parkCar();break;case 2:checkOutCar();break;case 3:showParkingRecords();break;case 4:printf("Thank you for using the Parking Lot Management System!\n");break;default:printf("Invalid choice. Please try again.\n");break;}} while (choice != 4);return 0;}
这个函数是程序的主函数,用来接收用户的输入,并根据用户的选择调用相应的函数。通过一个 do-while 循环,一直显示菜单,直到用户选择退出。