第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;
}
}
}本回答被提问者采纳