生成代码停车场管理系统c语言
时间: 2024-10-18 19:13:17 浏览: 27
编写一个简单的C语言停车场管理系统可以包括以下几个部分:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int slot_number; // 停车位编号
int status; // 停车状态,0表示空位,1表示已占用
} ParkingSpot;
// 假设最大停车位数量
#define MAX_SPOTS 50
ParkingSpot parking_spots[MAX_SPOTS];
void display_status() {
for (int i = 0; i < MAX_SPOTS; i++) {
if (parking_spots[i].status == 0)
printf("Slot %d: Empty\n", i+1);
else
printf("Slot %d: Occupied\n", i+1);
}
}
int park(int slot) {
if (slot >= 1 && slot <= MAX_SPOTS && parking_spots[slot-1].status == 0) {
parking_spots[slot-1].status = 1;
return 1; // 成功停车
} else {
printf("Sorry, Slot %d is not available.\n", slot);
return 0; // 无法停车
}
}
int leave(int slot) {
if (slot >= 1 && slot <= MAX_SPOTS && parking_spots[slot-1].status == 1) {
parking_spots[slot-1].status = 0;
return 1; // 成功离开
} else {
printf("Invalid slot number or spot is already empty.\n");
return 0; // 无法离开
}
}
int main() {
int choice;
while (1) {
printf("\nPark and Leave Parking System:\n");
printf("1. View Status\n");
printf("2. Park\n");
printf("3. Leave\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch(choice) {
case 1: display_status(); break;
case 2: { int slot; scanf("%d", &slot); park(slot); } break;
case 3: { int slot; scanf("%d", &slot); leave(slot); } break;
case 4: exit(0); // 退出系统
default: printf("Invalid option.\n");
}
}
}
阅读全文