停车场管理系统的代码.docx
- 文档编号:18480906
- 上传时间:2023-08-18
- 格式:DOCX
- 页数:13
- 大小:17.22KB
停车场管理系统的代码.docx
《停车场管理系统的代码.docx》由会员分享,可在线阅读,更多相关《停车场管理系统的代码.docx(13页珍藏版)》请在冰点文库上搜索。
停车场管理系统的代码
设停车场是一个可停放n辆车的狭长通道,且只有一个大门可供汽车进出。
在停车场内,汽车按到达的先后次序,由北向南依次排列(假设大门在最南端)。
若车场内已停满n辆车,则后来的汽车要在门外的便道上等候,当有车开走时,便道上的第一辆车即可开入。
当停车场内某辆车要离开时,在它之后进去的车辆必须先推出车场为它让路,待该辆车开出大门以后,其他车辆再按原次序返回车场。
每辆车离开停车场时,应按其停留时间的长短交费(在便道上停留的时间不收费)。
试编写程序,模拟上述管理过程。
要求以顺序栈模拟停车场,以链队列模拟便道。
从终端读入汽车到达或离去的数据,每组数据包括三项:
(1)是“到达”还是“离去”
(2)汽车牌照号码;(3)“到达”或“离去”的时刻。
与每组输入信息相应的输出信息为:
如果是到达的车辆,则输出其在停车场中或便道上的位置;如果是离去的车辆,则输出其在停车场中停留的时间和应缴的费用。
(需另设一个栈,临时停放为让路而从车场退出的车。
)
#include
#defineM5
usingnamespacestd;
typedefintDatatype;
typedefstruct
{
Datatypebianhao[M];
inttop;
inth[M];//时
intm[M];//分
ints[M];//秒
}Seqstack;//停车场栈的定义
typedefstructNode//便道结点,表示一辆汽车
{
Datatypebianhao;
structNode*next;
}node;
typedefstruct
{
node*front;
node*rear;
intcount;
}biandao;//便道
//停车场顺序栈初始化
voidInitSeqstack(Seqstack*t)
{
t->top=-1;
}
//进栈,即进入停车场
intPush(Seqstack*t,intx,inth,intm,ints)
{
if(t->top==M-1)
return0;//停车场栈已满
t->top++;
t->bianhao[t->top]=x;
t->h[t->top]=h;
t->m[t->top]=m;
t->s[t->top]=s;
return1;
}
//出栈,即离开停车场
intPop(Seqstack*t,int*x,int*h,int*m,int*s)
{
if(t->top==-1)
return0;
else
{
*x=t->bianhao[t->top];
*h=t->h[t->top];
*m=t->m[t->top];
*s=t->s[t->top];
t->top--;
return1;
}
}
//查找某牌照的车在停车场中的位置,若找到则返回其位置,否则返回-1
intFind(Seqstackt,intx)
{
inti;
for(i=0;i<=t.top;i++)
{if(t.bianhao[i]!
=x)
continue;
else
break;
}
if(i>t.top)
return(-1);
else
return(i);
}
//判断停车场内是否已满
intIsSeqstackFull(Seqstackt)
{
if(t.top==M-1)
return1;
else
return0;
}
//判断停车场内是否已没有车辆
intIsSeqstackEmpty(Seqstackt)
{
if(t.top==-1)
return1;
else
return0;
}
//依次显示停车场内停放的所有车辆
voidShowSeqstack(Seqstackt)
{
inti;
if(t.top==-1)
cout<<"停车场内没有停放车辆"< else for(i=0;i<=t.top;i++) cout<<"牌照: "< } //队列初始化 intInitbiandao(biandao*Q) { Q->front=newnode; if(Q->front! =NULL) { Q->rear=Q->front; Q->front->next=NULL; Q->count=0; return(true); } elsereturn(false); } //入队操作,即当停车场满了的时候,再到达的车辆进去便道队列 intEnterbiandao(biandao*Q,intx) { node*NewNode; NewNode=newnode; if(NewNode! =NULL) { NewNode->bianhao=x; NewNode->next=NULL; Q->rear->next=NewNode; Q->rear=NewNode; Q->count++; return(true); } elsereturn(false); } //出队操作,即便道上的车辆从便道开出来 intDeletebiandao(biandao*Q,int*x) { node*p; if(Q->front==Q->rear) return(false); p=Q->front->next; Q->front->next=p->next; if(Q->rear==p) Q->rear=Q->front; *x=p->bianhao; free(p); Q->count--; return(true); } //判断便道队列是否为空 intIsbiandaoEmpty(biandaoQ) { if(Q.front==Q.rear) return(true); else return(false); //当count=0时,就空了 } //依次显示便道上停放的所有车辆 voidShowbiandao(biandaoQ) { node*p; p=Q.front->next; if(p==NULL) cout<<"便道上没有停放车辆! "< else while(p! =NULL) { cout<<"牌照: "< p=p->next; } }36 //计算停留时间差 doubleTime(inth1,intm1,ints1,inth2,intm2,ints2) { doublep,q; p=h1*3600+m1*60+s1-(h2*3600+m2*60+s2); q=(double)p/3600; return(q); } //计算停车费用 doubleCost(doublet,intu) { return(t*u); } //主函数 voidmain() { doubletime,cost; intch; intx,*y,z; inti,flag=1,h,m,s,unit_price,hh,mm,ss; y=newint; Seqstack*t;//定义停车栈 t=newSeqstack; InitSeqstack(t); biandao*Q;//定义便道队列 Q=newbiandao; Initbiandao(Q); Seqstack*r;//定义让路栈 r=newSeqstack; InitSeqstack(r); cout<<"请设置停车费用单价: (__元/小时)"< cin>>unit_price; while(flag) { cout<<"***************************"< cout<<"请选择命令: "< 到达"< 离开"< 依次显示停车场内停放车辆"< 依次显示便道上停放车辆"< 结束"< cout<<"***************************"< cin>>ch; switch(ch) { case1: cout<<"请输入到达的汽车牌照号码: "< cin>>x; if(IsSeqstackFull(*t)) { cout<<"停车场已满,请在便道等候! "< Enterbiandao(Q,x); cout<<"将此车停放在便道的"< "< } else { while (1) { cout<<"请输入到达的时间(例如,210823): "< cin>>h>>m>>s; if(h<0||h>23)continue;//输入数据不合法,回去重新输入 if(m<0||m>59)continue; if(s>-1&&s<61) break;//输入时间全部合法,退出循环 } Push(t,x,h,m,s); cout<<"将此车停放在停车场的"< } break; case2: if(IsSeqstackEmpty(*t)) cout<<"停车场内已没有车辆"< else { cout<<"请输入要离开的汽车的车牌照号码: "< cin>>x; if(Find(*t,x)==-1) cout<<"停车场内没有该汽车! "< else//有该号码的汽车 { while (1) { cout<<"请输入离开的时间(例如,235803): "< cin>>h>>m>>s; if(h<0||h>23)continue;//输入数据不合法,回去重新输入 if(m<0||m>59)continue; if(s>-1&&s<61) break;//输入时间全部合法,退出循环 } if(Find(*t,x)==t->top) { Pop(t,y,&hh,&mm,&ss);//要离开的车辆正好是最后一辆进入停车场的车 time=Time(h,m,s,hh,mm,ss); cout<<"停留时间为: "<
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 停车场 管理 系统 代码