数据结构停车场问题实验报告.docx

上传人:b****4 文档编号:11701414 上传时间:2023-03-30 格式:DOCX 页数:17 大小:112.29KB
下载 相关 举报
数据结构停车场问题实验报告.docx_第1页
第1页 / 共17页
数据结构停车场问题实验报告.docx_第2页
第2页 / 共17页
数据结构停车场问题实验报告.docx_第3页
第3页 / 共17页
数据结构停车场问题实验报告.docx_第4页
第4页 / 共17页
数据结构停车场问题实验报告.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

数据结构停车场问题实验报告.docx

《数据结构停车场问题实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构停车场问题实验报告.docx(17页珍藏版)》请在冰豆网上搜索。

数据结构停车场问题实验报告.docx

数据结构停车场问题实验报告

――停车场管理问题

姓名:

学号:

一、问题描述

设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。

车辆按到达停车场时间的早晚依次从停车场最里面向大门口处停放(最先到达的第一辆车放在停车场的最里面)。

如果停车场已放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车就进入停车场。

停车场内如有某辆车要开走,在它之后进入停车场的车都必须先退出停车场为它让路,待其开出停车场后,这些车辆再依原来的次序进场。

每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。

如果停留在便道上的车未进停车场就要离去,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆的次序。

编制一程序模拟该停车场的管理。

二、实现要求

(停车场或便道上),以及某辆车离开停车场时应交纳的费用和

要求程序输出每辆车到达后的停车位置

它在停车场内停留的时间。

三、实现提示

汽车的模拟输入信息格式可以是:

(到达/离去,汽车牌照号码,到达/离去的时刻)。

例如,(‘A',,1,5)表示1号牌照车在5这个时刻到达,而(‘D',,5,20)表示5号牌照车在20这个时刻离去。

整个程序可以在输入信息为(‘E',0,0)时结束。

本题可用栈和队列来实现。

四、需求分析

停车场采用栈式结构,停车场外的便道采用队列结构(即便道就是等候队列)。

停车场的管理流程如

1当车辆要进入停车场时,检查停车场是否已满,如果未满则车辆进栈(车辆进入停车场);如果停车场已满,则车辆进入等候队列(车辆进入便道等候)。

2当车辆要求出栈时,该车到栈顶的那些车辆先弹出栈(在它之后进入的车辆必须先退出车场为它让路),再让该车出栈,其他车辆再按原次序进栈(进入车场)。

当车辆出栈完毕后,检查等候队列(便道)

中是否有车,有车则从队列头取出一辆车压入栈中。

五、流程图

幵始

附俎化两乍桟Euler和

Temp段一个讥列Wail

 

车到达

旱■

退出

Room杓车站煜临时慢

退岀列袤

显示

便逋车进车场

'■troom

t

 

元Etiter

栈Enicr元素岀険

队fiWat中

1

沅畫出仇

限刃中元鶯进找

六、详细设计

1.本程序主要包含四个模块

1)主程序模块

intmain(){

Initialization();

CarNodecar;

SqStackPark,TempPark;

LinkQueueQ;

InitStack(Park);

InitStack(TempPark);

InitQueue(Q);

while((scanf("%c%d%d",&car.event,&car.num,&car.time))&&(car.event!

='e'&&car.event!

='E')){

getchar();

switch(car.event)

{

case'A':

case'a':

Arrive(Park,Q,car);break;

case'D':

case'd':

Leave(Park,TempPark,Q,car);break;

default:

printf("您的第一个数据输入有误!

\n");break;

}

}

printf("程序结束,谢谢使用!

\n");

return0;

2)分别构造空栈和空队列

栈:

StatusInitStack(SqStack&S){//构造一个空栈

S.Stacksize=0;

S.base=(CarNode*)malloc((MAX)*sizeof(CarNode));

if(!

S.base){

exit(OVERFLOW);

printf("存储空间分配失败");

}

S.top=S.base;

returnOK;

}

队列:

StatusInitQueue(LinkQueue&Q){//构造一个空队列(带头结点)

Q.front=Q.rear=(QueueNode*)malloc(sizeof(QueueNode));

if(!

Q.front){

exit(OVERFLOW);

printf("存储空间分配失败");

}

Q.front->next=NULL;

Q.queuesize=0;

returnOK;

3)车辆到达处理

StatusArrive(SqStack&S,LinkQueue&Q,CarNode&e){//车辆到达处理if((S.top-1)->time<=e.time){//时间处理

if(!

Check_Stack(S,e)&&!

Check_Queue(Q,e)){//是否已存在if(S.top-S.base

Push(S,e);

printf("成功进入停车场,在%d号车库!

\n",S.top-S.base);

returnOK;

}

else{

EnQueue(Q,e);

printf(”停车场已满,车辆进入便道,在%d号车位!

\n",Q.queuesize);

}

}

else

printf("该牌照的车已存在,输入有误,请重新输入\n");returnOK;

}

else{

printf("时间输入有误,请重新输入!

\n");

returnFALSE;

}

}

4)车辆离开处理

StatusLeave(SqStack&S,SqStack&TempS,LinkQueue&Q,CarNode&e){//车辆离开处理

CarNodea;

intleatime,leanum;

intentertime;//进入停车场时间

intcost;

if(!

(Check_Stack(S,e)||Check_Queue(Q,e)))

{

\n");

printf("数据输入错误,本停车场内无所查询车辆,请重新输入!

returntrue;

}

else

{

if(Check_Stack(S,e))//若需要离开的车辆在停车场{

if(e.num==(S.top-1)->num)//车辆处在栈顶

{

Pop(S,a);

leatime=e.time;

leanum=e.num;

entertime=a.time;

printf("车辆进入车库时间:

%d\t现在(离开)时间:

%d\t停留时

「可:

%d\t\n",entertime,leatime,leatime-entertime);

}

else//车辆处在栈中间

{

do{

Pop(S,a);//从栈中依次退出

Push(TempS,a);//依次进入临时栈

}while((S.top-1)->num!

=e.num);//直到top指针下一个位置的num=车牌号Pop(S,a);//该车离开

leatime=e.time;

leanum=e.num;

entertime=a.time;

printf("车进入停车场时间:

%d\t现在(离开)时间:

%d\t停留时

「可:

%d\t\n",entertime,leatime,leatime-entertime);

do{//其余车辆按原来次序返回停车场

Pop(TempS,a);

Push(S,a);

}while(TempS.top!

=TempS.base);//条件与上面不同,此时是全部回去

}

cost=(leatime-entertime)*price;

if(cost>=0)

printf("您的车牌号为%d的车应交纳的费用是:

%d\n",leanum,cost);

if(Q.front!

=Q.rear){//队列不空的话从便道进停车场

DeQueue(Q,a);

if(a.time

entertime=leatime;

a.time=leatime;

Push(S,a);//该车进入停车场

printf("车牌号为%d的车辆从便道上进入%d号车库!

从现在开始计时,现在时间

为:

%d\n",a.num,S.top-S.base,a.time);

}

elseif(Check_Queue(Q,e)){//从便道直接离开

do{

DeQueue(Q,a);

EnQueue(Q,a);

}while(Q.front->next->data.num!

=e.num);

DeQueue(Q,e);//前面的车进入队尾

0!

\n",e.num);

printf("您的车牌号为%d的车辆未进入车库从便道直接离开,费用为}

}

returntrue;

2.主要设计程序如下

#include

#include

#include

#defineMAX2//停车场容量

#defineprice2//单价

#defineOK1

#defineFALSE0

#defineTRUE1

#defineERROR-1

#defineOVERFLOW-2

typedefintStatus;

//=================================================================typedefstructCarNode{

charevent;

intnum;

inttime;

}CarNode;//车辆信息结点

typedefstructSqStack{

CarNode*base;

CarNode*top;

intStacksize;

}SqStack;//栈(停车场)

typedefstructQNode{

CarNodedata;

structQNode*next;

}QueueNode;//便道结点

typedefstructLinkQueue{

QueueNode*front;

QueueNode*rear;

intqueuesize;

}LinkQueue;//队列(便道)

//=====================================================================

StatusInitStack(SqStack&S){//构造一个空栈

S.Stacksize=0;

S.base=(CarNode*)malloc((MAX)*sizeof(CarNode));

if(!

S.base){

exit(OVERFLOW);

printf("存储空间分配失败");

}

S.top=S.base;

returnOK;

//=====================================================================

StatusInitQueue(LinkQueue&Q){//构造一个空队列(带头结点)

Q.front=Q.rear=(QueueNode*)malloc(sizeof(QueueNode));

if(!

Q.front){

exit(OVERFLOW);

printf("存储空间分配失败");

}

Q.front->next=NULL;

Q.queuesize=0;

returnOK;

}

//=====================================================================

StatusGetTop(SqStackS,CarNode&e){//返回栈顶元素

if(S.top==S.base)

returnERROR;

e=*(S.top-1);

returnTRUE;

}

//=====================================================================

StatusPop(SqStack&S,CarNode&e){//删除栈顶元素

if(S.top==S.base)

returnERROR;

e=*--S.top;

returnOK;

}

//=====================================================================

StatusPush(SqStack&S,CarNodee){//插入元素为新的栈顶元素(在栈不满的前提下)if(S.top-S.base>=MAX)

returnFALSE;

*S.top++=e;

returnOK;

}

//=====================================================================

StatusDeQueue(LinkQueue&Q,CarNode&e){//删除队头元素(带头结点)

if(Q.rear==Q.front)

returnERROR;

QueueNode*p=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(p==Q.rear)

Q.rear=Q.front;

free(p);

Q.queuesize--;

returnOK;

}

//=====================================================================

StatusEnQueue(LinkQueue&Q,CarNodee){//插入新的队尾元素

QueueNode*p=(QueueNode*)malloc(sizeof(QueueNode));

if(!

p)

exit(OVERFLOW);

p->data=e;

p->next=NULL;

Q.rear->next=p;

Q.rear=p;

Q.queuesize++;

returnOK;

}

//=====================================================================StatusCheck_Stack(SqStack&S,CarNodee){//车辆到达时车库内是否有同名车

CarNode*Temp=S.base;

while((Temp!

=(S.top))&&(Temp->num!

=e.num))

Temp++;

if((Temp==S.top))

returnFALSE;

else

returnTRUE;

}

//=====================================================================

StatusCheck_Queue(LinkQueue&Q,CarNodee){//车辆到达时便道上是否有同名车

QueueNode*Temp=Q.front;

while((Temp!

=Q.rear)&&(Temp->data.num!

=e.num))

Temp=Temp->next;

if((Temp==Q.rear)&&(Temp->data.num!

=e.num))

returnFALSE;

else

returnTRUE;

}//=====================================================================StatusArrive(SqStack&S,LinkQueue&Q,CarNode&e){//车辆到达处理

if((S.top-1)->time<=e.time){//时间处理

if(!

Check_Stack(S,e)&&!

Check_Queue(Q,e)){//是否已存在

if(S.top-S.base

Push(S,e);

printf("成功进入停车场,在%d号车库!

\n",S.top-S.base);

returnOK;

}

else{

EnQueue(Q,e);

else

printf("该牌照的车已存在,输入有误,请重新输入\n");returnOK;

}

else{

printf("时间输入有误,请重新输入!

\n");

returnFALSE;

}

}

//=======================================================

StatusLeave(SqStack&S,SqStack&TempS,LinkQueue&Q,CarNode&e)

{//车辆离开处理

CarNodea;

intleatime,leanum;

intentertime;//进入停车场时间

intcost;

if(!

(Check_Stack(S,e)||Check_Queue(Q,e)))

{

\n");

printf("数据输入错误,本停车场内无所查询车辆,请重新输入!

returntrue;

}

else

{

if(Check_Stack(S,e))//若需要离开的车辆在停车场

{

if(e.num==(S.top-1)->num)//车辆处在栈顶

Pop(S,a);

leatime=e.time;

leanum=e.num;

entertime=a.time;

printf("车辆进入车库时间:

%d\t现在(离开)时间:

%d\t停留时

「可:

%d\t\n",entertime,leatime,leatime-entertime);

}

else//车辆处在栈中间

{

do{

Pop(S,a);//从栈中依次退出

Push(TempS,a);//依次进入临时栈

}while((S.top-1)->num!

=e.num);//直到top指针下一个位置的num=车牌号Pop(S,a);//该车离开

leatime=e.time;

leanum=e.num;

entertime=a.time;

printf("车进入停车场时间:

%d\t现在(离开)时间:

%d\t停留时

「可:

%d\t\n",entertime,leatime,leatime-entertime);

do{//其余车辆按原来次序返回停车场

Pop(TempS,a);

Push(S,a);

}while(TempS.top!

=TempS.base);//条件与上面不同,此时是全部回去

}

cost=(leatime-entertime)*price;

if(cost>=0)

printf("您的车牌号为%d的车应交纳的费用是:

%d\n",leanum,cost);

if(Q.front!

=Q.rear){//队列不空的话从便道进停车场

DeQueue(Q,a);

if(a.time

entertime=leatime;

a.time=leatime;

printf("车牌号为%d的车辆从便道上进入%d号车库!

从现在开始计时,现在时间为:

%d\n",a.num,S.top-S.base,a.time);

}

}

elseif(Check_Queue(Q,e)){//从便道直接离开

do{

DeQueue(Q,a);

EnQueue(Q,a);

}while(Q.front->next->data.num!

=e.num);

DeQueue(Q,e);//前面的车进入队尾

printf("您的车牌号为%d的车辆未进入车库从便道直接离开,费用为0!

\n",e.num);

}

}

returntrue;

}

//=====================================================================

voidInitialization(){//初始化程序

printf("姓名:

杨智伟学号:

2012040651\n");

printf("==========================================================\n");

printf("*停车场管理模拟程序*\n");

printf("==========================================================\n");

printf(”请依次输入车辆到达(A/a)/离去(D/d)/结束(E/e)信息、车牌号以及当前时间:

\n\n");}

//=====================================================================

intmain(){

Initialization();

CarNodecar;

SqStackPark,TempPark;

LinkQueueQ;

InitStack(Park);

InitStack(TempPark);

InitQueue(Q);

while((scanf("%c%d%d",&car.event,&car.num,&car.time))&&(car.event!

='e'&&car.event!

='E'))

{

getchar();//除去输入结束时的回车

switch(car.event)

{

case'A':

case'a':

Arrive(Park,Q,car);break;

case'D':

case'd':

Leave(Park,TempPark,Q,car);break;

default:

printf("您的第一个数据输入有误!

\n");break;

}

}

printf("程序结束,谢谢使用!

\n");

return0;

}

七、程序运行截图

1.交互界面

2.车辆进入

3.车辆离去

IBMam■»bmbmam

停车场管理模抱程序

请嵌次输入车辆到达5”离去结束车牌号以及当前时间*詁嬴心幵坍在丄号车爭

2iH

成功进入停车场■在2号车库!

车逬A#$场时咼5现在(离幵)时葆弓停留时间汩

恆的车脚寻为1的车应交纳的费用是洱

4.停车场已满进入便道

停车场管理模拟程序

请恢次输入车辆到达<A/aV»去结束GAO信息"车牌号以及当前时间=

隹打和人停车九在i耳车库申

a210

成功进入停车场■在2号车库?

«

皐址入焉芙场时护那在(頁开)时间次停留时间迪

您芮车牌舟怙1舸车应交纳血费用

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1