c++ 停车场问题 谁能帮我写一下啊

那位高手能给我一个 分就这些 最好有注释

问题描述
设有一个可以停放n辆汽车的狭长停车场,它只有一个大门可以供车辆进出。车辆按到达停车场时间的早晚依次从停车场最里面向大门口停放(最先到达的第一辆车放在停车场最里面)。如果停车场已经放满n辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排在便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在它之后进入停车场的车必须先退出停车场为它让路,待其开出停车场后,这些车辆再依原来的次序进场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。如果停留在便道上的车未进入停车场就要离去,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆的次序。编制程序模拟该停车场的管理。
基本要求
(1) 输出每辆车到达后的停车位置(停车场或便道上),以及某辆车离开停车场时应缴纳的费用和它在停车场内停留的时间。
(2) 汽车模拟输入格式为:(到达/离去,汽车牌照号码,到达/l离去的时刻)
(3) 根据输入命令来决定车辆的进入或离开。
实现提示
停车场只有一个大门,因此可以用栈来模拟,而当栈满后,继续来到的车辆只能停在便道上,可知这可以用一个队列来模拟,先排队的车辆先离开便道进入停车场。由于排在停车场内的车辆可以离开停车场,并且要求在依原来的次序进入停车场,因此在一个栈和一个队列的基础上,还需要有一个地方(车辆规避所)保存为了让路离开停车场的车辆,很显然要用栈来模拟,因此要用到两个栈和一个队列。

第1个回答  推荐于2016-11-21
我写的这个是C的,用C++的话原理一样,就是申请存储空间改成new,scanf、printf改成cin、cout就行了。
具体实现方法是用2个栈(停车场)和1个队列(便道)的操作来实现的,你好好研究一下吧!
#include <stdio.h>
#include <stdlib.h>

#define MaxNum 5
#define WaitingNum 3
#define Price 2

typedef struct
{
int CarNum[MaxNum]; //车牌号
int CarTime[MaxNum]; //进场时间
int top;
}SqStack;
typedef struct
{
int CarNum[WaitingNum];
int front, rear;
}SqQueue;

void InitStack(SqStack * &s)
{
s = (SqStack *)malloc(sizeof(SqStack));
s->top = -1;
}

int StackEmpty(SqStack *s)
{
return(s->top == -1);
}

int StackFull(SqStack *s)
{
return(s->top == MaxNum - 1);
}

int Push(SqStack * &s, int Num, int Time)
{
if(s->top == MaxNum - 1)
return 0;
s->top++;
s->CarNum[s->top] = Num;
s->CarTime[s->top] = Time;
return 1;
}

int Pop(SqStack * &s, int &Num, int &Time)
{
if(s->top == -1)
return 0;
Num = s->CarNum[s->top];
Time = s->CarTime[s->top];
s->top--;
return 1;
}

void DispStack(SqStack *s)
{
int i;
for(i = s->top; i >= 0; i--)
printf("%d ", s->CarNum[i]);
printf("\n");
}

void InitQueue(SqQueue * &q)
{
q = (SqQueue *)malloc(sizeof(SqQueue));
q->front = q->rear = 0;
}

int QueueEmpty(SqQueue *q)
{
return(q->front == q->rear);
}

int QueueFull(SqQueue *q)
{
return((q->rear + 1)% WaitingNum == q->front);
}

int EnQueue(SqQueue *q, int e)
{
if((q->rear + 1) % WaitingNum == q->front)
return 0;
q->rear = (q->rear + 1) % WaitingNum;
q->CarNum[q->rear] = e;
return 1;
}

int DeQueue(SqQueue * &q, int &e)
{
if(q->front == q->rear)
return 0;
q->front = (q->front + 1) % WaitingNum;
e = q->CarNum[q->front];
return 1;
}

void DispQueue(SqQueue *q)
{
int i;
i = (q->front + 1) % WaitingNum;
printf("%d", q->CarNum[i]);
while((q->rear - i + WaitingNum) % WaitingNum > 0)
{
i = (i + 1) % WaitingNum;
printf("%d", q->CarNum[i]);
}
printf("\n");
}

void main()
{
int comm;
int Num, e1, Time, e2;
int i, j;
SqStack *St, *St1;
SqQueue *Qu;
InitStack(St);
InitStack(St1);
InitQueue(Qu);
while(true)
{
printf("请选择车辆状态:\n1.车辆到达\n2.车辆离开\n3.停车场当前停车情况\n4.候车场当前停车情况\n0.退出程序\n");
scanf("%d", &comm);
if(comm == 1 || comm == 2)
{
printf("请分别输入(到达或离开的)车辆编号和(进场或入场)时间:\n");
scanf("%d%d", &Num, &Time);
}
switch(comm)
{
case 1:
if(!StackFull(St))
{
Push(St, Num, Time);
printf("此车进入停车场第 %d 号车位\n", St->top + 1);
}
else
{
if(!QueueFull(Qu))
{
EnQueue(Qu, Num);
printf("停车场目前已满,请稍后!");
printf("此车进入候车场第 %d 号\n", Qu->rear);
}
else
printf("候车场已满,请另寻其他停车场吧!");
}
break;
case 2:
for(i = 0;i <= St->top && St->CarNum[i] != Num; i++);
if(i > St->top)
printf("停车场没有该编号的车,请确定是否输入正确\n");
else
{
for(j = i; j <= St->top; j++)
{
Pop(St, e1, e2);
Push(St1, e1, e2);
}
Pop(St, e1, e2);
printf("编号%d汽车,停车费用为:%d\n", Num, (Time - e2) * Price);
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))
{
printf("停车场现有如下车辆:\n");
DispStack(St);
}
else
printf("停车场目前没有车辆!\n\n");
break;
case 4:
if(!QueueEmpty(Qu))
{
printf("候车场现在有如下车辆:\n");
DispQueue(Qu);
}
else
printf("候车场目前没有车辆!\n\n");
break;
case 0:
exit(0);
break;
default:
printf("你的选择有误,请重新选择!\n\n");
break;
}
}
}本回答被提问者采纳