停车场管理程序设计

 停车场管理系统 - C语言实现

简介
本文介绍了一个简单的停车场管理系统的C语言实现。该系统允许用户将车辆添加到停车场、从停车场移除车辆,查看停车场内外的车辆情况,以及对停车时长进行计算和收费等功能。

一、代码结构
1、结构体
- `Car`:车辆信息结构体,包括车牌号、车主信息、位置、到达时间和收费金额等属性。
- `Stack`:栈结构,用于实现停车场。
- `CQueueNode`:队列节点结构,用于实现停车场外便道。
- `LinkQueue`:队列结构,用于管理停车场外便道上的车辆。

2、主要函数
- `InitStack`:初始化栈。
- `Push`:将车辆入栈。
- `IsEmpty`:判断栈是否为空。
- `IsFull`:判断栈是否已满。
- `In`:车辆到达停车场。
- `Out`:车辆离开停车场。
- `saveDates`:将停车场数据保存到磁盘文件。
- `readDates`:从磁盘文件读取停车场数据。

3、 控制流程
`control` 函数实现了停车场管理系统的各项功能,包括车辆到达、离开、查看停车场情况、数据持久化等。

4、数据持久化
本系统实现了数据持久化功能,可以将停车场数据保存到磁盘文件,并在需要时从磁盘文件读取数据。

二、结论
停车场管理系统的实现对于学习C语言和数据结构非常重要。通过本文介绍的系统,读者可以了解如何使用C语言实现简单的数据结构和算法,并了解数据持久化的实现方法。

三、实现代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#define SIZE 3 //停车场容量

typedef struct
{
    char num[6]; // 车牌号
    char owner[20]; // 车主信息
    int position; // 位置
    int arrivalTime; // 到达时间(以分钟为单位)
    float money; // 收费金额
} Car;

typedef struct
{
    Car elem[SIZE + 1];
    int top;
} Stack;

typedef struct Node
{
    Car data;
    struct Node* next;
} CQueueNode;

typedef struct
{
    CQueueNode* front;
    CQueueNode* rear;
} LinkQueue;

void control();
void InitStack(Stack* S);
void Push(Stack* S, Car* r);
int IsEmpty(Stack* S);
int IsFull(Stack* S);
int GetTop(Stack* S, Car* n);
void InitQueue(LinkQueue* Q);
int EnterQueue(LinkQueue* Q, Car* t);
int DeleteQueue(LinkQueue* Q, Car* x);
void print1(Stack* S);
void print2(LinkQueue* Q);
void TaM(Car* r, int arrivalTime);
void In(Stack* S, LinkQueue* Q, Car* r);
void Out(Stack* S, Stack* S0, Car* r, LinkQueue* Q);
void print();
int saveDates(Stack* S);
int readDates(Stack* S);
void SortByParkingTime(Car* cars, int size);
void printSortedParkingData(Car* cars, int size);
int FindCarByNum(Stack* S, const char* num, Car* result);
void SortByCarNum(Car* cars, int size);
void printSortedCarData(Car* cars, int size);
void CalculateParkingDuration(Car* cars, int size, int arrivalTime);
int isLegalLicensePlateNumber(char* licensePlateNumber);

int main()
{
    control();
    return 0;
}

void InitStack(Stack* S)
{
    S->top = 0;
}

void Push(Stack* S, Car* r)
{
    S->top++;
    strcpy(S->elem[S->top].num, r->num);
    strcpy(S->elem[S->top].owner, r->owner);
    r->position = S->elem[S->top].position = S->top;
    S->elem[S->top].arrivalTime = r->arrivalTime;
}

int IsEmpty(Stack* S)
{
    return (S->top == 0 ? 1 : 0);
}

int IsFull(Stack* S)
{
    return (S->top == SIZE ? 1 : 0);
}

int GetTop(Stack* S, Car* n)
{
    strcpy(n->num, S->elem[S->top].num);
    strcpy(n->owner, S->elem[S->top].owner);
    n->position = S->elem[S->top].position;
    n->arrivalTime = S->elem[S->top].arrivalTime;
    return 1;
}

void InitQueue(LinkQueue* Q)
{
    Q->front = (CQueueNode*)malloc(sizeof(CQueueNode));
    if (Q->front != NULL)
    {
        Q->rear = Q->front;
        Q->front->next = NULL;
    }
}

int EnterQueue(LinkQueue* Q, Car* t)
{
    CQueueNode* NewNode;
    NewNode = (CQueueNode*)malloc(sizeof(CQueueNode));
    if (NewNode != NULL)
    {
        strcpy(NewNode->data.num, t->num);
        strcpy(NewNode->data.owner, t->owner);
        NewNode->data.arrivalTime = t->arrivalTime;
        NewNode->next = NULL;
        Q->rear->next = NewNode;
        Q->rear = NewNode;
        return 1;
    }
    else
    {
        return 0;
    }
}

int DeleteQueue(LinkQueue* Q, Car* x)
{
    CQueueNode* p;
    if (Q->front == Q->rear)
    {
        return 0;
    }
    p = Q->front->next;
    Q->front->next = p->next;
    if (Q->rear == p)
    {
        Q->rear = Q->front;
    }
    strcpy(x->num, p->data.num);
    strcpy(x->owner, p->data.owner);
    x->arrivalTime = p->data.arrivalTime;
    free(p);
    return 1;
}

void print1(Stack* S)
{
    int tag;
    Car x;
    printf("=========================================================================\n");
    printf("停车场停车情况:\n");
    if (IsEmpty(S))
    {
        printf("无车!");
    }
    printf("\t车牌号\t\t 车主信息\t\t停放位序\t到达时间\n");
    for (tag = S->top; S->top > 0; S->top--)
    {
        if (GetTop(S, &x))
        {
            printf("\t %s \t\t %s \t\t %d\t\t %d\n", x.num, x.owner, x.position, x.arrivalTime);
        }
    }
    printf("=========================================================================\n");
    S->top = tag;
}

void print2(LinkQueue* Q)
{
    printf("停车场外便道中等待车辆情况:\n");
    CQueueNode* p;
    p = Q->front->next;
    printf("=========================================================================\n");
    printf("\t等待车牌号\t\t车主信息\t\t到达时间\n");
    for (; p != NULL; p = p->next)
    {
        printf("\t %s \t\t %s \t\t %d\n", p->data.num, p->data.owner, p->data.arrivalTime);
    }
    printf("=========================================================================\n");
}

void TaM(Car* r, int arrivalTime)
{
    time_t currentTime;
    struct tm* localTime;
    time(&currentTime);
    localTime = localtime(&currentTime);
    int currentMinutes = localTime->tm_hour * 60 + localTime->tm_min;
    int parkingMinutes = currentMinutes - arrivalTime;
    printf("\n停车时长:%d分钟\n", parkingMinutes);
    printf("每小时收费3元\n");
    r->money = 0.05 * parkingMinutes;
    printf("请支付金额%.2f元\n", r->money);
}

void In(Stack* S, LinkQueue* Q, Car* r)
{
    if (IsFull(S))
    {
        printf("车库已满,请等待!\n");
        EnterQueue(Q, r);
    }
    else
    {
        time_t currentTime;
        struct tm* localTime;
        time(&currentTime);
        localTime = localtime(&currentTime);
        int arrivalTime = localTime->tm_hour * 60 + localTime->tm_min;
        r->arrivalTime = arrivalTime;
        Push(S, r);
        printf("\n您现在所在位置 %d\n", r->position);
    }
}

void Out(Stack* S, Stack* S0, Car* r, LinkQueue* Q)
{
    int tag = S->top;
    Car x;
    if (IsEmpty(S))
    {
        printf("没有此车!\n");
    }
    else
    {
        for (; strcmp(r->num, S->elem[tag].num) != 0 && tag > 0; --tag)
        {
            Push(S0, &S->elem[tag]);
            S->top--;
        }
        if (strcmp(r->num, S->elem[tag].num) == 0)
        {
            TaM(r, S->elem[tag].arrivalTime);
            S->top--;
            for (; S0->top > 0; S0->top--)
            {
                Push(S, &S0->elem[S0->top]);
            }
            if (S->top < SIZE&& Q->front != Q->rear)
            {
                DeleteQueue(Q, &x);
                Push(S, &x);
            }
        }
        else if (tag == 0)
        {
            printf("未进入停车场应支付金额 0元!");
            for (; S0->top > 0; S0->top--)
            {
                Push(S, &S0->elem[S0->top]);
            }
        }
    }
    printf("=========================================================================\n");
}

void CalculateParkingDuration(Car* cars, int size, int arrivalTime)
{
    printf("=========================================================================\n");
    printf("停车时长情况:\n");
    printf("\t\t车辆\t\t停车时长\t\t此时所需支付停车费\n");
    for (int i = 1; i <= size; i++)
    {
        int parkingMinutes = arrivalTime - cars[i].arrivalTime;
        cars[i].money = 0.05 * parkingMinutes;
        printf("\t\t %s \t\t %d 分钟 \t\t %.2f \n", cars[i].num, parkingMinutes, cars[i].money);
    }
    printf("=========================================================================\n");
}

void SortByParkingTime(Car* cars, int size)
{
    int i, j;
    Car temp;
    for (i = 1; i <= size - 1; ++i)
    {
        for (j = 1; j <= size - i; ++j)
        {
            if (cars[j].arrivalTime > cars[j + 1].arrivalTime)
            {
                temp = cars[j];
                cars[j] = cars[j + 1];
                cars[j + 1] = temp;
            }
        }
    }
}

void printSortedParkingData(Car* cars, int size)
{
    printf("Sorted parking data based on parking time:\n");
    for (int i = 1; i <= size; i++)
    {
        printf("车牌号 %s: 到达时间 %d\n", cars[i].num, cars[i].arrivalTime);
    }
}

void SortByCarNum(Car* cars, int size)
{
    int i, j;
    Car temp;
    for (i = 1; i <= size - 1; ++i)
    {
        for (j = 1; j <= size - i; ++j)
        {
            if (strcmp(cars[j].num, cars[j + 1].num) > 0)
            {
                temp = cars[j];
                cars[j] = cars[j + 1];
                cars[j + 1] = temp;
            }
        }
    }
}

void printSortedCarData(Car* cars, int size)
{
    printf("Sorted car data based on car number:\n");
    for (int i = 1; i <= size; i++)
    {
        printf("车牌号 %s: 到达时间 %d\n", cars[i].num, cars[i].arrivalTime);
    }
}

int FindCarByNum(Stack* S, const char* num, Car* result)
{
    for (int i = S->top; i > 0; i--)
    {
        if (strcmp(S->elem[i].num, num) == 0)
        {
            strcpy(result->num, S->elem[i].num);
            result->position = S->elem[i].position;
            result->arrivalTime = S->elem[i].arrivalTime;
            return 1; // 找到并返回
        }
    }
    return 0; // 未找到
}

int isLegalLicensePlateNumber(char* licensePlateNumber)
{
    if (strlen(licensePlateNumber) == 5)
    {
        for (int i = 0; i < 5; i++)
        {
            if (!isalpha(licensePlateNumber[i]) && !isdigit(licensePlateNumber[i]))
            {
                return 0; // 非法车牌号
            }
        }
        return 1; // 合法车牌号
    }
    else
    {
        return 0; // 非法车牌号
    }
}

int saveDates(Stack* S)
{
    FILE* fp = fopen("theParking.bin", "wb");
    if (fp == NULL)
    {
        printf("File open failed!\n");
        return 1;
    }
    fwrite(S, sizeof(Stack), 1, fp);
    fclose(fp);
    return 0;
}

int readDates(Stack* S)
{
    FILE* fp = fopen("theParking.bin", "rb");
    if (fp == NULL)
    {
        printf("File open failed!\n");
        return 1;
    }
    fread(S, sizeof(Stack), 1, fp);
    int tag;
    Car x;
    printf("=========================================================================\n");
    printf("停车场停车情况:\n");
    printf("\t车牌号\t\t车主信息\t\t停放位序\t到达时间\n");
    for (tag = S->top; S->top > 0; S->top--)
    {
        if (GetTop(S, &x))
        {
            printf("\t %s \t\t %s \t\t %d \t\t %d\n", x.num, x.owner, x.position, x.arrivalTime);
        }
    }
    printf("=========================================================================\n");
    S->top = tag;
    fclose(fp);
    return 0;
}

void print()
{
    printf("\t\t==================================================================\n");
    printf("\t\t***************************欢迎光临*******************************\n");
    printf("\t\t******************************************************************\n");
    printf("\t\t* 请选择:                                                        *\n");
    printf("\t\t*-------------------0 :退出进程----------------------------------*\n");
    printf("\t\t*-------------------1 :车辆到达停车场----------------------------*\n");
    printf("\t\t*-------------------2 :车辆离开停车场----------------------------*\n");
    printf("\t\t*-------------------3 :查看停车场内的车辆数据--------------------*\n");
    printf("\t\t*-------------------4 :查看停车场外便道上的车辆数据--------------*\n");
    printf("\t\t*-------------------5 :将Stack中的数据存入磁盘文件---------------*\n");
    printf("\t\t*-------------------6 :将磁盘文件中的数据读取到Stack-------------*\n");
    printf("\t\t*-------------------7 :将停车场中车辆按到达停车场时间排序--------*\n");
    printf("\t\t*-------------------8 :将停车场中车辆按车牌号排序----------------*\n");
    printf("\t\t*-------------------9 :根据车牌号查找车辆信息--------------------*\n");
    printf("\t\t*-------------------10:计算所有车辆到达某一时间的停车时长--------*\n");
    printf("\t\t******************************************************************\n");
    printf("\t\t-----------------收费标准:每小时3元,每分钟0.05元----------------\n");
    printf("\t\t==================================================================\n\n");
}

void control()
{
    int sel = 1;
    Car c[10] = { 0 };
    int carCount = 0;
    char carNumToFind[6] = { 0 };
    Car foundCar;
    Stack S, S0;
    LinkQueue Q;

    InitStack(&S);
    InitStack(&S0);
    InitQueue(&Q);

    system("color 0E");
    system("cls");

    while (sel != 0)
    {
        print();
        printf("Input your choice: ");
        scanf("%d", &sel);
        fflush(stdin);

        switch (sel)
        {
        case 0:
            return;
        case 1:
            if (carCount < 10)
            {
                char ch[6] = { 0 };
                int n = 0;
                printf("Enter car number: ");
                scanf("%s", ch);
                n = isLegalLicensePlateNumber(ch);
                if (n == 0)
                {
                    printf("Invalid license plate number!\n");
                    break;
                }
                else
                {
                    printf("Valid license plate number!\n");
                }
                strcpy(c[carCount].num, ch);
                printf("Enter owner information: ");
                scanf("%s", c[carCount].owner);
                printf("Car arrival time is recorded.\n");
                In(&S, &Q, &c[carCount]);
                carCount++;
            }
            else
            {
                printf("No available parking space!\n");
            }
            break;
        case 2:
            printf("Enter car number: ");
            scanf("%s", carNumToFind);
            fflush(stdin);
            for (int i = 0; i < carCount; i++)
            {
                if (strcmp(carNumToFind, c[i].num) == 0)
                {
                    Out(&S, &S0, &c[i], &Q);
                    break;
                }
            }
            break;
        case 3:
            print1(&S);
            break;
        case 4:
            print2(&Q);
            break;
        case 5:
            saveDates(&S);
            break;
        case 6:
            readDates(&S);
            break;
        case 7:
            SortByParkingTime(c, carCount);
            printSortedParkingData(c, carCount);
            break;
        case 8:
            SortByCarNum(c, carCount);
            printSortedCarData(c, carCount);
            break;
        case 9:
            printf("Enter car number to find: ");
            scanf("%s", carNumToFind);
            if (isLegalLicensePlateNumber(carNumToFind))
            {
                if (FindCarByNum(&S, carNumToFind, &foundCar))
                {
                    printf("Car information: Car number %s, parking position %d, arrival time %d\n",
                        foundCar.num, foundCar.position, foundCar.arrivalTime);
                }
                else
                {
                    printf("Car with number %s not found\n", carNumToFind);
                }
            }
            else
            {
                printf("Invalid license plate number!\n");
            }
            break;
        case 10:
            int arrivalTime;
            printf("Enter the time to calculate parking duration (hour:minute): ");
            scanf("%d", &arrivalTime);
            CalculateParkingDuration(c, carCount, arrivalTime);
            break;
        default:
            printf("Invalid input, please enter 1, 2, 3, 4, ..., 10\n");
        }
    }
}


 

  • 22
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
问题描述: 汽车在停车场内按车到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车放在车场的最北端),若车场内已停满车辆,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可进入;当停车场内某辆车要离开时,在他之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在他离开时必须按他停留时间长短缴纳费用。以栈模拟停车场,以队列模拟车场外的便道,按照从中端读入的输入数据序列进行模拟管理。每一组输入数据进行包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号以及到达或离去的时刻。车离开时,输出汽车应缴纳的停车费。 狭长停车场只有一个门可容纳n辆车,当在有车进来时须停在外面的便道上,当停车场里的车开走时,它后面的车须首先退出为它让道,之后再按原来的次序进入停车场,此时停在便道上的第一两车可以进入停车场,从终端输入数据包括车是离去还是到达,车牌号码,到达或者离去的时间,输出相关信息并输出相关停车费用,停在便道上车在未进停车场就离去不收费。 实现提示:用栈和队列,停车场要以栈实现,还须另外设一栈用来为让道停放退出来的车,便道上的车以队列实现,栈以顺序存储,而队列以链式存储。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值