1、数据结构课程设计停车场管理系统的设计与实现停车场管理系统的设计与实现一、需求分析1、问题描述停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后开入的车辆必须先退出车场为它让路,待该辆车开出大门外,其它车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模
2、拟程序。2、基本要求a. 以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。b. 每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码及到达或离去的时刻,对每一组输入数据进行操作后的输出数据为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车离去;则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。c. 栈以顺序结构实现,队列以链表实现。二、概要设计1.数据结构本程序需要用到五个结构体,分别为时间Time,车辆信息Car,模拟停车场SqStack,车辆顺序链表QueueNode以及模拟便道LinkQueue。2
3、.程序模块本程序包含两个模块,一个是实现功能的函数的模块,另一个是主函数模块。系统子程序及功能设计本系统共有五个子程序,分别是:void InitStack(SqStack *s,int n) /初始化栈int InitQueue(LinkQueue *Q) /初始化链表void Leaveinformation(Car *p) /离开的车辆信息void Arrive(SqStack *A,LinkQueue *B,int n) /汽车进站void Leave(SqStack *A,SqStack *C,LinkQueue *B,int n) /汽车出站3. 各模块之间的调用关系以及算法设计A
4、rrive函数需要调用Leaveinformantion函数主函数调用InitStack, InitQueue ,Leaveinformation, Arrive, Leave这些函数。三、详细设计1.数据类型定义typedef struct time int hour; int min; Time; typedef struct char num10; Time reach; Time leave; Car; typedef struct Car *stackMAX+1; int top; SqStack; /停车场typedef struct car Car *data; struct c
5、ar *next; QueueNode; typedef struct Node QueueNode *head; QueueNode *rear;LinkQueue;/便道2.系统主要子程序详细设计a. 初始化栈void InitStack(SqStack *s,int n) int i; s-top=0; for(i=0;istacks-top=NULL; b.初始化链表int InitQueue(LinkQueue *Q) Q-head=(QueueNode *)malloc(sizeof(QueueNode); if(Q-head!=NULL) Q-head-next=NULL; Q-
6、rear=Q-head; return 1; else return 0; c.显示离开的车辆信息void Leaveinformation(Car *p) /离开的车辆信息 int A1,A2,B1,B2,a,b,c; char d; cout请输入离开的时间:(小时:分钟):p-leave.hourdp-leave.min; cout出站的车的车牌号为:reach.hour; A2=p-reach.min; B1=p-leave.hour; B2=p-leave.min; a=(B1-A1)*60+B2-A2; if(a=60) b=a/60; c=a-60*b; else b=0; c=
7、a; cout 出站车辆信息endl; cout-endl; cout进停车场时间:( 小时:分钟) endl; coutreach.hour:reach.minendl; cout出停车场时间: ( 小时:分钟) endl; coutleave.hour:leave.minendl; cout停留时间: endl; coutbhcminendl; cout应付费用(每分钟0.1元): endl; cout0.1*a元endl; cout-endl; free(p); d.汽车进站void Arrive(SqStack *A,LinkQueue *B,int n) /汽车进站 Car *p;
8、QueueNode *t; int a,b; char c; string d; p=(Car *)malloc(sizeof(Car); cout请输入车牌号(例:AB1234):d; if(A-toptop+; cout请输入到达时间:(小时:分钟)p-reach.hourcp-reach.min; cout车辆在停车场的位置为:topstackA-top=p; else cout停车场已满需等候data=p; t-next=NULL; B-rear-next=t; B-rear=t; e.汽车出战void Leave(SqStack *A,SqStack *C,LinkQueue *B,
9、int n) /汽车出站 int i, room; Car *p,*t; QueueNode *q; cout请输入要离开的车在车场的位置:room; while(A-toproom) C-top+; C-stackC-top=A-stackA-top; A-stackA-top=NULL; A-top-; p=A-stackA-top; A-stackA-top=NULL; A-top-; while(C-top=1) A-top+; A-stackA-top=C-stackC-top; C-stackC-top=NULL; C-top-; Leaveinformation(p); if(B
10、-head!=B-rear)&A-tophead-next; t=q-data; A-top+; cout便道上的汽车进入车场.该车的车牌号为:endl; cout该车进入停车场的位置为:tophead-next=q-next; if(q=B-rear) B-rear=B-head; A-stackA-top=t; free(q); 四、测试与分析1.显示主菜单,运行程序可以显示出如下界面。2.车辆进站在主菜单下选1,车辆进站,输入车牌号以及车辆到达的时间。3.车辆出战在主菜单下选2,车辆出站,输入要离开停车场的车辆位置,以及离开的时间。可以得到车俩在停车场的停留时间以及应付费用。4.退出系统
11、退出程序。五、附录1.停车场管理.h#include#include#include #includeusing namespace std;#define MAX 2typedef struct time int hour; int min; Time; typedef struct char num10; Time reach; Time leave; Car; typedef struct Car *stackMAX+1; int top; SqStack; /停车场typedef struct car Car *data; struct car *next; QueueNode; ty
12、pedef struct Node QueueNode *head; QueueNode *rear;LinkQueue;/便道void InitStack(SqStack *s,int n) int i; s-top=0; for(i=0;istacks-top=NULL; int InitQueue(LinkQueue *Q) Q-head=(QueueNode *)malloc(sizeof(QueueNode); if(Q-head!=NULL) Q-head-next=NULL; Q-rear=Q-head; return 1; else return 0; void Leavein
13、formation(Car *p) /离开的车辆信息 int A1,A2,B1,B2,a,b,c; char d; cout请输入离开的时间:(小时:分钟):p-leave.hourdp-leave.min; cout出站的车的车牌号为:reach.hour; A2=p-reach.min; B1=p-leave.hour; B2=p-leave.min; a=(B1-A1)*60+B2-A2; if(a=60) b=a/60; c=a-60*b; else b=0; c=a; cout 出站车辆信息endl; cout-endl; cout进停车场时间:( 小时:分钟) endl; cout
14、reach.hour:reach.minendl; cout出停车场时间: ( 小时:分钟) endl; coutleave.hour:leave.minendl; cout停留时间: endl; coutbhcminendl; cout应付费用(每分钟0.1元): endl; cout0.1*a元endl; cout-endl; free(p); void Arrive(SqStack *A,LinkQueue *B,int n) /汽车进站 Car *p; QueueNode *t; int a,b; char c; string d; p=(Car *)malloc(sizeof(Car
15、); cout请输入车牌号(例:AB1234):d; if(A-toptop+; cout请输入到达时间:(小时:分钟)p-reach.hourcp-reach.min; cout车辆在停车场的位置为:topstackA-top=p; else cout停车场已满需等候data=p; t-next=NULL; B-rear-next=t; B-rear=t; void Leave(SqStack *A,SqStack *C,LinkQueue *B,int n) /汽车出站 int i, room; Car *p,*t; QueueNode *q; cout请输入要离开的车在车场的位置:roo
16、m; while(A-toproom) C-top+; C-stackC-top=A-stackA-top; A-stackA-top=NULL; A-top-; p=A-stackA-top; A-stackA-top=NULL; A-top-; while(C-top=1) A-top+; A-stackA-top=C-stackC-top; C-stackC-top=NULL; C-top-; Leaveinformation(p); if(B-head!=B-rear)&A-tophead-next; t=q-data; A-top+; cout便道上的汽车进入车场.该车的车牌号为:e
17、ndl; cout该车进入停车场的位置为:tophead-next=q-next; if(q=B-rear) B-rear=B-head; A-stackA-top=t; free(q); 2.main.c(主函数)#include停车场管理.hvoid main() SqStack Enter,Temp; LinkQueue Wait; int ch,n; n=MAX; InitStack(&Enter,n); InitStack(&Temp,n); InitQueue(&Wait); while(1) cout 停车场管理系统 endl; cout-endl; cout 1. 车辆进站 endl; cout 2. 车辆出站 endl; cout 3. 退出系统 endl; cout请选择: 13:ch; if(ch3 | ch1) do cout输入有误,请重新输入!ch; while(ch=1); switch(ch) case 1:Arrive(&Enter,&Wait,n); break; case 2:Leave(&Enter,&Temp,&Wait,n); break; case 3:exit(0); default: break; 六、用户手册运行停车管理.exe,即可运行该停车管理系统并完成相应的功能。