停车场管理系统c语言
时间: 2023-09-11 12:03:35 浏览: 220
停车场管理系统 C语言
您好!对于停车场管理系统的C语言实现,您可以参考以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SLOTS 100
typedef struct {
int slotNumber;
char licensePlate[10];
bool isOccupied;
} ParkingSlot;
ParkingSlot parkingSlots[MAX_SLOTS];
void initializeParkingSlots() {
for (int i = 0; i < MAX_SLOTS; i++) {
parkingSlots[i].slotNumber = i + 1;
parkingSlots[i].isOccupied = false;
}
}
void parkVehicle(char licensePlate[]) {
for (int i = 0; i < MAX_SLOTS; i++) {
if (!parkingSlots[i].isOccupied) {
parkingSlots[i].isOccupied = true;
strcpy(parkingSlots[i].licensePlate, licensePlate);
printf("Vehicle with license plate %s parked at slot number %d\n", licensePlate, parkingSlots[i].slotNumber);
return;
}
}
printf("No available slots\n");
}
void removeVehicle(int slotNumber) {
if (slotNumber < 1 || slotNumber > MAX_SLOTS) {
printf("Invalid slot number\n");
return;
}
if (!parkingSlots[slotNumber - 1].isOccupied) {
printf("Slot already empty\n");
return;
}
parkingSlots[slotNumber - 1].isOccupied = false;
printf("Vehicle with license plate %s removed from slot number %d\n", parkingSlots[slotNumber - 1].licensePlate, slotNumber);
}
void printStatus() {
printf("Slot Number\tLicense Plate\n");
for (int i = 0; i < MAX_SLOTS; i++) {
if (parkingSlots[i].isOccupied) {
printf("%d\t\t%s\n", parkingSlots[i].slotNumber, parkingSlots[i].licensePlate);
}
}
}
int main() {
initializeParkingSlots();
int choice;
char licensePlate[10];
int slotNumber;
do {
printf("\n--- Parking Lot Management System ---\n");
printf("1. Park Vehicle\n");
printf("2. Remove Vehicle\n");
printf("3. Print Status\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter license plate: ");
scanf("%s", licensePlate);
parkVehicle(licensePlate);
break; case 2:
printf("Enter slot number: ");
scanf("%d", &slotNumber);
removeVehicle(slotNumber);
break;
case 3:
printStatus();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
break;
}
} while (choice != 4);
return 0;
}
```
这是一个简单的停车场管理系统,包含了停车、取车和打印状态等功能。您可以根据需要进行修改和扩展。希望对您有帮助!如果您有任何问题,请随时提问。
阅读全文