停车场管理
设停车场是一个可停放n辆车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若停车厂内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;停车场内某辆车要离开时,在它之后进入的车连必须先退出车厂为它让路,待该车辆开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车时必须按它停留的时间长短缴纳费用。编写按上述要求进行管理的模拟程序。
实验提示:
可以将停车场定义成一个顺序栈s0,便道定义成一个链队列q,而停车场中的某辆车要离开,则在它后面进停车场的车必须让道,让其离开,所以必须有一个临时的顺序栈s1,存放让道的车辆。
typedef struct //定义栈的数据元素类型
{
int num; //车牌号
int time; //到达时间
}elemtype;
typedef struct //定义栈
{elemtype *base; //栈底指针
elemtype *top; //栈顶指针
int stacksize;
}stack;
typedef struct qnode //定义队列的数据结点类型
{int num;
int time;
struct qnode *next;
}qnode,*qptr;
typedef struct //定义链队列
{qptr front; //队头指针
qptr rear; //队尾指针
}queue;
#include<malloc.h>
#include<stdio.h>
#include<time.h>
#define GARAGE 2
#define PRICE 1
typedef struct Snode //车辆信息
{
int order;
time_t time;
}Snode;
typedef struct Stack //停车场和临时停车栈
{struct Snode *base;
int top;
int stacksize;
}Stack;
Stack* Create_Stack()
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
if(!p){printf("栈内存分配失败\n");return 0;}
p->base=(Snode *)malloc(GARAGE*sizeof(Snode));
if(!p->base){printf("栈数据域内存分配失败\n");return 0;}
p->top=0;
p->stacksize=GARAGE;
return p;
}
void Push(Stack *p,int order,time_t tim)
{
if(p->top>=p->stacksize){printf("栈溢出重新分配内存失败\n");return;}
Snode *sn;
sn=p->base;
(sn+p->top)->order=order;
(sn+p->top)->time=tim;
p->top++;
}
Snode* Pop(Stack *p) //车库出栈
{
time_t end;
end = time(NULL);
if(p->top==0){printf("车库此时为空\n");return NULL;}
p->top--;
Snode *sn,*re;
sn=p->base;
re=(Snode*)malloc(sizeof(Snode)); //re是作为要返回的变量
re->order=((sn+p->top)->order);
re->time=((sn+p->top)->time);
(sn+p->top)->order=NULL;
(sn+p->top)->time=NULL;
return re;
}
typedef struct Qnode //车辆信息
{
int order;
struct Qnode *next;
}Qnode;
typedef struct LinkQueue //等待车辆队列
{
struct Qnode *front;
struct Qnode *rear;
}LinkQueue;
LinkQueue* Create_LinkQueue()
{
LinkQueue *linkQueue = (LinkQueue *)malloc(sizeof(LinkQueue));
if(!linkQueue){printf("申请内存失败\n");return 0;}
Qnode *qnode = (Qnode *)malloc(sizeof(Qnode));
if(!qnode){printf("申请内存失败\n");return 0;}
linkQueue->front=qnode;linkQueue->rear=qnode;
qnode->next=NULL;
return linkQueue;
}
void EnQueue(LinkQueue *linkQueue,int order)
{
Qnode *qnode = (Qnode *)malloc(sizeof(Qnode));
if(!qnode) {printf("申请内存失败\n");return;}
qnode->order=order;
qnode->next=NULL;
linkQueue->rear->next=qnode;
linkQueue->rear=linkQueue->rear->next;
}
Qnode* DeQueue(LinkQueue *linkQueue)
{
if(linkQueue->front==linkQueue->rear){printf("链队列为空\n");return NULL;}
Qnode *qnode_del=linkQueue->front->next;
Qnode *re=(Qnode *)malloc(sizeof(Qnode));
re->order= qnode_del->order;
linkQueue->front->next=qnode_del->next;
if(linkQueue->rear==qnode_del)linkQueue->rear=linkQueue->front;
free(qnode_del);
return re;
}
void Show_S(Stack *ParkingLot)
{
Snode *s=ParkingLot->base;
int i=0;
while(i<ParkingLot->top)
{
printf("%d ",s->order);
s++;
i++;
}
}
void Show_Q(LinkQueue *WaitingLot)
{
Qnode *f,*r;
f= WaitingLot->front->next;
r = WaitingLot->rear;
if (r == NULL)
return;
else
while (f != NULL)
{
printf("%d ",f->order);
f = f->next;
}
}
void main()
{
time_t start,end;
int order;
Stack *ParkingLot=Create_Stack(); //停车库
Stack *TempLot=Create_Stack(); //临时停车库
LinkQueue *WaitingLot=Create_LinkQueue(); //等待车辆队列
int choice;
printf("每秒钟的停车费为1元\n");
printf("输入1是汽车开进车库\n输入2是汽车开出车库\n输入3是让等待的车辆进入车库\n输入4是检查各车道情况\n***************正在等待用户输入...\n");
while(scanf("%d",&choice)!=EOF)
{
if(choice==1)
{
printf("请输入要开入的汽车的序号");
scanf("%d",&order);
if(ParkingLot->top==ParkingLot->stacksize) //先判断车库有没有满
{
EnQueue(WaitingLot,order);
printf("车库已满,序号为%d的汽车进入便道等待\n",order);
}
else
{
start = time(NULL);
Push(ParkingLot,order,start); //没满的话就把这辆车放进去
printf("%d号车进入车库,入库时间为",order);printf(ctime(&start));
}
}
if(choice==2)
{
printf("请输入要开出的汽车的序号");
scanf("%d",&order);
Snode *sc;
for(int max=ParkingLot->top;order!=(ParkingLot->base+max-1)->order;max--)
{
sc=Pop(ParkingLot);printf("%d号车为了给%d号车让路进入了临时停车道\n",sc->order,order);
Push(TempLot,sc->order,sc->time);
}
sc=Pop(ParkingLot);
end = time(NULL);
printf("%d号车应缴纳的钱数为%f元,出库时间为",sc->order,difftime(end,sc->time)*PRICE);
printf(ctime(&end));putchar('\n');
}
if(choice==3&&TempLot->top!=0)
{
Snode *ic;int i=TempLot->top;
while(TempLot->top!=0)
{
ic=Pop(TempLot);
Push(ParkingLot,ic->order,ic->time);
printf("%d ",ic->order);
}
printf("返回车库\n");
Qnode *wc=DeQueue(WaitingLot);
start = time(NULL);
Push(ParkingLot,wc->order,start);
printf("%d号车进入车库,入库时间为",wc->order);printf(ctime(&start));
}
if(choice==3&&TempLot->top==0&&ParkingLot->top<ParkingLot->stacksize)
{
Qnode *wc=DeQueue(WaitingLot);
start = time(NULL);
Push(ParkingLot,wc->order,start);
printf("%d号车进入车库,入库时间为",wc->order);printf(ctime(&start));
}
if(choice==4)
{
printf("车库情况:");Show_S(ParkingLot);
putchar('\n');
printf("等待便道情况:");Show_Q(WaitingLot);
putchar('\n');
printf("临时停车道情况:");Show_S(TempLot);
putchar('\n');
}
printf("***************正在等待用户输入...\n");
}
}