#include <stdio.h> #include <stdlib.h> #include <string.h>

typedef struct { char type[20]; char licensePlate[20]; char parkingLocation[20]; int parkingTime; float parkingFee; } Vehicle;

typedef struct { float carFee; float motorcycleFee; float bicycleFee; } Fee;

void addVehicle(Vehicle *vehicles, int *count, FILE *file); void modifyVehicle(Vehicle *vehicles, int count, FILE *file); void deleteVehicle(Vehicle *vehicles, int *count, FILE *file); void customizeFee(Fee *fee, FILE *file); void queryVehicle(Vehicle *vehicles, int count); void payFee(Vehicle *vehicles, int count, FILE *file);

int main() { Vehicle vehicles = (Vehicle)malloc(100 * sizeof(Vehicle)); int count = 0; Fee fee = {5.0, 3.0, 1.0}; FILE *file = fopen("vehicles.txt", "a+");

int choice;
do {
    printf("Welcome to the Parking Lot Management System\n");
    printf("1. Add Vehicle\n");
    printf("2. Modify Vehicle\n");
    printf("3. Delete Vehicle\n");
    printf("4. Customize Fee\n");
    printf("5. Query Vehicle\n");
    printf("6. Pay Fee\n");
    printf("0. Exit\n");
    printf("Please enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            addVehicle(vehicles, &count, file);
            break;
        case 2:
            modifyVehicle(vehicles, count, file);
            break;
        case 3:
            deleteVehicle(vehicles, &count, file);
            break;
        case 4:
            customizeFee(&fee, file);
            break;
        case 5:
            queryVehicle(vehicles, count);
            break;
        case 6:
            payFee(vehicles, count, file);
            break;
        case 0:
            printf("Thank you for using the Parking Lot Management System!\n");
            break;
        default:
            printf("Invalid choice, please try again.\n");
    }

    printf("\n");
} while (choice != 0);

free(vehicles);
fclose(file);

return 0;

}

void addVehicle(Vehicle *vehicles, int *count, FILE *file) { printf("Add Vehicle\n");

printf("Enter vehicle type (Car/Motorcycle/Bicycle): ");
scanf("%s", vehicles[*count].type);

printf("Enter license plate: ");
scanf("%s", vehicles[*count].licensePlate);

printf("Enter parking location: ");
scanf("%s", vehicles[*count].parkingLocation);

printf("Enter parking time (in minutes): ");
scanf("%d", &vehicles[*count].parkingTime);

if (strcmp(vehicles[*count].type, "Car") == 0) {
    vehicles[*count].parkingFee = vehicles[*count].parkingTime * 0.01;
} else if (strcmp(vehicles[*count].type, "Motorcycle") == 0) {
    vehicles[*count].parkingFee = vehicles[*count].parkingTime * 0.02;
} else if (strcmp(vehicles[*count].type, "Bicycle") == 0) {
    vehicles[*count].parkingFee = vehicles[*count].parkingTime * 0.03;
}

fprintf(file, "%s %s %s %d %.2f\n", vehicles[*count].type, vehicles[*count].licensePlate, vehicles[*count].parkingLocation, vehicles[*count].parkingTime, vehicles[*count].parkingFee);

(*count)++;

}

void modifyVehicle(Vehicle *vehicles, int count, FILE *file) { printf("Modify Vehicle\n");

char licensePlate[20];
printf("Enter license plate of the vehicle to modify: ");
scanf("%s", licensePlate);

int found = 0;
for (int i = 0; i < count; i++) {
    if (strcmp(vehicles[i].licensePlate, licensePlate) == 0) {
        printf("Enter new parking location: ");
        scanf("%s", vehicles[i].parkingLocation);

        printf("Enter new parking time (in minutes): ");
        scanf("%d", &vehicles[i].parkingTime);

        if (strcmp(vehicles[i].type, "Car") == 0) {
            vehicles[i].parkingFee = vehicles[i].parkingTime * 0.01;
        } else if (strcmp(vehicles[i].type, "Motorcycle") == 0) {
            vehicles[i].parkingFee = vehicles[i].parkingTime * 0.02;
        } else if (strcmp(vehicles[i].type, "Bicycle") == 0) {
            vehicles[i].parkingFee = vehicles[i].parkingTime * 0.03;
        }

        fprintf(file, "%s %s %s %d %.2f\n", vehicles[i].type, vehicles[i].licensePlate, vehicles[i].parkingLocation, vehicles[i].parkingTime, vehicles[i].parkingFee);

        printf("Vehicle information modified successfully.\n");
        found = 1;
        break;
    }
}

if (!found) {
    printf("Vehicle not found.\n");
}

}

void deleteVehicle(Vehicle *vehicles, int *count, FILE *file) { printf("Delete Vehicle\n");

char licensePlate[20];
printf("Enter license plate of the vehicle to delete: ");
scanf("%s", licensePlate);

int found = 0;
for (int i = 0; i < *count; i++) {
    if (strcmp(vehicles[i].licensePlate, licensePlate) == 0) {
        for (int j = i; j < *count - 1; j++) {
            vehicles[j] = vehicles[j + 1];
        }
        (*count)--;
        printf("Vehicle deleted successfully.\n");
        found = 1;
        break;
    }
}

if (!found) {
    printf("Vehicle not found.\n");
}

// Rewrite the file with the updated vehicle information
fclose(file);
file = fopen("vehicles.txt", "w");
for (int i = 0; i < *count; i++) {
    fprintf(file, "%s %s %s %d %.2f\n", vehicles[i].type, vehicles[i].licensePlate, vehicles[i].parkingLocation, vehicles[i].parkingTime, vehicles[i].parkingFee);
}

}

void customizeFee(Fee *fee, FILE *file) { printf("Customize Fee\n");

printf("Enter car fee per minute: ");
scanf("%f", &fee->carFee);

printf("Enter motorcycle fee per minute: ");
scanf("%f", &fee->motorcycleFee);

printf("Enter bicycle fee per minute: ");
scanf("%f", &fee->bicycleFee);

fprintf(file, "%.2f %.2f %.2f\n", fee->carFee, fee->motorcycleFee, fee->bicycleFee);

printf("Fee customized successfully.\n");

}

void queryVehicle(Vehicle *vehicles, int count) { printf("Query Vehicle\n");

char licensePlate[20];
printf("Enter license plate of the vehicle to query: ");
scanf("%s", licensePlate);

int found = 0;
for (int i = 0; i < count; i++) {
    if (strcmp(vehicles[i].licensePlate, licensePlate) == 0) {
        printf("Vehicle Type: %s\n", vehicles[i].type);
        printf("License Plate: %s\n", vehicles[i].licensePlate);
        printf("Parking Location: %s\n", vehicles[i].parkingLocation);
        printf("Parking Time: %d minutes\n", vehicles[i].parkingTime);
        printf("Parking Fee: %.2f\n", vehicles[i].parkingFee);
        found = 1;
        break;
    }
}

if (!found) {
    printf("Vehicle not found.\n");
}

}

void payFee(Vehicle *vehicles, int count, FILE *file) { printf("Pay Fee\n");

char licensePlate[20];
printf("Enter license plate of the vehicle to pay fee: ");
scanf("%s", licensePlate);

int found = 0;
for (int i = 0; i < count; i++) {
    if (strcmp(vehicles[i].licensePlate, licensePlate) == 0) {
        float feeToPay = vehicles[i].parkingFee;
        printf("Fee to Pay: %.2f\n", feeToPay);

        int paymentMethod;
        printf("Select payment method:\n");
        printf("1. Cash\n");
        printf("2. Credit Card\n");
        printf("Please enter your choice: ");
        scanf("%d", &paymentMethod);

        if (paymentMethod == 1) {
            printf("Payment successful. Thank you!\n");
            found = 1;
            break;
        } else if (paymentMethod == 2) {
            float creditCardNumber;
            printf("Enter credit card number: ");
            scanf("%f", &creditCardNumber);

            printf("Payment successful. Thank you!\n");
            found = 1;
            break;
        } else {
            printf("Invalid payment method.\n");
        }
    }
}

if (!found) {
    printf("Vehicle not found.\n");
}
#include stdioh#include stdlibh#include stringhtypedef struct char type20; char licensePlate20; char parkingLocation20; int parkingTime; float parkingFee; Vehicle;typedef struct fl

原文地址: https://www.cveoy.top/t/topic/hPCq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录