[槲寄生小记]停车场管理系统(初步)

仅作记录备份:

#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;
}

面向过程方法

  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值