数据结构习题集之停车场管理

停车场管理

【问题描述】
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车放在车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等待,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在他离开停车场时必须按它停留的时间长短交纳费用。【这里假设停车一个小时20元计算,按比较贵的算,一般10到15元吧】}试为停车场编制按上述要求进行管理的模拟程序。

【基本要求】
以栈模拟停车场,以队列模拟车场外的便道,按照以终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应该缴纳的费用(在便道上停留的时间不收费)。

【测试数据】
设n = 2,输入数据为:(A,1,5),(A,2,10),(D,1,15),(A,3,20),(A,4,25),(A,5,30),(D,2,35),(D,4,40),(E,0,0)。其中A表示到达,D表示离开,E表示输入结束

#include <stdio.h>
#include <stdlib.h>
#include "Sqstack.h"
#include "LinkQueue.h"
#include "Car.h"
SqStack ParkLot;
SqStack Temp;
LinkQueue WaitLane;
void Start(){
    int N;
    scanf("%d",&N);
    InitStack(&ParkLot,N);
    InitStack(&Temp,N-1);
    InitQueue(&WaitLane);
    return;
}
void Arrive(Car A){
   if(StackFull(&ParkLot))
   {
         EnQueue(&WaitLane,A);
         printf("停车场已满,停在便道第%d等候车位\n",QueueLength(&WaitLane));
   }
   else
   {
          Push(&ParkLot,A);
          printf("在停车场自北向南第%d个车位\n",ParkLot.Top-ParkLot.Base);
   }
   return;
}
void Departure(Car A){
   Car B;
   while(1)
   {
       Pop(&ParkLot,&B);
       if(B.CarNum==A.CarNum)
           break;
       else
           Push(&Temp,B);
   }
   printf("车牌%d离开停车场,应缴停车费%d元\n",B.CarNum,20*(A.Time-B.Time));
   while(!StackEmpty(&Temp))
   {
      Pop(&Temp,&B);
      Push(&ParkLot,B);
   }
   if(!QueueEmpty(&WaitLane))
   {
   DeQueue(&WaitLane,&B);
   int waitTime=A.Time-B.Time;
   B.Time=A.Time;
   Push(&ParkLot,B);
   printf("便道上最前面的车%d等候%d时长后在%d时刻进入停车场\n",B.CarNum,waitTime,B.Time);
   }
   return;
}
void Run(){
    char Op;
    Car A;
    while(1)
    {
        scanf("%c%d%d",&Op,&A.CarNum,&A.Time);
        if(Op=='A')
             Arrive(A);
        if(Op=='D')
             Departure(A);
        if(Op=='E')
             break;
    }
    return;
}

int main()
{
    system("Color 0e");
    Start();
    Run();
    return 0;
}

车数据结构的共用头文件

#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED

typedef  struct Car{
    int  CarNum;
    int  Time;
}Car;

#endif // CAR_H_INCLUDED

用到的栈的头文件

#ifndef SQSTACK_H_INCLUDED
#define SQSTACK_H_INCLUDED
#define STACKINCREMENT 10    //存储空间分配增量
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#include "Car.h"
typedef   int Status;
typedef   Car  SElemType;
typedef struct {
      SElemType *Base;
      SElemType *Top;
      int StackSize;
}SqStack;

Status InitStack(SqStack *S,int N){
     S->Base=(SElemType *)malloc(N*sizeof(SElemType));
     if(!S->Base)
         exit(-1);
     S->Top=S->Base;
     S->StackSize=N;
     return OK;
}

Status  Push(SqStack *S,SElemType e){
      int Length=S->Top-S->Base;
      if(Length>=S->StackSize)
      {
         S->Base=(SElemType*)realloc(S->Base,(S->StackSize+STACKINCREMENT)*sizeof(SElemType));
         if(!S->Base)
             exit(-1);
         S->Top=S->Base+S->StackSize;
         S->StackSize=S->StackSize+STACKINCREMENT;
      }
      *S->Top++=e;
      return OK;
}

Status Pop(SqStack *S,SElemType *e){
     if(S->Top==S->Base)
            return ERROR;
     *e=*(--S->Top);
     return OK;
}

Status GetTop(SqStack *S,SElemType *e){
     if(S->Top==S->Base)
            return ERROR;
     *e=*(S->Top-1);
     return OK;
}

Status StackFull(SqStack *S){
     if(S->Top-S->Base==S->StackSize)
          return TRUE;
     else
         return  FALSE;
}

Status StackEmpty(SqStack *S){
     if(S->Base==S->Top)
          return TRUE;
     else
         return  FALSE;
}
#endif // SQSTACK_H_INCLUDED

队列头文件

#ifndef LINKQUEUE_H_INCLUDED
#define LINKQUEUE_H_INCLUDED
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#include "Car.h"
typedef   int Status;
typedef  Car QElemType;
typedef struct QNode{
     QElemType data;
     struct QNode *next;
}QNode,*QueuePtr;

typedef struct{
     QueuePtr front;
     QueuePtr rear;
}LinkQueue;

Status InitQueue(LinkQueue *Q){
     Q->front=(QueuePtr)malloc(sizeof(QNode));
     if(!Q->front)
          exit(-1);
     Q->rear=Q->front;
     Q->front->next=NULL;
     return OK;
}

Status EnQueue(LinkQueue *Q,QElemType e){
      QueuePtr P=(QueuePtr)malloc(sizeof(QNode));
      if(!P)
         exit(-1);
      P->data=e;
      P->next=NULL;
      Q->rear->next=P;
      Q->rear=P;
      return OK;
}

Status DeQueue(LinkQueue*Q,QElemType *e){
      if(Q->front==Q->rear)
            return ERROR;
      QueuePtr P=Q->front->next;
      *e=P->data;
       Q->front->next=P->next;
       if(Q->rear==P)
           Q->rear=Q->front;
       free(P);
       return OK;
}

Status QueueLength(LinkQueue *Q){
       QueuePtr P=Q->front->next;
       int num=1;
       while(P->next!=NULL)
       {
           num++;
           P=P->next;
       }
       return num;
}
Status QueueEmpty(LinkQueue *Q){
      if(Q->front==Q->rear)
          return TRUE;
      else
          return  FALSE;
}

#endif // LINKQUEUE_H_INCLUDED

在这里插入图片描述

posted on 2019-04-25 23:12  偷影子的人儿  阅读(99)  评论(0编辑  收藏  举报