停车场模拟管理系统实验报告.docx

上传人:牧羊曲112 文档编号:5034391 上传时间:2023-05-30 格式:DOCX 页数:20 大小:122.87KB
返回 下载 相关 举报
停车场模拟管理系统实验报告.docx_第1页
第1页 / 共20页
停车场模拟管理系统实验报告.docx_第2页
第2页 / 共20页
停车场模拟管理系统实验报告.docx_第3页
第3页 / 共20页
停车场模拟管理系统实验报告.docx_第4页
第4页 / 共20页
停车场模拟管理系统实验报告.docx_第5页
第5页 / 共20页
亲,该文档总共20页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《停车场模拟管理系统实验报告.docx》由会员分享,可在线阅读,更多相关《停车场模拟管理系统实验报告.docx(20页珍藏版)》请在三一办公上搜索。

1、一. 问题描述1.实验题目:设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进 出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南 端,最先到达的第一辆车停放在车场的最北端)。若停车场内已经停满n辆车, 那么后来的车只能在门外的便道上等候。一旦有车开走,则排在便道上的第一辆 车即可开入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场 为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场。每辆停放在车 场的车在它离开停车场时必须按它停留的时间长短缴纳费用。试为停车场编制按 上述要求进行管理的模拟程序.要求:根据各结点的信息,调用相应的函数或者语

2、句,将结点入栈入队,出栈或者 出队。二. 需求分析1。程序所能达到的基本可能:程序以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入数据的序 列进行模拟管理。栈以顺序结构实现,队列以链表结构实现。同时另设一个栈, 临时停放为给要离去的汽车让路而从停车场退出来的汽车。输入数据按到达或离 去的时刻有序。当输入数据包括数据项为汽车的“到达”A表示)信息,汽车 标识(牌照号)以及到达时刻时,应输出汽车在停车场内或者便道上的停车位置; 当输入数据包括数据项为汽车的“离去” D表示)信息,汽车标识(牌照号) 以及离去时刻时,应输出汽车在停车场停留的时间和应缴纳的费用(便道上停留 的时间不收费);当输入

3、数据项为(P ,0,0)时,应输出停车场的车数;当输入 数据项为(W,0, 0)时,应输出候车场车数;当输入数据项为(E, 0,0),退 出程序;若输入数据项不是以上所述,就输出ERROR!” .2。输入输出形式及输入值范围:程序运行后进入循环,显示提示信息:“Please input the state,number and time of the car:”,提示用户输入车辆信息(“到达”或者“离开”,车牌编号,到达或 者离开的时间)。若车辆信息为“到达”,车辆信息开始进栈(模拟停车场),当 栈满,会显示栈满信息:“The parking place is full!”,同时车辆进队列(模

4、拟停车 场旁便道),并显示该进入便道车辆的车牌编号,让用户知道该车的具体位置; 若车辆信息为“离开”,会显示该车进入停车场的时间以及相应的停车费用,若 该车较部分车早进停车场,这部分车需先退出停车场,暂时进入一个新栈为其让 道,会显示进入新栈的车辆的车牌编号及其入停车场的时间,当待离开车离开停 车场后,这部分车会重新进入停车场,同时便道上的第一辆车进入停车场;若输 入(P,0,0),会显示停车场的车数;若输入(W,0,0),会显示便道上 的车数;若输入(E,0, 0),程序会跳出循环,同时程序结束;若输入为其 他字母,程序会显示“ERROR!”报错。若便道上没有车辆停靠,会显示便道为空 的信息

5、:用户每输入一组数据,程序就会根据相应输入给出输出。输入值第一个 必须为字母,后两个为数字。三.概要设计为了实现上述功能,该程序以栈模拟停车场以及临时停放为给要离去的汽车 让路而从停车场退出来的汽车的场地,以队列模拟车场外的便道,因此需要栈和 队列这两个抽象数据类型。1. 栈抽象数据类型定义:ADT SqStack 数据对象:D= , b , c d I a e int, b e int, c e int, d e char,i i i, i iiiii=1,2, 3。.。,n,n数据关系:R= (a , b , d ) I a , b d e D, a , b d e struct car;

6、i i i i i, ii i, i基本操作:Judge_Output(s,q,r); /根据r中车辆信息控制车辆是入栈s还 是入队q以及相关操作A_cars(s,q, a); 将到达车辆a的信息入栈s或者入队q D_cars (s,q, d) ;/将待离开车辆d出栈s,并将q中相应车辆入栈并进行相关的操作ADT SqStack2。队列抽象数据类型定义:ADT LinkQueue数据对象:D= a ,b , c I a g Qnode *, b g Qnode i i i ii大,c gint , i=1, 2,3.。.,n, n;数据关系:R=;基本操作:Judge_Output (s,q,

7、r);/根据r中车辆信息控制车辆是入栈s还是入队q以及相关操作A_cars (s, q, a); /将到达车辆a的信息入栈s或者入队qD_cars(s,q, d); /将待离开车辆d出栈s,并将q中相应车辆入栈并进行相关的操作 ADT LinkQueue3。主要算法流程图:I. Judge_Output算法流程图:F输入为E (e)结束II. A_cars算法流程图:1Tr.、停车场未满1FF车进停车场车进便道1结束III. D_cars算法流程图:4. 本程序保护模块:主函数模块栈单元模块:实现栈的抽象数据类型队列单元模块:实现队列的抽象数据类型调用关系:栈单元模块主函数模块|-/ 队列单元

8、模块四.详细设计1. 相关头文件库的调用说明:# include#include#define MAXSIZE 14#define n 2#define fee 102. 元素类型、结点类型和结点指针类型:struct car char bb;int num;int time;;struct rangweicarint num;int time;typedef struct stackk struct rangweicar HMAXSIZE;int topp ; SqStackk;#define QNODE struct QnodeQNODE int data;QNODE *next;3. 栈

9、类型和队列类型:typedef struct stack struct car Gn;int top;SqStack;typedef struct linkqueue QNODE *front,大 rear;int geshu;LinkQueue;/部分基本操作的伪码实现void Judge_Output (SqStack *s, LinkQueue *q,struct car *r) if(大r).bb= E | (*r)。bb= e)printf (STOP!n);else if (*r).bb=P I | (*r) .bb= p)printf( The number of parking

10、 cars is %dn”, (s-top)+1); else if(大r)。bb=WII (大r)。bb= w) printf(The number of waiting cars is %dn”, q-geshu);else if(*r).bb= A| (*r)。bb=a)A_cars(s,q, *r);else if (大r)。bb= D| (大r).bb=d)D_cars (s, q,*r);elseprintf (ERROR! n);A_cars(SqStack 大 s,LinkQueue *q, struct car a)QNODE 大 t;if (stop ! =n-1)(sto

11、p) +;(sG s-top )。bb=a.bb;(s-G s-top ).num=a.num;(s-Gs-top)。time=a.time;else printf (The parking place is full!n);t= (QNODE *) malloc(sizeof (QNODE);t一data=a.num;t-next=NULL;qrearnext=t;qrear=t;printf(the number of the car in the access road is:%dn, qrear-data);q-geshu+;int D_cars (SqStack 大 s, LinkQ

12、ueue *q,struct car d)int i, j,l;float x,y;QNODE *p;SqStackk 大 k;if(d。num= (sG stop)。num)x=d.time- (s-Gs-top)。time;y=fee*x;printf(” The time is %。2f hours,the fee is %.2f yuann”,x, y);if(q-geshu=0)printf( The queue is empty! n);return 0;elsep=q-front一next;q一front-next=p一next;(s一Gs一top).num=pdata;(s一G

13、s-top) .time=d.time;free(p);q一geshu一-;if (q一front-next=NULL )q-rear=qfront;return 1;elsefor(i=0; i(stop); i+)if( (s一G i)。num!=d.num)continue;else break;if (i=(s-top)(printf (ERROR! n);return 一1;x=d.time (s一Gi)。time;y=fee*x;printf ( The time is %.2f hours,the fee is %。2f yuann ,x,y); k= (SqStackk *)

14、malloc(sizeof(SqStackk);ktopp=1;for (j= (stop);ji;j-)ktopp+;(kH k-topp) .num= (s-Gj) .num;(k一H k-topp)。time=(s一G j) .time;stop;for (l=0;l=(ktopp) ; l+)printf(the information(number and time) in the new stack is:n);printf (%d,%dn”, (k-H l) .num, (kHl)。time); stop-;while(ktopp=0)stop+;(sG stop)。bb=A ;

15、(s-G s-top)。num=(k-Hktopp)。num;(s一G stop)。time= (kH k-topp )。time; ktopp-;if(q-geshu=0) printf (The access road is empty! n); return 2;elses-top+;p=qfrontnext;q-frontnext=pnext;(s-Gstop) .num=pdata;(s一Gs-top).time=d。time;free(p);qgeshu;if(qfrontnext=NULL) qrear=qfront;return 3;4。主函数的伪码:main ()SqStac

16、k 大 s;LinkQueue *q;QNODE 大 p;struct car aa MAXSIZE;int i;s= (SqStack *)malloc(sizeof (SqStack);stop=1 ;q= (LinkQueue *)malloc(sizeof (LinkQueue);p=(QNODE *)malloc (sizeof (QNODE);p-next=NULL;q一front=q一rear=p;qgeshu=0;printf(* * *大* * * * * * *大* * * * * * *printf( * * * * * * * * * *n”);printf(” * *

17、 * *n”);printf(”* * * * * * * * * * * * * *n);printf ( * * * * * * * * * * * * * * * * * *n”);* * * * *停车场管理系统* * * * * * *n);for(i=0; iMAXSIZE; i+)printf(” Please input the state, number and time of the car: n);scanf (” %c, %d, %d”, &(aai。bb),&(aa i.num),&(aai。time);getchar ();Judge_Output (s, q,&a

18、ai);if (aai。bb=E II aai.bb=e) break;5.函数调用关系:五.测试分析:1. 出现问题及解决办法:该程序是四个程序调试中最顺利的一个,只在一个地方上出了问题,就是输 入字符时由于回车键也是字符,回车键总会被读入,导致经常输出“ERROR!”. 后来找到原因后在scanf函数后紧接着加了一个getchar();语句后就恢复了正常。2。方法优缺点分析:优点:用栈和队列来模拟停车场让整个问题显得简单,易于实现;缺点:栈和队列这两个数学模型用在停车场管理上还是有失妥当的,现实中停车 场出口入口不可能为同一处,不可能当一辆车要离开,在它后面进来的车必须为 它让路,因此无法

19、用栈的“后进先出”原则来模拟;而且没有考虑便道上的车在 等待过程中可以中途开走等情况,而这些都无法用队列的“先进先出”原则来模 拟.3. 主要算法的时间和空间复杂度分析:(1)由于算法Judge_Output函数根据判断条件,每次只选择一个程序段执行, 所以其时间复杂度是O(1);(2) 由于算法A_cars函数根据判断条件,将数据入栈或入队列,所以其时间复 杂度也是0(1);(3) 由于算法D_cars函数在出栈数据不在最顶端时需将n个数据先出该栈,再入 新栈,再回旧栈的操作,故其时间复杂度是0(n);(4) 所有算法的空间复杂度都是0(1).六. 使用说明程序运行后用户根据提示一次输入车辆

20、的状态信息,车牌编号,时间,程序 会根据车辆的状态信息调用相应的函数,并输出用户想得到的信息。七. 调试结果输入数据:(A ,1,5),(A,2,10),(D,1,15),(A,3,20),(A,4, 25),( A,5, 30) , (D,2, 35) , (D,4,40),(P,0, 0),( W ,0, 0),(F, 0, 0) ,( (E, 0,0)。输出数据:1号车停放时间为10小时,收费100元;2号车停放时间为25小时, 收费250元;4号车停放5小时,收费50元;此时停车场有两辆车,便道上无车.若 停车场已满,则会显示停车场已满的信息;若便道上无车等待停车,会显示便道 上无车的

21、信息;若中途有车离开,需其后的车让道,会显示进入临时停车场的车 辆的信息;若输入(F, 0, 0),输出“ERROR!” ;若输入(E, 0,0),程序结 束。运行结果截屏:pleasePlease A,2,10 Please D,l,15input the state,number and time ofinput the state,number and time ofinputthe state,number and time ofThe time is 10.00 hours,the fee is 100.00the car:the car:the car: ijuanthe inf

22、orinat ion in the neu stack is :2,10The access road is emptyfPlease input the state,number and time of the car:A,3,20Please input the state,number and time of the car:A,4,25The parking place is full?the numbeF of the car in the access road is:4Please input the state,number and time of the car:A,5,30

23、The parking place is full?the numbeF of the car in the access road is:5Please input the state,number and time of the car: D,2,35The time is 25.00 hours,the fee is 250.00 ijuanhliR *i n-FnvmAt: inn niimhRi and t: i mft in 1:11ft npu si: ar k i kcar:car:car:car:3,20八;附录input the state,nunbep and time

24、of the D,4,40F.0.0 赢fefee源程序文件清单:卵hours,the_ fee is 50.00齐叩 rTiseini)ut the state,number and time of the 机?top)+1);else if(*r)。bb=W|(*r) .bb=w)/大若车辆状态为W,输出便道车辆数大/printf (”The number of waiting cars is %dn”,q-geshu);else if (*r) .bb=A II (*r)。bb=a)/*若车辆状态为A,调用 A_cars 函数大/A_cars (s,q, 大 r);else if(*r)

25、 .bb=D| I (大r)。bb=d)/大若车辆状态为D,调用 D_cars 函数*/D_cars (s,q, *r);elseprintf (ERROR! n);/大若车辆状态为其他字母,报错*/A_cars (SqStack大s,LinkQueue大q, struct car a) /大该算法实现对车辆状态为到达的车 辆的操 QNODE大t;作大/if(s一top!=n-1)/大若停车场还没有满,则车进停车场,并存入车辆的状态,车牌编(s-top)+;号和到达时间信息*/(sGs-top ).bb=a.bb;(s-G s-top) .num=a.num;(s-G s-top)。time=

26、a。time;elseprintf(”The parking place is full!n);/*若停车场已满,车进便道,并显示该车的车牌编t= (QNODE大)malloc(sizeof(QNODE);号,同时记录便道车辆数目大/tdata=a.num;t-next=NULL;q-rearnext=t;q-rear=t;printf (”the number of the car in the access road is:%dn”,qreardata);qgeshu+;int D_cars (SqStack 大 s,LinkQueue 大q,struct car d)/*该算法实现车辆状

27、态为离开的车int i,j,l;辆的操作大/float x,y;QNODE 大 p;SqStackk 大 k;if(d.num= (s-Gs-top).num)/大若待离开车为最后进停车场的车的情况*/x=d.time-(sG stop)。time;y=fee*x;/大直接计算停车时间,费用并离去*/printf (”The time is %.2f hours, the fee is %。2f yuann”,x,y);if (qgeshu=0)/大若便道上无车,函数返回大/printf(The queue is empty!n”);return 0;Else/*若便道上有车,第一辆车进停车场

28、大/p=q-frontnext;qfront-next= next;(sGs-top).num= data;/*并存入其车牌编号及进停车场的时间*/(sG s-top ).time=d.time;free(p);qgeshu;if(qfront-next=NULL)qrear=qfront;/大若此时便道上无车,返回1*/return 1;Else/*待离开的车不是最后进停车场的那辆车的情况*/for(i=0;i(stop) ;i+)/大先找到待离开车在停车场中的位置*/if (sG i) .num!=d.num) continue;else break; if(i= (stop)printf

29、(ERROR!n”);return -1;x=d。time(s-G i ).time;/大计算待离开车的停车时间并计算费用*/y=fee*x;printf (”The time is %。2f hours,the fee is %。2f yuann”,x, y);k= (SqStackk大)malloc (sizeof(SqStackk);/大设立一个新栈临时停放为该车离开而让ktopp=1;路的车辆*/for (j=(s-top); ji; j-)k-topp+;(kH k-topp ).num= (s-G j)。num;(kH k-topp)。time=(sGj).time; stop-;

30、for(l=0;l=(ktopp) ;l+)printffthe information(number and time) in the new stack isM);printf(%d, %dn”,(k一Hl )。num,(kHl) .time); /*显示在新栈中的车辆信息*/s-top;while(k一topp=0)/大将新栈中的车重新开入停车场中*/(s一top+;(s-Gs一top) .bb=A;(s-Gs一top ).num=(k-H k-topp ).num;(s-G stop ) o time= (k-H k一topp) .time;k一topp;if (q一geshu=0)/

31、*若便道上无车,则返回2,无车开入停车场中*/(printf(The access road is empty!n);return 2;Else/大若便道上有车,则第一辆车开入停车场中*/stop+;p=q一front-next;q-front一next=p-next;(sGs-top)o num=p-data;(s-Gs-top )o time=do time;free(p);qgeshu-;if(q-front-next=NULL)q-rear=qfront;return 3;main () SqStack *s;LinkQueue *q;QNODE 大 p;struct car aaMA

32、XSIZE;int i;s= (SqStack 大)malloc (sizeof (SqStack) );/大对停车场初始化大/s一top=1;q= (LinkQueue 大)malloc(sizeof (LinkQueue);p= (QNODE *) malloc(sizeof(QNODE) );/大对便道初始化大/pnext=NULL;qfront=qrear=p;qgeshu=0;printf (* * * *大大*n”);大大大大*大*大*大大*大*printf (* *n)printf ( ” * * *n);printf(* *;* *停车场管理系统* *n);printf (* * * * * * * * * * * * *n”);for (i=0; iMAXSIZE;i+)/*输入车辆信息*/(printf (Please input the state, number and time of the car: n”);scanf(”c,%d,%d”,&(aa i。bb), & (aai.num),&(aai。time);getchar ();Judge_Output (s, q,&aai);if (aai .bb=E) break;

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

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号