1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_SIZE 10 #define PARKING_RATE_PER_HOUR 10
typedef struct { char licensePlate[10]; int arrivalTime; } Car;
typedef struct { Car cars[MAX_SIZE]; int top; } ParkingLot;
typedef struct { Car cars[MAX_SIZE]; int front, rear; } Queue;
void initParkingLot(ParkingLot* parkingLot) { parkingLot->top = -1; }
void initQueue(Queue* queue) { queue->front = -1; queue->rear = -1; }
bool isParkingLotFull(ParkingLot* parkingLot) { return parkingLot->top == MAX_SIZE - 1; }
bool isParkingLotEmpty(ParkingLot* parkingLot) { return parkingLot->top == -1; }
bool isQueueEmpty(Queue* queue) { return queue->front == -1; }
void parkCar(ParkingLot* parkingLot, Car car) { if (!isParkingLotFull(parkingLot)) { parkingLot->top++; parkingLot->cars[parkingLot->top] = car; printf("车牌为 %s 的车辆停在停车场。\n", car.licensePlate); } else { printf("停车场已满,车牌为 %s 的车必须等待。\n", car.licensePlate); } }
void carLeaves(ParkingLot* parkingLot, int position, int leaveTime) { if (!isParkingLotEmpty(parkingLot) && position >= 0 && position <= parkingLot->top) { Car car = parkingLot->cars[position]; printf("车牌为 %s 的汽车从停车场驶出。\n", car.licensePlate); int parkingTime = leaveTime - car.arrivalTime; printf("停车场内停留时间: %d 个单位。\n", leaveTime - car.arrivalTime); int parkingFee = parkingTime * PARKING_RATE_PER_HOUR; printf("停车费用: %d 元.\n", parkingFee); for (int i = position; i < parkingLot->top; i++) { parkingLot->cars[i] = parkingLot->cars[i + 1]; } parkingLot->top--; } else { printf("无效位置或停车场空无一人!.\n"); } }
void enterQueue(Queue* queue, Car car) { if (queue->rear == MAX_SIZE - 1) { printf("队列已满,车牌为 %s 的车辆无法进入队列。\n", car.licensePlate); } else { if (isQueueEmpty(queue)) { queue->front = 0; } queue->rear++; queue->cars[queue->rear] = car; printf("车牌为 %s 的车辆进入队列。\n", car.licensePlate); } }
void leaveQueue(Queue* queue) { if (!isQueueEmpty(queue)) { Car car = queue->cars[queue->front]; printf("车牌为 %s 的车辆从队列中离开。\n", car.licensePlate); for (int i = queue->front; i < queue->rear; i++) { queue->cars[i] = queue->cars[i + 1]; } queue->rear--; if (queue->front == queue->rear) { queue->front = -1; queue->rear = -1; } } else { printf("队列已空。\n"); } }
void displayStatus(ParkingLot* parkingLot, Queue* queue) { printf("停车场状态:\n"); if (isParkingLotEmpty(parkingLot)) { printf("Empty\n"); } else { for (int i = 0; i <= parkingLot->top; i++) { printf("位置 %d: 带车牌的汽车 %s\n", i, parkingLot->cars[i].licensePlate); } } printf("\nQueue Status:\n"); if (isQueueEmpty(queue)) { printf("Empty\n"); } else { for (int i = queue->front; i <= queue->rear; i++) { printf("Position %d: Car with license plate %s\n", i, queue->cars[i].licensePlate); } } } int main() { ParkingLot parkingLot; Queue queue; initParkingLot(&parkingLot); initQueue(&queue); int choice; do { printf("\n菜单:\n"); printf("1. 车辆到达\n"); printf("2. 车辆离开\n"); printf("3. 显示出停车场内及便道上的停车状况\n"); printf("4. 退出系统\n"); printf("输入您的选择: "); scanf("%d", &choice); switch (choice) { case 1: { Car newCar; printf("输入车牌号: "); scanf("%s", newCar.licensePlate); printf("输入到达时间: "); scanf("%d", &newCar.arrivalTime); if (!isParkingLotFull(&parkingLot)) { parkCar(&parkingLot, newCar); } else { enterQueue(&queue, newCar); } break; } case 2: { int position, leaveTime; printf("输入汽车离开位置: "); scanf("%d", &position); printf("输入出发时间: "); scanf("%d", &leaveTime); carLeaves(&parkingLot, position, leaveTime); if (!isQueueEmpty(&queue)) { Car car = queue.cars[queue.front]; leaveQueue(&queue); parkCar(&parkingLot, car); } break; } case 3: displayStatus(&parkingLot, &queue); break; case 4: printf("退出。\n"); break; default: printf("选择无效,请重新输入!\n"); } } while (choice != 4); return 0; }
|