数据结构 停车场管理 完整版 实习报告_地下停车场实习报告
数据结构 停车场管理 完整版 实习报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“地下停车场实习报告”。
实习报告
题目:停车场管理
一. 需求分析
1. 用栈来表示停车场,用队列来表示停车道。
2. 用户需输入车辆的必要信息,如车辆的到达或离开,汽车牌号以及到达或离去的时刻。停车场的容量及单位时间的停车费由编程序者自行设置,结构需输出车辆停车所需缴纳的费用。
3. 本程序要求对车辆的动态能够输出具体的信息内容,包括停车或离开的时间,位置,及所需缴纳的停车费。4. 测试数据为:
N=2,输入数据为:(’A’,1,5),(‘A’,2.,10),(‘D’,1,15),(‘A’,3,20),(‘A’,4,25),(‘A’,5,30),(‘D’,2,35),(‘D’,4,40),(‘E’,0,0).其中:’A’表示到达,’D’表示离去,’E’表示输入结束。5.程序执行的命令为:
1.创建栈和队列。2.对车辆的行为进行相应的处理。3.输出车辆的信息。
二. 概要设计
1.设定栈的抽象数据类型定义:
ADT Stack{
数据对象:D={ai|ai属于Elem,i=1,2……,n, n>=0}
数据关系:R1={| ai-1,ai属于D,i=2,……,n}
基本操作:
initStack(&S)
操作结果:构造一个空栈S.pop(&S,&e)
初始条件:栈S已存在。
操作结果:删除S的栈顶元素,并以e返回其值。
push(&S,&e)
初始条件:栈S已存在。
操作结果:在栈S的栈顶插入新的栈顶元素e。
lengthstack(S)
初始条件:栈S已存在。
操作结果:返回S中的元素个数,即栈的长度。}ADT Stack;2.设定队列的抽象数据类型定义:
ADT Queue{
数据对象:D={ai| ai属于Elem, i=1,2,……,n, n>=0}
数据关系:R1={| ai-1,ai 属于D,i=2,……,n}
基本操作:
initqueue(&Q)
操作结果:构造一个空队列Q.enqueue(&Q, e)
初始条件:队列Q已存在。
操作结果:插入元素e为Q的新的队尾元素。
dequeue(&Q, &e)
初始条件:Q为非空队列。
操作结果:删除Q的对头元素,并用e返回其值。
Lengthqueue(Q)
初始条件:队列Q已存在。
操作结果:返回Q的元素个数,即队列的长度。}ADT Queue 3.本程序主要包括三个模块
1.主程序模块;
int main(){
初始化;
do{
接受命令;
处理命令;
}while(命令!=退出); } 2.处理车辆到达模块; 3.处理车辆离开模块;
各模块之间的调用关系如下:
处理车辆到达模块主程序模块处理车辆离开模块
三. 详细设计
设计程序如下:
#include #define n 2
//将停车场的容量设为2; #define cost 10 //将单位时间的停车费设为10,车道里不收费;
#define OVERFLOW-2
#define ERROR 0 //分配栈的存储空间失败; using namespace std;
typedef struct Elem {//定义元素数据结构类型
int carnum;int time;}Elem;
typedef struct QNode {//队列
struct QNode *next;Elem Qelem;}QNode,*QueuePtr;
typedef struct { QueuePtr front;//队头指针
QueuePtr rear;//队尾指针 }LinkQueue;
void initqueue(LinkQueue &Q){//构造一个空队列
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));if(!Q.front)exit(OVERFLOW);Q.front->next=Q.rear->next=NULL;}
void enqueue(LinkQueue &Q,int carnum,int time){//入队操作
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));p->Qelem.carnum=carnum;p->Qelem.time=time;p->next=NULL;Q.rear->next=p;Q.rear=p;} int lengthqueue(LinkQueue Q){ int i=0;QueuePtr p;p=Q.front->next;while(p!=Q.rear){
i++;
p=p->next;} i++;return i;} void dequeue(LinkQueue &Q,Elem &e){//从对头离队操作,并返回其值 QueuePtr p=(QueuePtr)malloc(sizeof(QNode));if(Q.front==Q.rear)
cout
p=Q.front->next;
e=p->Qelem;
Q.front->next=p->next;
if(Q.rear==p)Q.rear=Q.front;
free(p);} } typedef struct { Elem *base;Elem *top;int stacksize;}Sqstack;void initStack(Sqstack &S){//创建一个空栈
S.base=(Elem*)malloc(n*sizeof(Elem));if(!S.base)exit(OVERFLOW);S.top=S.base;S.stacksize=n;} int push(Sqstack &S,Elem &e)//插入新的元素 { Elem *temp;if(S.top-S.base==S.stacksize)
return 1;else {
temp=S.top;
temp->carnum=e.carnum;
temp->time=e.time;
S.top++;
return 0;} } int lengthstack(Sqstack S){//当前栈的长度
return S.top-S.base;}
int pop(Sqstack &S,Elem &e){ //删除栈顶元素,并返回其值
if(S.top==S.base)return ERROR;e=*--S.top;return 1;} void carin(Sqstack &S,LinkQueue &Q,Elem car){ int k=0;//输入数据正确
QueuePtr r;Elem *temp;temp=S.base;while(temp!=S.top)/在栈中寻找是否有同一编号的车;
{
if(temp->carnum==car.carnum)
{
cout
k=1;//找到了有同一编号的车
break;
}
temp++;} if(k==0&&Q.front!=Q.rear){//在栈中未找到,从队列中查找
r=Q.front->next;//队头
while(r&&r->Qelem.carnum!=car.carnum)
{r=r->next;}
if(r&&r->Qelem.carnum==car.carnum){cout
{
if(S.top-S.base!=S.stacksize)//说明栈未满,{
S.top->carnum=car.carnum;
S.top->time=car.time;
S.top++;
cout
}
else
{
enqueue(Q,car.carnum,car.time);
cout
} } }
void carleave(Sqstack &S,LinkQueue &Q,Elem car){ int ture=0;//在栈中没有找到与要离开的车
Elem e,em,*temp;
QueuePtr p,r;temp=S.base;if(ture==0){
while(temp!=S.top)//先在栈中寻找;
{
if(temp->carnum==car.carnum)
{
int temp_cost;
temp_cost=(car.time-temp->time)*cost;
ture=1;//在栈中找到
couttime
break;
}
temp++;
}
if(ture==1)
//备用栈
{
Sqstack spear;
initStack(spear);
while(S.top!=temp+1)//先在栈中寻找;
{
pop(S,em);
push(spear,em);
}
pop(S,*temp);
if(spear.top!=spear.base)
{
while(spear.top!=spear.base)
{
pop(spear,em);
push(S,em);
} } } if(ture==1&&Q.front!=Q.rear)//栈中有车离开,将队列中的车进入栈中 { dequeue(Q,e);
//离队,并返回数据e S.top->carnum=e.carnum;S.top->time=car.time;S.top++;cout
} } if(ture==0&&Q.front!=Q.rear)//栈中没找到要离开的车
{
p=Q.front;
r=Q.front->next;//队头
while(r&&r->Qelem.carnum!=car.carnum)
{
p=r;
r=r->next;
}
if(r&&r->Qelem.carnum==car.carnum)
ture=2;//在队列中找到要离开的车
if(r&&r->Qelem.carnum==car.carnum)
{
ture=2;
coutQelem.carnum
p->next=r->next;
free(r);
} }//直接从队列离开
if(ture==0)
cout
char c;
int j=0,temp_time,i=1;//i==0,判断临时记录时间的temp_time应该去该次的值,还是上次的值。j==0,表示第一次输入数据,不需要检测数据是否正确
LinkQueue Q;Sqstack S;Elem car;initqueue(Q);
initStack(S);
cout
while(cin>>c>>car.carnum>>car.time)
{
if(j==1)
{
if(S.top==S.base)
cout
else
{
if(car.time
{
cout
i=0;//temp_time还是记录上次的值
}
else
{
temp_time=car.time;
i=1;
}
}
if(i==1)//正确的数据
{
if(c=='A')//到达
carin(S,Q,car);
else if(c=='D')
{
if(S.top==S.base);
else
carleave(S,Q,car);
}
}
j=1;
}
if(j==0)//第一次输入数据不需要检测
{
if(c=='A')//到达
carin(S,Q,car);
else if(c=='D')
{
if(S.top==S.base)
cout
else
carleave(S,Q,car);
}
j=1;
temp_time=car.time;
}
if(c=='E')
{
cout
break;
}
} return 0;}
四. 调试分析
1.本次作业是设计停车场的管理系统,就需要判断车牌号,及时间的输入的正确性,输入的数据有比较严格的要求,必须符合实际。因此对数据需要多次判断。2.处理车辆到达模块和处理车辆离开模块其空间复杂度为O(m*n);3.本程序循环用的很多,找车,排队,等等。4.主程序设计的有点乱。
五
用户手册
1.运行,按屏幕提示输入车辆信息;
2.回车显示车辆在停车场或停车道的信息; 3.输入E,则退出。
六. 测试结果
七 附录
#include