版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/40364159
停车场管理
【问题描述】
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供
汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向
南排列(大门在最南端),若停车场内已停满n辆汽车,则后来的汽车
只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即
可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出
车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,
每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费
用。试为停车场编制按上述要求进行管理的模拟程序。
【基本要求】
以栈模拟停车场,以队列模拟车场外的便道。栈以顺序结构实现,
队列以链表结构实现。
每一组输入数据包括:汽车“到达”或“离去”信息、汽车牌照号码
以及到达或离去的时刻。
输出信息:若是车辆到达,则输出汽车在停车场内或便道上的停车
位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费
用(在便道上停留的时间不收费)。
【测试数据】
设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’表示输入结束。
【实现提示】
需另设一个栈,临时停放为给要离去的汽车让路而从停车场退出来
的汽车,也可用顺序存储结构实现。输入数据按到达或离去的时刻有序。
栈中每个元素表示一辆汽车,包含两个数据项:汽车的牌照号码和进入
停车场的时刻。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <sstream>
#include <stdlib.h>
#include <malloc.h>
using namespace std;
struct BUS
{
int num;
int intime;
int outtime;
}bus[10000],buss[1000];
struct seq//停车场
{
int bus[10000];
int top;
};
struct seqq
{
int buss[1000];
int topp;
};
struct Biandao
{
int data;
int time;
int num;
struct Biandao *next;
};
struct biandao
{
Biandao *first;
Biandao *rear;
}biandao;
int main()
{
int x,t;
int n;
printf("请输入停车场车位数量n:\n");
cin >> n;
char a[5];
seq s;
s.top=0;
seqq ss;
ss.topp=0;
biandao.first=NULL;
biandao.rear =NULL;
int num = 0;
printf("请输入操作: 车牌号: 时间:\n");
while (cin >> a)
{
if ( !strcmp (a,"A"))
{
cin>>x>>t;
if ( s.top >= n )//如果停车场已满
{
Biandao *p;
p=(Biandao *)malloc(sizeof(Biandao));
p->data=x;
p->time=t;
num++;
p->next=NULL;
if (biandao.rear != NULL)
biandao.rear->next=p;
biandao.rear=p;
if (biandao.first==NULL)
biandao.first=p;
printf("在便道的第%d位置\n",num);
}
else//停车场未满
{
bus[s.top].num=x;
bus[s.top].intime=t;
printf("停车场的第%d位置\n",s.top+1);
s.top++;
}
}
else if (!strcmp (a,"D"))
{
cin>>x>>t;
if (s.top == 0)
{
printf("输入错误 重新输入\n");
}
else
{
int nu;
for (int i=0;i<=s.top;i++)
{
if (bus[i].num == x)
{
nu=i;
}
}
bus[nu].outtime=t;
printf("停留时间%d, 费用 %d \n", bus[nu].outtime - bus[nu].intime ,bus[nu].outtime - bus[nu].intime );
for (int i=nu ;i<=s.top;i++)//出停车站
{
bus[i]=bus[i+1];
}
s.top--;
if (biandao.first!=NULL)//如果便道上有车
{
bus[s.top].num = biandao.first->data;
bus[s.top].intime = t;
s.top++;
num--;
Biandao *q;
q=biandao.first;
biandao.first=biandao.first->next;
free(q);
}
}
}
else if(!strcmp (a,"E"))
{
break;
}
else
{
printf("输入有误 重新输入\n");
}
}
return 0;
}
留言