停車場模擬管理問題

問題描述
設停車場是一個可停放n輛車的狹長通道,且只有一個大門可供汽車進出。汽車在停車場內按車輛到達時間的先後順序,依次由北向南排列,(大門在最南端,最先到達的第一輛車放在車場的最北端)。若停車場內已經停滿n輛車,那麼後來的車只能在門外的便道上等候。一旦有車開走,則排在便道上的第一輛車即可開入。當停車場內某車要離開時,在它之後進入的車輛必須先退出車場爲它讓路,待該車開出大門外,其他車輛再按原次序進入車場,每輛停放在車場的車在它離開時必須按它停留的時間長短繳納費用。試爲停車場編制按上述要求進行管理的模擬程序。
要求
根據各節點的信息,調用相應的函數或者語句,將節點入棧入隊,出棧或者出對。
代碼如下:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<stack>
#include<time.h>
#include <windows.h>
using namespace std;

int n = 0;
int money = 0;
time_t Time = 0;
struct Vehicle_Information//車輛信息
{
    char LicensePlateNumber[8];
    time_t ArriveTime;
    time_t LeaveTime;
    time_t Time;
    int Cost;
    Vehicle_Information()
        :ArriveTime(0)
        ,LeaveTime(0)
        ,Time(0)
        ,Cost(0)
    {
        *LicensePlateNumber = NULL;
    }
};

void delay()//延遲函數
{
    int n = 6;
    cout<<"*請等待";
    while(n)
    {
        Sleep(200);
        cout<<".";
        n--;
    }
    cout<<endl;
}
typedef struct Vehicle_Information Vehicle;

class Parking
{
public:

    bool IsParked()//判斷停車場是否已停滿車
    {
        if(Park.size() == n)
            return true;
        return false;
    }

    bool Find0(char* num)//在停車場中查找
    {
        int count = 0;
        while(!Park.empty())
        {
            if(strcmp(Park.top().LicensePlateNumber,num) == 0)
            {
                count = 1;
                break;
            }
            GiveWay.push(Park.top());
            Park.pop();
        }
        if(!GiveWay.empty())
        {
            while(!GiveWay.empty())
            {
                Park.push(GiveWay.top());
                GiveWay.pop();
            }
        }
        if(count == 1)
            return true;
        return false;
    }

    bool Find1(char *num)
    {
        queue<Vehicle> V;
        int count = 0;
        while(!Shortcut.empty())
        {
            if(strcmp(Shortcut.front().LicensePlateNumber,num) == 0)
            {
                count = 1;
                break;
            }
            V.push(Shortcut.front());
            Shortcut.pop();
        }
        if(!V.empty())
        {
            while(!V.empty())
            {
                Shortcut.push(V.front());
                V.pop();
            }
        }
        if(count == 1)
            return true;
        return false;
    }
    void Input()//輸入車輛信息,並將車輛停入停車場
    {
        cout<<"*請輸入所停車輛的信息:"<<endl;
        cout<<"*車牌號:";
        cin>>Information.LicensePlateNumber;
        delay();
        if(Find0(Information.LicensePlateNumber))
        {
            cout<<"*請注意!您輸入的號碼爲僞造號碼!"<<endl;
            return;
        }
        if(Find1(Information.LicensePlateNumber))
        {
            cout<<"*請注意!您輸入的號碼爲僞造號碼!"<<endl;
            return;
        }
        if(!IsParked())
        {
            Information.ArriveTime = time(&Time);
            Park.push(Information);
            Sleep(100);
            cout<<"*車輛已停好!"<<endl;
        }
        else
        {
            Shortcut.push(Information);
            cout<<"*停車場已滿!車輛已停入便道!"<<endl;
        }
    }
    void Output()//車輛離開停車場
    {
        if(Park.empty())
        {
            cout<<"*停車場已空!"<<endl;
        }
        cout<<"*請輸入要離開車輛的車牌號:";
        char num[8] = {0};
        cin>>num;
        delay();
        int n = Park.size();
        while(!Park.empty())
        {
            if(strcmp(Park.top().LicensePlateNumber,num) == 0)
            {
                Park.top().LeaveTime = time(&Time);
                Park.top().Time = (int)(Park.top().LeaveTime - Park.top().ArriveTime );
                Park.top().Cost =(int)( Park.top().Time * money);
                CurrentVehicleInformation(Park.top());
                Park.pop();
                Sleep(100);
                cout<<"*車輛已駛離!"<<endl;
                break;
            }
            GiveWay.push(Park.top());
            Park.pop();
        }
        if(GiveWay.size() == n)
        {
            cout<<"*無此車牌號!"<<endl;
            return;
        }
        if(!GiveWay.empty())
        {
            while(!GiveWay.empty())
            {
                Park.push(GiveWay.top());
                GiveWay.pop();
            }
        }
    }
    void CurrentVehicleInformation(Vehicle V)//當前所操作車輛信息
    {
        printf("車牌號\t駛入時間\t當前時間\t停車時間\t當前費用\n");
        printf("%s\t",V.LicensePlateNumber);
        TimeTransform(&(V.ArriveTime));
        TimeTransform(&(V.LeaveTime));
        printf("%ds\t\t",V.Time);
        printf("%d元\n",V.Cost);
    }
    void ShortcutIn()//將便道上的車停入停車場
    {
        if(Shortcut.empty())
        {
            cout<<"*便道未停靠車輛!"<<endl;
            return;
        }
        else
        {
            if(Park.size() == n)
            {
                cout<<"*停車位已滿!"<<endl;
                return;
            }
            else
            {
                Shortcut.front().ArriveTime = time(&Time);
                Park.push(Shortcut.front());
                Shortcut.pop();
                delay();
                cout<<"*車輛已停入車位!"<<endl;
            }
        }
    }
    void TimeMoney()//賦予車輛當前時間,用於計算當前費用
    {
        time_t t = time(&Time);
        while(!Park.empty())
        {
                Park.top().LeaveTime = t;
                Park.top().Time = (int)(Park.top().LeaveTime - Park.top().ArriveTime );
                Park.top().Cost =(int)( Park.top().Time * money);
                GiveWay.push(Park.top());
                Park.pop();
        }
        while(!GiveWay.empty())
        {
            Park.push(GiveWay.top());
            GiveWay.pop();
        }   
    }
    void ParkInformation()//停車場信息
    {
        cout<<"*當前停車場共有 "<<Park.size()<<" 輛車!"<<endl;
        cout<<"*剩餘車位爲:"<<n-Park.size()<<endl;
        cout<<"*便道目前有 "<<Shortcut.size()<<" 輛車在等待停車"<<endl;
        if(!Park.empty())
        {
            TimeMoney();
            cout<<"*停車場所停車輛信息:"<<endl;
            printf("車牌號\t駛入時間\t當前時間\t停車時間\t當前費用\n");    
            while(!Park.empty())
            {
                GiveWay.push(Park.top());
                Park.pop();
            }
            while(!GiveWay.empty())
            {
                printf("%s\t",GiveWay.top().LicensePlateNumber);
                TimeTransform(&(GiveWay.top().ArriveTime));
                TimeTransform(&(GiveWay.top().LeaveTime));
                printf("%ds\t\t",GiveWay.top().Time);
                printf("%d元\n",GiveWay.top().Cost);
                Park.push(GiveWay.top());
                GiveWay.pop();
            }

        }
    }
    void TimeTransform(time_t const *time_value)//時間轉換
    {
        struct tm* p;
        p = localtime(time_value);
        printf("%d:%d:%d    \t",p->tm_hour,p->tm_min,p->tm_sec);
    }
protected:
    Vehicle Information;
    stack<Vehicle> Park;//停車場
    stack<Vehicle> GiveWay;//讓路
    queue<Vehicle> Shortcut;//便道
};

void Menu()
{
    cout<<endl;
    cout<<"*************************************************************"<<endl;
    Sleep(200);
    cout<<"                           菜單                              "<<endl;
    Sleep(200);
    cout<<"*************************************************************"<<endl;
    Sleep(200);
    cout<<"************* 1.車輛入庫 2.車輛駛離 ******************"<<endl;
    Sleep(200);
    cout<<"************* 3.便道車輛駛入   4.停車場內所有車輛信息 ******"<<endl;
    Sleep(200);
    cout<<"************* 5.退出 ****************************************"<<endl;
}
int main()
{
    Parking P;
    cout<<"*************************************************************"<<endl;
    cout<<"                      停車場管理系統                           "<<endl;
    cout<<"*************************************************************"<<endl;
    cout<<"*請輸入停車場的車位數:"<<endl;
    cin>>n;
    cout<<"*請輸入停車場每秒的停車費用:"<<endl;
    cin>>money;
    if(n < 0)
    {
        n = 0;
    }
    while(n)
    {
        Menu();
        int count = 0;
        Sleep(500);
        cout<<"*請選擇當前要進行的操作:"<<endl;
        cin>>count;
        switch(count)
        {
            case 1:
                P.Input();
                break;
            case 2:
                P.Output();
                break;
            case 3:
                P.ShortcutIn();
                break;
            case 4:
                P.ParkInformation();
                break;
            case 5:
                cout<<"*已退出!"<<endl;
                exit(0);
                break;
            default:
                cout<<"*輸入有誤!"<<endl;
                break;

        }
    }
    if(n == 0)
    {
        cout<<"*您的停車場正在被強拆!";
        delay();
        cout<<"*停車場已拆除完畢!請輸入大於0的車位進行重建!"<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章