C++实现停车场管理系统

有一个可以停放n 辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的早晚依次从停车场最里面向大门口处停放(最先到达的第一辆车放在停车场的最里面)。如果停车场已放满n 辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在他之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车辆在依原来的次序进场。每辆车在离开停车场时,都应依据它在停车场内停留的时间长短交费。如果停留在便道上的车未进停车场就要离去,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆的次序。
1. 以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。
2. 每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。
3. 对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费,功能可自己添加)。
1.头文件

#define _AFXDLL
#include <afx.h>
#include<iostream>
#include<string>
#define MAX 3
using namespace std;

struct Time      //时间结构体,用于车辆进场,出场时间记录
{
    int year;
    int month;
    int day;
    int hour;
    int min;
};

struct CarNode     //车辆结构体
{
    char num[10];   //车牌号
    Time reach;      //到达时间
    Time leave;      //离开时间
};
struct StackCar   //停车场栈
{
    int top;
    CarNode *CarStack[MAX + 1];
};
struct QCarNode   //链队中的汽车结点结构体
{
    CarNode *data;    //汽车信息
    struct QCarNode *next;
};
struct LinkQueueCar  //便道
{
    QCarNode *head;   //对头指针
    QCarNode *rear;    //队尾指针
};
class CarSystem{
    StackCar *CarBase, *QuitTemp;   //停车场栈,车辆出停车场时的临时栈
    LinkQueueCar *WaitQueue;    //便道
public:
    CarSystem();      //构造函数
    ~CarSystem();           //析构函数
    int Arrival();          //车辆进站
    void Leave();            //车辆出站
    void ShowLeaveInfo(CarNode *p, int item);   //离开车辆缴费相关信息
    void ShowInfo();          //显示车位情况
    void Carstack();   //停车场中车位状况
    void Carqueue();    //便道中车位状况
    void QueueCarLeave(char a[]);   //便道中车辆离开
};
void ShowMenu()
{
    cout << "********************************************" << endl;
    cout << "****** 停 车 场 管 理 系 统 ******" << endl;
    cout << "****** 0.安全退出系统 ******" << endl;
    cout << "****** 1.汽车停车登记 ******" << endl;
    cout << "****** 2.汽车离开登记 ******" << endl;
    cout << "****** 3.便道汽车离开 ******" << endl;
    cout << "****** 4.车位信息查看 ******" << endl;
    cout << "\n\t\n\t\t请选择:";
}

CarSystem::CarSystem()         //构造函数
{
    CarBase = new StackCar;            //停车场栈
    CarBase->top = 0;
    CarBase->CarStack[CarBase->top] = NULL;

    QuitTemp = new StackCar;         //车辆临时栈
    QuitTemp->top = 0;
    QuitTemp->CarStack[QuitTemp->top] = NULL;

    WaitQueue = new LinkQueueCar;       //便道
    WaitQueue->head = new QCarNode;
    if (WaitQueue->head != NULL)
    {
        WaitQueue->head->next = NULL;
        WaitQueue->rear = WaitQueue->head;
    }
}

CarSystem::~CarSystem()
{
    QCarNode *ptemp;
    while (CarBase->top != 0)
        CarBase->CarStack[--CarBase->top] = NULL;
    while (WaitQueue->head != WaitQueue->rear)
    {
        ptemp = WaitQueue->head;
        WaitQueue->head = WaitQueue->head->next;
        delete ptemp;
    }
}

int CarSystem::Arrival()    //车辆进站登记
{
    CarNode *p;       //汽车临时指针
    QCarNode *t;      //队列中汽车临时指针
    CTime start_time = CTime::GetCurrentTime();   //获取系统当前时间作为车辆进站时间
    p = new CarNode;    
    cout << "登记车牌号:";
    cin >> p->num;
    if (CarBase->top < MAX)       //有空车位
    {
        CarBase->top++;
        cout << "车辆在停车场第 " << CarBase->top  << " 号车位" ;
        p->reach.year = start_time.GetYear();
        p->reach.month = start_time.GetMonth();
        p->reach.day = start_time.GetDay();
        p->reach.hour = start_time.GetHour();
        p->reach.min = start_time.GetMinute();
        CarBase->CarStack[CarBase->top] = p;
        return 1;
    }
    else           //没有空车位
    {
        cout << "停车场已满,请在便道等待...";
        t = new QCarNode;
        t->data = p;
        t->next = NULL;
        WaitQueue->rear->next = t;
        WaitQueue->rear = t;
        return 1;
    }
}

void CarSystem::Leave()     //车辆出站登记
{
    int item;
    CarNode *p, *t;   
    QCarNode *q;
    if (CarBase->top > 0)      //车站有车时
    {
        while (1)
        {
            cout << "请输入车在车场的位置:";
            cin >> item;
            if (item >= 1 && item <= CarBase->top) break;    //判断输入位置
        }
        while (CarBase->top > item)  //位置不在栈顶的汽车出站
        {
            QuitTemp->top++;
            QuitTemp->CarStack[QuitTemp->top] = CarBase->CarStack[CarBase->top];
            CarBase->CarStack[CarBase->top] = NULL;
            CarBase->top--;
        }

        p = CarBase->CarStack[CarBase->top];
        CarBase->CarStack[CarBase->top] = NULL;
        CarBase->top--;

        while (QuitTemp->top >= 1)  //当暂时存储汽车的栈结构中有汽车时汽车重新进站
        {
            CarBase->top++;
            CarBase->CarStack[CarBase->top] = QuitTemp->CarStack[QuitTemp->top];
            QuitTemp->CarStack[QuitTemp->top] = NULL;
            QuitTemp->top--;
        }

        ShowLeaveInfo(p, item);

        if ((WaitQueue->head != WaitQueue->rear) && CarBase->top<MAX)    //停车场有车位且便道有车时,便道中车辆进入停车场
        {
            CTime start_time = CTime::GetCurrentTime();   //获取系统当前时间作为车辆进站时间
            q = WaitQueue->head->next;
            t = q->data;
            CarBase->top++;
            cout << "\n便道的" << t->num << "号车进入车场第" << CarBase->top << "号车位.";
            t->reach.year = start_time.GetYear();
            t->reach.month = start_time.GetMonth();
            t->reach.day = start_time.GetDay();
            t->reach.hour = start_time.GetHour();
            t->reach.min = start_time.GetMinute();

            WaitQueue->head->next = q->next;
            if (q == WaitQueue->rear)
                WaitQueue->rear = WaitQueue->head;
            CarBase->CarStack[CarBase->top] = t;
            free(q);
        }
        else cout << "便道里没有车..." << endl;
    }
    else cout << "挺车场里没有车..." << endl;
}

void CarSystem::ShowLeaveInfo(CarNode *p, int room)     //汽车离站时缴费显示
{
    CTime end_time = CTime::GetCurrentTime();
    p->leave.year = end_time.GetYear();
    p->leave.month = end_time.GetMonth();
    p->leave.day = end_time.GetDay();
    p->leave.hour = end_time.GetHour();
    p->leave.min = end_time.GetMinute();
    printf("\n离开车辆的车牌号为:");
    cout << p->num;
    cout << "\n到达时间为: " << p->reach.year << " 年 " << p->reach.month << " 月 " << p->reach.day << " 日 " << p->reach.hour << " 时 " << p->reach.min << " 分";
    cout << "\n离开时间为: " << p->leave.year << " 年 " << p->leave.month << " 月 " << p->leave.day << " 日 " << p->leave.hour << " 时 " << p->leave.min << " 分";
    cout << "\n应交费用为: " << ((p->leave.hour - p->reach.hour) * 60 + (p->leave.min - p->reach.min)) * 100 << " 元";
    free(p);
}

void CarSystem::QueueCarLeave(char a[])    //便道中的车直接离开
{
    QCarNode *p,*q;
    p = WaitQueue->head->next;
    q = WaitQueue->head;
    if (WaitQueue->head != WaitQueue->rear)
    {
        while (strcmp(p->data->num, a) && p != NULL)
        {
            q = p;
            p = p->next;
        }
        q->next = p->next;
        free(p);
    }
    else
    {
        cout << "便道中无车!" << endl;
    }
}

void CarSystem::ShowInfo()    //查询车位状态
{
    int choice = 0;
    cout << "请输入查看列表:" << endl;
    cout << "--- 1.停车场车位状况 ---" << endl;
    cout << "--- 2.便道停车位状况 ---" << endl;
    cout << "--- 3.返回主菜单 ---" << endl;
    while (choice!=3)
    {
        while (1)
        {
            cin >> choice;
            if (choice >= 1 || choice <= 3) break;
            else cout << "请检查输入!" << endl;
        }
        switch (choice)
        {
        case 1:
            Carstack();
            break;
        case 2:
            Carqueue();
            break;
        case 3:
            return;
            break;
        default:
            break;
        }
    }
}

void CarSystem::Carstack()     //车场车位显示
{
    int i;
    if (CarBase->top>0)
    {
        cout << "停车场车位状态:";
        cout << "\n车位号\t" << " 到达时间 \t" << "车牌号\n";
        for (i = 1; i <= CarBase->top; i++)
        {
            cout << " " << i << "\t ";
            cout << CarBase->CarStack[i]->reach.year << "年" << CarBase->CarStack[i]->reach.month << "月" << CarBase->CarStack[i]->reach.day
                << "日" << CarBase->CarStack[i]->reach.hour << "时" << CarBase->CarStack[i]->reach.min << "分";
            cout << "\t " << CarBase->CarStack[i]->num << endl;
        }
    }
    else cout << "\n车场里没有车";
}

void CarSystem::Carqueue()     //便道车位显示
{
    QCarNode *p;
    p = WaitQueue->head->next;
    cout << "便道车位状况:" << endl;
    if (WaitQueue->head != WaitQueue->rear)
    {
        cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
        while (p != NULL)
        {
            cout << p->data->num << "\t";
            p = p->next;
        }
        cout << endl;
        cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
    }
    else
    {
        cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
        cout << "便道中无车!" << endl;
        cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
    }
}

2.源文件

#include "CarSystem.h"
#include<iostream>

using namespace std;
int main()
{
    CarSystem CAR;         //停车场类
    int x = 100;
    char a[10];
    while (x != 0)
    {
        system("pause");
        system("cls");      //清屏
        ShowMenu();
        cin >> x;
        switch (x)
        {
        case 0:
            exit(0);
            break;
        case 1:
            CAR.Arrival();
            cout << endl << "-----------------------------------------" << endl;
            cout << "停车场更新..." << endl;
            CAR.Carstack();
            cout << "-----------------------------------------" << endl;
            break;
        case 2:

            CAR.Leave();
            cout << endl << "-----------------------------------------" << endl;
            cout << "停车场更新..." << endl;
            CAR.Carstack();
            cout << "-----------------------------------------" << endl;
            break;
        case 3:

            cout << "请输入要离开的车牌号:" ;
            cin >> a;
            CAR.QueueCarLeave(a);
            cout << "便道更新..." << endl;
            CAR.Carqueue();
            break;
        case 4:
            CAR.ShowInfo();
            break;
        }
    }
    system("pause");
    return 0; 
}
    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/csdnzyq/article/details/50541101
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞