您的位置:首页 > 理论基础 > 数据结构算法

2015年大二上-数据结构-队列(6)- 停车场模拟

2015-11-22 19:19 489 查看
设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出(这样的停车场世间少有)。汽车在停车场内按车辆到达时间的先后顺序,最先到达的第一辆车停放在车场的最南端,依次向北排开。若车场内已停满n辆汽车,则后来的汽车只能在门外的候车场上等候,一旦有车开走,则排在候车场上的第一辆车即可开入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路(假定停车场内设有供车辆进出的便道,所有的司机也必须在车内随时待命),待该辆车开出大门外,其他车辆再按原次序进入车场。每辆停放在车场的车在它离开停车场时,要按停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。

  


提示:以栈模拟停车场,以队列模拟车场外的候车场,有车离开时,供车辆进出的便道也应该用栈表示。按照从键盘读入的输入数据序列进行模拟管理。汽车到达和离开时,每一组输入数据包括汽车牌照号码(设为整数)以及到达或离去的时刻(为简单化,也设为整数,如1,代表停车场开始营业的第1小时)。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或修车场上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在候车场上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。

头文件

SqStack.h

/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年11月22日
*版本号:v1.0
*
*问题描述:头文件
*输入描述:无。
*输出描述:无。
*/

#ifndef SQSTACK_H_INCLUDED
#define SQSTACK_H_INCLUDED
#define MaxSize 5
typedef int ElemType;
typedef struct
{
int CarNo[MaxSize];           /*车牌号*/
int CarTime[MaxSize];         /*进场时间*/
int top;                /*栈指针*/
}SqStack;                   /*定义顺序栈类型,用于描述停车场*/
void InitStack(SqStack *&s);    //初始化栈
void DestroyStack(SqStack *&s);  //销毁栈
bool StackEmpty(SqStack *s);     //栈是否为空
int StackLength(SqStack *s);  //返回栈中元素个数——栈长度
bool Push(SqStack *&s,ElemType n,ElemType t); //入栈
bool Pop(SqStack *&s,ElemType &n,ElemType &t); //出栈
bool GetTop(SqStack *s,ElemType &n,ElemType &t); //取栈顶数据元素
void DispStack(SqStack *s);  //输出栈

#endif // SQSTACK_H_INCLUDED
SqQueue.h

/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年11月22日
*版本号:v1.0
*
*问题描述:头文件。
*输入描述:无。
*输出描述:无。
*/
#ifndef SQQUEUE_H_INCLUDED
#define SQQUEUE_H_INCLUDED
#define MaxSize 5
typedef int ElemType;
typedef struct
{
int CarNo[MaxSize];           /*车牌号*/
int front,rear;         /*队首和队尾指针*/
} SqQueue;                  /*定义循环队类型,用于描述候车场*/

void InitQueue(SqQueue *&q);  //初始化顺序环形队列
void DestroyQueue(SqQueue *&q); //销毁顺序环形队列
bool QueueEmpty(SqQueue *q);  //判断顺序环形队列是否为空
int QueueLength(SqQueue *q);   //返回队列中元素个数,也称队列长度
bool EnQueue(SqQueue *&q,ElemType e);   //进队
bool DeQueue(SqQueue *&q,ElemType &e);  //出队
void DispQueue(SqQueue *q);

#endif // SQQUEUE_H_INCLUDED


源文件

SqStack.cpp

/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年11月22日
*版本号:v1.0
*
*问题描述:源文件
*输入描述:无。
*输出描述:无。
*/
#include <stdio.h>
#include <malloc.h>
#include "sqstack.h"

void InitStack(SqStack *&s)
{
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
void DestroyStack(SqStack *&s)
{
free(s);
}
bool StackEmpty(SqStack *s)
{
return (s->top==-1);
}
int StackLength(SqStack *s)
{
return (s->top+1);
}
bool Push(SqStack *&s,ElemType n,ElemType t)
{
if(s->top==MaxSize-1)
return false;
++s->top;
s->CarNo[s->top]=n;
s->CarTime[s->top]=t;
return true;
}
bool Pop(SqStack *&s,ElemType &n,ElemType &t)
{
if(s->top==-1)
return false;
n=s->CarNo[s->top];
t=s->CarTime[s->top];
--s->top;
return true;
}
bool GetTop(SqStack *s,ElemType &n,ElemType &t)
{
if(s->top==-1)
return false;
n=s->CarNo[s->top];
t=s->CarTime[s->top];
return true;
}
void DispStack(SqStack *s)
{
int i=s->top;
while(i>=0)
{
printf("%d ",s->CarNo[i]);
--i;
}
printf("\n");
}


SqQueue.cpp

/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年11月22日
*版本号:v1.0
*
*问题描述:源文件。
*输入描述:无。
*输出描述:无。
*/
#include <stdio.h>
#include <malloc.h>
#include "SqQueue.h"

void InitQueue(SqQueue *&q)
{
q=(SqQueue *)malloc(sizeof(SqQueue));
q->rear=q->front=0;
}
void DestroyQueue(SqQueue *&q)
{
free(q);
}
bool QueueEmpty(SqQueue *q)
{
return (q->rear==q->front);
}
int QueueLength(SqQueue *q)
{
return (q->rear-q->front+MaxSize)%MaxSize;
}
bool EnQueue(SqQueue *&q,ElemType e)
{
if((q->rear+1)%MaxSize==q->front)
return false;
q->rear=(q->rear+1)%MaxSize;
q->CarNo[q->rear]=e;
return true;
}
bool DeQueue(SqQueue *&q,ElemType &e)
{
if(QueueEmpty(q))
return false;
q->front=(q->front+1)%MaxSize;
e=q->CarNo[q->front];
return true;
}
void DispQueue(SqQueue *q)
{
while(q->rear-q->front!=0)
{
++q->front;
printf("%d ",q->CarNo[q->front]);
}
}


主函数

main.cpp

/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年11月22日
*版本号:v1.0
*
*问题描述:停车场模拟。
*输入描述:输入需要的功能。
*输出描述:输出对应的功能。
*/
#include <stdio.h>
#include "SqQueue.h"
#include "SqStack.h"
#define Price 2             /*每单位时间停车费用*/

int main()
{
SqStack *st1,*st2;
SqQueue *sq;
InitStack(st1);
InitStack(st2);
InitQueue(sq);
int number,car_number,time,time_now,n,t,i,j;
do
{
printf("输入指令(1:到达 2:离开 3:显示停车场 4:显示候车场 0:退出):");
scanf("%d",&number);
switch(number)
{
case 1:
printf("输入车号和时间(设车号和时间均为整数): ");
scanf("%d%d",&car_number,&time);
if(Push(st1,car_number,time))
{
printf("  >>停车场位置:%d\n",st1->top+1);
}
else
{
if(EnQueue(sq,car_number))
printf("  >>侯车场位置:%d\n",sq->rear);
else printf("  >>候车场已满,不能停车\n");
}
break;
case 2:
printf("输入车号和当前时间(设车号和时间均为整数): ");
scanf("%d%d",&car_number,&time_now);
for(i=0; i<=st1->top&&st1->CarNo[i]!=car_number; ++i);
if(i>st1->top)
printf("  >>未找到该编号的汽车!\n");
else
{
{
for(j=st1->top; j>0; --j)
{
Pop(st1,n,t);
Push(st2,n,t);
}
}
Pop(st1,n,t);
printf("  >>%d汽车停车费用为:%d\n",n,(time_now-t)*Price);
while(Pop(st2,n,t))
Push(st1,n,t);
if(DeQueue(sq,n))
Push(st1,n,time_now);
}
break;
case 3:
if(!StackEmpty(st1))
{
printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/
DispStack(st1);
}
else
printf("  >>停车场中无车辆!\n");
break;
case 4:
if(!QueueEmpty(sq))
{
printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/
DispQueue(sq);
}
else
printf("  >>候车场中无车辆!\n");
break;
case 0:
if (!StackEmpty(st1))
{
printf("  >>停车场中的车辆:"); /*输出停车场中的车辆*/
DispStack(st1);
}
if (!QueueEmpty(sq))
{
printf("  >>候车场中的车辆:"); /*输出候车场中的车辆*/
DispQueue(sq);
}
break;
default:    /*其他情况*/
printf("  >>输入的命令错误\n");
break;
}

}
while(number!=0);
return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: