停车场管理
###【要求C或C++编程】
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若停车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。
【基本要求】
以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。
【测试数据】
设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’表示到达(arrival);‘D’表示离去(departure);‘E’表示输入结束(end)。
【实现提示】
需另设一个栈,临时停放为给要离去的汽车让路而从停车场退出来的汽车,也用顺序存储结构实现。输入数据按到达或离去的时刻有序。栈中每个元素表示一辆汽车,包含两个数据项:汽车的牌照号码和进入停车场的时刻。
[解决办法]
- C/C++ code
#include "stdio.h"#include "stdlib.h"#include "string.h"#define MAX 2 //车库容量 #define price 0.05 //每车每分钟费用typedef struct time //时间结点{ int hour; int min; }Time;typedef struct node //车辆信息结点 { char num[10]; Time reach; Time leave; }CarNode;typedef struct NODE //模拟车站{ CarNode *stack[MAX+1]; int top; }SeqStackCar;typedef struct car{ CarNode *data; struct car *next; }QueueNode; typedef struct Node //模拟通道{ QueueNode *head; QueueNode *rear; }LinkQueueCar;void InitStack(SeqStackCar *); //初始化栈int InitQueue(LinkQueueCar *); //初始化便道int Arrival(SeqStackCar *,LinkQueueCar *); //车辆到达void Leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *); //车辆离开 void List(SeqStackCar,LinkQueueCar); //显示存车信息int main(void) { SeqStackCar Enter,Temp; LinkQueueCar Wait; int ch; InitStack(&Enter); //初始化车站 InitStack(&Temp); //初始化让路的临时栈 InitQueue(&Wait); //初始化通道 while(1) { printf("\n1. the car arrive"); printf(" 2. the car leave"); printf(" 3. the schedule "); printf(" 4. exit\n"); while(1) { scanf("%d",&ch); if(ch>=1 && ch<=4) break; else printf("\nPlease choose: 1|2|3|4."); } switch(ch) { case 1:Arrival(&Enter,&Wait);break; //车辆到达 case 2:Leave(&Enter,&Temp,&Wait);break; //车辆离开 case 3:List(Enter,Wait);break; //列表打印信息 case 4:exit(0); //退出主程序 default: break; } } } void InitStack(SeqStackCar *s) //初始化栈{ int i; s->top=0; for(i=0;i<=MAX;i++) s->stack[s->top]=NULL; } int InitQueue(LinkQueueCar *Q) //初始化便道{ Q->head=(QueueNode *)malloc(sizeof(QueueNode)); if(Q->head!=NULL) { Q->head->next=NULL; Q->rear=Q->head; return(1); } else return(-1); } void PRINT(CarNode *p,int room) //打印出站车的信息{ int A1,A2,B1,B2; printf("\nplease input thedepart time:/**:**/"); scanf("%d:%d",&(p->leave.hour),&(p->leave.min)); printf("\nthe number of the car:"); puts(p->num); printf("\nthe time the car arrive: %d:%d",p->reach.hour,p->reach.min); printf("the depart time: %d:%d",p->leave.hour,p->leave.min); A1=p->reach.hour; A2=p->reach.min; B1=p->leave.hour; B2=p->leave.min; printf("\nthe fee: %2.1f元",((B1-A1)*60+(B2-A2))*price); free(p); } int Arrival(SeqStackCar *Enter,LinkQueueCar *W) //车辆到达{ CarNode *p; QueueNode *t; p=(CarNode *)malloc(sizeof(CarNode)); flushall(); printf("\ninput the number of the car(例:陕A1234):"); gets(p->num); if(Enter->top<MAX) //车场未满,车进车场 { Enter->top++; printf("\nthe place of the car.",Enter->top); printf("\nthe time thecar arrive:/**:**/"); scanf("%d:%d",&(p->reach.hour),&(p->reach.min)); Enter->stack[Enter->top]=p; return(1); } else //车场已满,车进便道 { printf("\n该车须在便道等待!"); t=(QueueNode *)malloc(sizeof(QueueNode)); t->data=p; t->next=NULL; W->rear->next=t; W->rear=t; return(1); } } void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W) //车辆离开{ int i, room; CarNode *p,*t; QueueNode *q; //判断车场内是否有车 if(Enter->top>0) //有车 { while(1) //输入离开车辆的信息 { printf("\n请输入车在车场的位置/1--%d/:",Enter->top); scanf("%d",&room); if(room>=1&&room<=Enter->top) break; } while(Enter->top>room) //车辆离开 { Temp->top++; Temp->stack[Temp->top]=Enter->stack[Enter->top]; Enter->stack[Enter->top]=NULL; Enter->top--; } p=Enter->stack[Enter->top]; Enter->stack[Enter->top]=NULL; Enter->top--; while(Temp->top>=1) { Enter->top++; Enter->stack[Enter->top]=Temp->stack[Temp->top]; Temp->stack[Temp->top]=NULL; Temp->top--; } PRINT(p,room); //判断通道上是否有车及车站是否已满 if((W->head!=W->rear)&&Enter->top<MAX) //便道的车辆进入车场 { q=W->head->next; t=q->data; Enter->top++; printf("\n便道的%s号车进入车场第%d位置.",t->num,Enter->top); printf("\n请输入现在的时间/**:**/:"); scanf("%d:%d",&(t->reach.hour),&(t->reach.min)); W->head->next=q->next; if(q==W->rear) W->rear=W->head; Enter->stack[Enter->top]=t; free(q); } else printf("\n便道里没有车.\n"); } else printf("\n车场里没有车."); //没车} void List1(SeqStackCar *S) //列表显示车场信息{ int i; if(S->top>0) //判断车站内是否有车 { printf("\n车场:"); printf("\n 位置 到达时间 车牌号\n"); for(i=1;i<=S->top;i++) { printf(" %d ",i); printf("%d:%d ",S->stack[i]->reach.hour,S->stack[i]->reach.min); puts(S->stack[i]->num); } } else printf("\n车场里没有车"); } void List2(LinkQueueCar *W) //列表显示便道信息{ QueueNode *p; p=W->head->next; if(W->head!=W->rear) //判断通道上是否有车 { printf("\n等待车辆的号码为:"); while(p!=NULL) { puts(p->data->num); p=p->next; } } else printf("\n便道里没有车."); } void List(SeqStackCar S,LinkQueueCar W) { int flag,tag; flag=1; while(flag) { printf("\n请选择 1|2|3:"); printf("\n1.车场\n2.便道\n3.返回\n"); while(1) { scanf("%d",&tag); if(tag>=1||tag<=3) break; else printf("\n请选择 1|2|3:"); } switch(tag) { case 1:List1(&S);break; //列表显示车场信息 case 2:List2(&W);break; //列表显示便道信息 case 3:flag=0;break; default: break; } } }