【第七週】項目6-停車場模擬

設停車場是一個可停放n輛汽車的狹長死衚衕,南邊封口,汽車只能從北邊進出(這樣的停車場世間少有)。汽車在停車場內按車輛到達時間的先後順序,最先到達的第一輛車停放在車場的最南端,依次向北排開。若車場內已停滿n輛汽車,則後來的汽車只能在門外的候車場上等候,一旦有車開走,則排在候車場上的第一輛車即可開入。當停車場內某輛車要離開時,在它之後進入的車輛必須先退出車場爲它讓路(假定停車場內設有供車輛進出的便道,所有的司機也必須在車內隨時待命),待該輛車開出外,其他車輛再按原次序進入車場。每輛停放在車場的車在它離開停車場時,要按停留的時間長短交納費用。試爲停車場編制按上述要求進行管理的模擬程序。

  這裏寫圖片描述

提示: 
  以棧模擬停車場,以隊列模擬車場外的候車場,有車離開時,供車輛進出的便道也應該用棧表示。按照從鍵盤讀入的輸入數據序列進行模擬管理。汽車到達和離開時,每一組輸入數據包括汽車牌照號碼(設爲整數)以及到達或離去的時刻(爲簡單化,也設爲整數,如1,代表停車場開始營業的第1小時)。對每一組輸入數據進行操作後的輸出信息爲:若是車輛到達,則輸出汽車在停車場內或修車場上的停車位置;若是車輛離去,則輸出汽車在停車場內停留的時間和應交納的費用(在候車場上停留的時間不收費)。棧以順序結構實現,隊列以鏈表結構實現。

#include <stdio.h>  
#include <malloc.h>  
#define N 10                    /*停車場內最多的停車數*/  
#define M 10                    /*候車場內最多的停車數*/  
#define Price 2             /*每單位時間停車費用*/  
typedef struct  
{  
    int CarNo[N];           /*車牌號*/  
    int CarTime[N];         /*進場時間*/  
    int top;                /*棧指針*/  
} SqStack;                  /*定義順序棧類型,用於描述停車場*/  
  
typedef struct  
{  
    int CarNo[M];           /*車牌號*/  
    int front,rear;         /*隊首和隊尾指針*/  
} SqQueue;                  /*定義循環隊類型,用於描述候車場*/  
  
/*以下爲順序棧的基本運算算法*/  
void InitStack(SqStack *&s)  
{  
    s=(SqStack *)malloc(sizeof(SqStack));  
    s->top=-1;  
}  
int StackEmpty(SqStack *s)  
{  
    return(s->top==-1);  
}  
int StackFull(SqStack *s)  
{  
    return(s->top==N-1);  
}  
int Push(SqStack *&s,int e1,int e2)  
{  
    if (s->top==N-1)  
        return 0;  
    s->top++;  
    s->CarNo[s->top]=e1;  
    s->CarTime[s->top]=e2;  
    return 1;  
}  
int Pop(SqStack *&s,int &e1,int &e2)  
{  
    if (s->top==-1)  
        return 0;  
    e1=s->CarNo[s->top];  
    e2=s->CarTime[s->top];  
    s->top--;  
    return 1;  
}  
void DispStack(SqStack *s)  
{  
    int i;  
    for (i=s->top; i>=0; i--)  
        printf("%d ",s->CarNo[i]);  
    printf("\n");  
}  
  
/*以下爲循環隊列的基本運算算法*/  
void InitQueue(SqQueue *&q)  
{  
    q=(SqQueue *)malloc (sizeof(SqQueue));  
    q->front=q->rear=0;  
}  
int QueueEmpty(SqQueue *q)  
{  
    return(q->front==q->rear);  
}  
int QueueFull(SqQueue *q)       /*判斷隊滿*/  
{  
    return ((q->rear+1)%M==q->front);  
}  
int enQueue(SqQueue *&q,int e)      /*進隊*/  
{  
    if ((q->rear+1)%M==q->front)    /*隊滿*/  
        return 0;  
    q->rear=(q->rear+1)%M;  
    q->CarNo[q->rear]=e;  
    return 1;  
}  
int deQueue(SqQueue *&q,int &e)     /*出隊*/  
{  
    if (q->front==q->rear)          /*隊空的情況*/  
        return 0;  
    q->front=(q->front+1)%M;  
    e=q->CarNo[q->front];  
    return 1;  
}  
void DispQueue(SqQueue *q)      /*輸出隊中元素*/  
{  
    int i;  
    i=(q->front+1)%M;  
    printf("%d ",q->CarNo[i]);  
    while ((q->rear-i+M)%M>0)  
    {  
        i=(i+1)%M;  
        printf("%d ",q->CarNo[i]);  
    }  
    printf("\n");  
}  
  
//main函數用於模擬停車場的工作  
int main()  
{  
    int comm;  
    int no,e1,time,e2;  
    int i,j,t;  
    SqStack *St,*St1;  //St是停車場,St1是在有車離開時,記錄爲該車移開位置的車輛  
    SqQueue *Qu;   //Qu是候車場  
    InitStack(St);  
    InitStack(St1);  
    InitQueue(Qu);  
    do  
    {  
        printf("輸入指令(1:到達 2:離開 3:顯示停車場 4:顯示候車場 0:退出):");  
        scanf("%d",&comm);  
        switch(comm)  
        {  
        case 1:     /*汽車到達*/  
            printf("輸入車號和時間(設車號和時間均爲整數): ");  
            scanf("%d%d",&no,&time);  
            if (!StackFull(St))         /*停車場不滿*/  
            {  
                Push(St,no,time);  
                printf("  >>停車場位置:%d\n",St->top+1);  
            }  
            else                        /*停車場滿*/  
            {  
                if (!QueueFull(Qu))     /*候車場不滿*/  
                {  
                    enQueue(Qu,no);  
                    printf("  >>候車場位置:%d\n",Qu->rear);  
                }  
                else  
                    printf("  >>候車場已滿,不能停車\n");  
            }  
            break;  
        case 2:     /*汽車離開*/  
            printf("輸入車號和時間(設車號和時間均爲整數): ");  
            scanf("%d%d",&no,&time);  
            for (i=0; i<=St->top && St->CarNo[i]!=no; i++);  //在棧中找  
            if (i>St->top)  
                printf("  >>未找到該編號的汽車\n");  
            else  
            {  
                t = St->top - i;  //需要出棧的車輛數目  
                for (j=0; j<t; j++)  //for (j=i; j<=St->top; j++)1樓評論講的原錯誤寫法  
                {  
                    Pop(St,e1,e2);  
                    Push(St1,e1,e2);        /*倒車到臨時棧St1中*/  
                }  
                Pop(St,e1,e2);              /*該汽車離開*/  
                printf("  >>%d汽車停車費用:%d\n",no,(time-e2)*Price);  
                while (!StackEmpty(St1))    /*將臨時棧St1重新回到St中*/  
                {  
                    Pop(St1,e1,e2);  
                    Push(St,e1,e2);  
                }  
                if (!QueueEmpty(Qu))        /*隊不空時,將隊頭進棧St*/  
                {  
                    deQueue(Qu,e1);  
                    Push(St,e1,time);       /*以當前時間開始計費*/  
                }  
            }  
            break;  
        case 3:     /*顯示停車場情況*/  
            if (!StackEmpty(St))  
            {  
                printf("  >>停車場中的車輛:"); /*輸出停車場中的車輛*/  
                DispStack(St);  
            }  
            else  
                printf("  >>停車場中無車輛\n");  
            break;  
        case 4:     /*顯示候車場情況*/  
            if (!QueueEmpty(Qu))  
            {  
                printf("  >>候車場中的車輛:"); /*輸出候車場中的車輛*/  
                DispQueue(Qu);  
            }  
            else  
                printf("  >>候車場中無車輛\n");  
            break;  
        case 0:     /*結束*/  
            if (!StackEmpty(St))  
            {  
                printf("  >>停車場中的車輛:"); /*輸出停車場中的車輛*/  
                DispStack(St);  
            }  
            if (!QueueEmpty(Qu))  
            {  
                printf("  >>候車場中的車輛:"); /*輸出候車場中的車輛*/  
                DispQueue(Qu);  
            }  
            break;  
        default:    /*其他情況*/  
            printf("  >>輸入的命令錯誤\n");  
            break;  
        }  
    }  
    while(comm!=0);  
    return 0;  
}  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章