仅作记录备份:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 2
struct Car {
char licensePlate[10];
char color[10];
char type[10];
int arrivalTime;
int departureTime;
};
struct Car parkingLot[MAX_SIZE];
struct Car waitingline[MAX_SIZE];
int numCars = 0;
int e1 = 0;
int account = 0;
int testflag = 1;
void enterParkingLot(char licensePlate[], char type[], char color[], int arrivalTime) {
if (testflag <= MAX_SIZE) {
account++;
struct Car newCar;
strcpy(newCar.licensePlate, licensePlate);
strcpy(newCar.type, type);
strcpy(newCar.color, color);
newCar.arrivalTime = arrivalTime;
parkingLot[numCars] = newCar;
numCars++;
printf("Car with license plate %s entered the parking lot.\n", licensePlate);
}
if (testflag > MAX_SIZE) {
e1 = 1;
struct Car waitingCar;
strcpy(waitingCar.licensePlate, licensePlate);
strcpy(waitingCar.type, type);
strcpy(waitingCar.color, color);
waitingline[e1 - 1] = waitingCar;
e1++;
printf("Parking lot is full. Car with license plate %s cannot enter.\n STAND BY", licensePlate);
}
testflag++;
}
void exitParkingLot(char licensePlate[], char type[], char color[], int departureTime) {
int i, found = 0;
for (i = 0; i < numCars; i++) {
if (strcmp(parkingLot[i].licensePlate, licensePlate) == 0) {
int parkingTime = departureTime - parkingLot[i].arrivalTime;
int parkingFee = parkingTime * 10; // Assuming parking fee is 10 per hour
printf("Car with license plate %s exited the parking lot. Parking fee: %d\n", licensePlate, parkingFee);
found = 1;
strcpy(parkingLot[i].licensePlate, ""); // Remove the car from the parking lot
for (int j = i; j < numCars - 1; j++) {
parkingLot[j] = parkingLot[j + 1];
}
numCars--;
testflag--;
int e2 = 0;
if (waitingline[e2].licensePlate[0] != '\0') {
parkingLot[numCars] = waitingline[e2];
numCars++;
e2++;
}
break;
}
}
if (!found) {
printf("Car with license plate %s is not in the parking lot.\n", licensePlate);
}
}
void searchCar(char licensePlate[]) {
int found = 0;
for (int i = 0; i < numCars; i++) {
if (strcmp(parkingLot[i].licensePlate, licensePlate) == 0) {
printf("Car with license plate %s is in the parking lot.\n", licensePlate);
printf("Type: %s\n", parkingLot[i].type);
printf("Color: %s\n", parkingLot[i].color);
printf("Arrival Time: %d\n", parkingLot[i].arrivalTime);
found = 1;
break;
}
}
if (!found) {
printf("Car with license plate %s is not in the parking lot.\n", licensePlate);
}
}
void parkingLotDataCheck() {
int flag = 0;
for (int i = 0; i < numCars; i++) {
if (strcmp(parkingLot[i].licensePlate, "") != 0) {
printf("%d. licensePlate %s type %s color %s \n", i + 1, parkingLot[i].licensePlate, parkingLot[i].type, parkingLot[i].color);
flag = 1;
}
}
if (flag == 0) {
printf("No cars in the parking lot.\n");
}
}
int main() {
int choice, arrivalTime, departureTime;
int m = 0;
int flag = 0;
char licensePlate[10];
char color[10];
char type[10];
while (1) {
printf("\n\n**************************Parking management system****************************\n");
printf(" ******************************\n");
printf(" 1. Enter parking lot\n");
printf(" 2. Exit parking lot\n");
printf(" 3. Search car\n");
printf(" 4. Parking lot data check\n");
printf(" 5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter license plate: ");
scanf("%s", licensePlate);
if (m > 0) {
for (int i = 0; i <= m; i++) {
if (strcmp(licensePlate, parkingLot[m - 1].licensePlate) == 0) {
printf("Add duplicate data, cancel the addition.\n");
flag = 1;
break;
}
}
for (int i = 0; i <= m; i++) {
if (strcmp(licensePlate, waitingline[m - 1].licensePlate) == 0) {
printf("Add duplicate data, cancel the addition.\n");
flag = 1;
break;
}
}
}
if (flag != 1) {
printf("Enter type: ");
scanf("%s", type);
printf("Enter color: ");
scanf("%s", color);
printf("Enter arrival time: ");
scanf("%d", &arrivalTime);
enterParkingLot(licensePlate, type, color, arrivalTime);
m++;
}
flag = 0;
break;
case 2:
printf("Enter license plate: ");
scanf("%s", licensePlate);
printf("Enter type: ");
scanf("%s", type);
printf("Enter color: ");
scanf("%s", color);
printf("Enter departure time: ");
scanf("%d", &departureTime);
exitParkingLot(licensePlate, type, color, departureTime);
break;
case 3:
printf("Enter license plate: ");
scanf("%s", licensePlate);
searchCar(licensePlate);
break;
case 4:
parkingLotDataCheck();
break;
case 5:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
面向过程方法