问题描述:
设停车场是一个可以停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆汽车停放在车场的最北端),若车场内已停满n辆车,那么后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆汽车要离开时,在它之后进入的车辆必须先退出停车场按顺序开入临时停放道为其让路,待其开出大门外后,再按原次序进入车场,每辆停放在停车场的汽车在它离开停车场时必须按其停留的时间长短缴纳费用(从进入停车场开始计费)。
示意图:

具体代码:
#include<iostream>
#include<malloc.h>
using namespace std;
#define N 3
#define M 4
#define Price 2
typedef struct{
int CarNo[N];
int CarTime[N];
int top;
}SqStack;
typedef struct{
int CarNo[M];
int front,rear;
}SqQueue;
void InitStack(SqStack *&s){
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
bool StackEmpty(SqStack *s){
return(s->top==-1);
}
bool StackFull(SqStack *s){
return(s->top==N-1);
}
bool Push(SqStack *&s,int e1,int e2){
if(s->top==N-1){
return false;
}
s->top++;
s->CarNo[s->top]=e1;
s->CarTime[s->top]=e2;
return true;
}
bool Pop(SqStack *&s,int &e1,int &e2){
if(s->top==-1){
return false;
}
e1=s->CarNo[s->top];
e2=s->CarTime[s->top];
s->top--;
return true;
}
void DispStack(SqStack *s){
for(int i=s->top;i>=0;i--){
cout<<" "<<s->CarNo[i];
}
cout<<endl;
}
void InitQueue(SqQueue *&q){
q=(SqQueue *)malloc(sizeof(SqQueue));
q->front=q->rear=0;
}
bool QueueEmpty(SqQueue *&q){
return(q->front==q->rear);
}
bool QueueFull(SqQueue *&q){
return((q->rear+1)%M==q->front);
}
bool enQueue(SqQueue *&q,int e){
if((q->rear+1)%M==q->front){
return false;
}
q->rear=(q->rear+1)%M;
q->CarNo[q->rear]=e;
return true;
}
bool deQueue(SqQueue *&q,int &e){
if(q->front==q->rear){
return false;
}
q->front=(q->front+1)%M;
e=q->CarNo[q->front];
return true;
}
void DispQueue(SqQueue *q){
int i=(q->front+1)%M;
cout<<" "<<q->CarNo[i];
while((q->rear-i+M)%M>0){
i=(i+1)%M;
cout<<" "<<q->CarNo[i];
}
cout<<endl;
}
int main(){
int comm,i,j;
int no,e1,time,e2;
SqStack * St,* St1;
SqQueue * Qu;
InitStack(St);
InitStack(St1);
InitQueue(Qu);
for(;;){
cout<<"请输入指令:"<<endl;
cout<<" 1:车到达"<<endl;
cout<<" 2:车离开"<<endl;
cout<<" 3:停车场状态"<<endl;
cout<<" 4:候车场状态"<<endl;
cout<<" 5:查询停车位与候车位数量以及停车场费用明细"<<endl;
cout<<" 0:结束系统"<<endl;
cout<<"你的指令为:";
cin>>comm;
cout<<"----------------------------------------"<<endl;
switch(comm){
case 1:{
cout<<" 车号:";
cin>>no;
cout<<" 到达时间:";
cin>>time;
if(!StackFull(St)){
Push(St,no,time);
cout<<" 停车场的位置是:"<<St->top+1<<endl;
}
else
{
if(!QueueFull(Qu)){
enQueue(Qu,no);
cout<<" 候车场的位置是:"<<Qu->rear<<endl;
}else{
cout<<" 停车场和候车场都已满,无法停车,请离开"<<endl;
}
}
break;
}
case 2:{
cout<<" 车号:";
cin>>no;
cout<<" 离开时间:";
cin>>time;
for(i=0;i<=St->top&&St->CarNo[i]!=no;i++);
if(i>St->top){
cout<<" 未找到该编号的汽车"<<endl;
}else{
for(j=i;j<=St->top;j++){
Pop(St,e1,e2);
Push(St1,e1,e2);
}
Pop(St,e1,e2);
cout<<" 车号为"<<no<<"的汽车停车的费用为:"<<(time-e2)*Price<<endl;
while (!StackEmpty(St1)){
Pop(St1,e1,e2);
Push(St,e1,e2);
}
if(!QueueEmpty(Qu)){
deQueue(Qu,e1);
Push(St,e1,time);
}
}
break;
}
case 3:{
if(!StackEmpty(St)){
cout<<" 停车场中的车辆:";
DispStack(St);
}
else{
cout<<" 停车场现在无车"<<endl;
}
break;
}
case 4:{
if(!QueueEmpty(Qu)){
cout<<" 候车场中的车辆:";
DispQueue(Qu);
}
else{
cout<<" 候车场现在无车"<<endl;
}
break;
}
case 5:{
cout<<"停车位有:"<<N<<"个"<<endl;
cout<<"候车位有:"<<M-1<<"个"<<endl;
cout<<"停车及候车的费用为:"<<Price<<"元每小时"<<endl;
cout<<"----------------------------------------"<<endl;
break;
}
case 0:{
exit(1);
}
}
}
return 1;
}
运行结果:



本文介绍了一种狭长通道停车场的管理系统,通过栈和队列数据结构实现车辆的有序停放和离开,支持车辆到达、离开、查看停车场和候车场状态等功能。系统详细描述了车辆的进出流程,包括在停车场满时将车辆引导至候车场,以及车辆离开时的费用计算。
3万+

被折叠的 条评论
为什么被折叠?



