1. 停车场管理系统
长沙师范23物联401原创
1) 问题描述
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次从停车场最里面向大门口停放(最先到达的第一辆车停放在停车场的最里面),若停车场内一停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出停车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入停车场,每辆停放在停车场内的车在它离开时必须按它停留时间长短交纳费用。如果停留在便道上的车未进车场就要离去,允许其离去,不收停车费,并且其他在便道上等待的车辆的次序不变,试设计这样一个停车场模拟管理程序。
2) 基本功能
a. 单个车辆入站。
当系统正常投入运行后,会有零散的车辆进进出出,因此,设计一个函数实现单个车辆入站。
b.车站内信息实时显示。
车站内信息包括两个部分:停车场内停放的车辆以及在外面通道上等停的车辆。
c.车辆出站。
当停车场内车辆出站后,检查通道上是否有车等停,如果有,则要把排在最前面的车调入停车场内。
d. 如果有车辆离去,计算其停车费用
#include"malloc.h"
#define maxsize 100
#include<stdlib.h>
#include<string.h>
int n,cost,j=0,k=0,l=0;//全局变量 n表示用户输入停车场停放的车 cost用户输入每小时停车的费用
//停在停车场中的车(通过顺序栈实现)
typedef struct
{
int number;//车号
int arr_time;//抵达时间
}CarNode;
//顺序栈实现停车场
typedef struct{
CarNode data[maxsize];//停车场最大可以停放100量汽车
int top;
}StopCar;
//链队实现排队进停车场
typedef struct WaitCar{
CarNode data;//等候区最大可以停放100量汽车
struct WaitCar *next;
}WaitCar;
typedef struct{
WaitCar *front;//队头
WaitCar *rear; //队尾
}WaitCarLink;
//顺序栈实现给临时出来的汽车让路
typedef struct{
CarNode data[maxsize];//临时停车最大可以停放100量汽车
int top;
}TemporaryCar;
void InitStopCar(StopCar *);//停车场初始化
void InitWaitCarLink(WaitCarLink *&);//初始化等候区
void InitTemporaryCar(TemporaryCar *);//临时停车初始化
void EnQueue(StopCar *,WaitCarLink *,int ,int );//入库函数
void DepartureCar(StopCar *,int,int );//离开函数
int IfTheCar(StopCar *,int);//判断是否有输入的这辆车
void WaitToStop(StopCar *,WaitCarLink *,int);//将便道上的车进入车库
void DisplayStack(StopCar *);
void DisplayQueue(WaitCarLink *);
int main()
{
int m=1;
char flag;
int car,time=1;//car车牌号 time存放时间,初始化
StopCar sc;//停车场;
InitStopCar(&sc);
WaitCarLink *wcl;
InitWaitCarLink(wcl);
printf("请输入停车场可以停放的最大车辆,以及停车每小时收取的费用:");
scanf("%d %d",&n,&cost);
getchar();
while(m)
{
printf("\n输入车的信息选择功能");
printf("\n1 汽车进车场\n");
printf("2 汽车出车场\n");
printf("3 查看停车场\n");
printf("4 退出程序\n");
printf("请选择(1,2,3,4): ");
scanf("%c",&flag);
switch(flag)
{
case '1':
printf("\n请输入车的车牌号,车的时间(进站时间,出站时间)(用空格间隔):");
scanf("%d %d",&car,&time);
EnQueue(&sc,wcl,car,time);break; //车进入停车场
case '2':
printf("\n请输入车的车牌号,车的时间(进站时间,出站时间)(用空格间隔):");
scanf("%d %d",&car,&time);
DepartureCar(&sc,car,time);
WaitToStop(&sc,wcl,time);
break;
case '3':
DisplayStack(&sc);
DisplayQueue(wcl);
break;
case '4':
m=0;
break;
default:
printf("Input error!\n");
break;
}
while (flag != '\n')
scanf("%c",&flag);
}
return 0;
}
/*初始化*/
void InitStopCar(StopCar *sc)//停车场初始化
{
sc->top=-1;
}
//车进入停车场
void InitWaitCarLink(WaitCarLink *&wcl)
{//等候区初始化
wcl=(WaitCarLink *)malloc(sizeof(WaitCarLink));
if(wcl->front!=NULL)
{
wcl->rear=wcl->front=NULL;
}
}
void InitTemporaryCar(TemporaryCar *tc)//临时停车初始化
{
tc->top=-1;
}
//车子进入临时通道or直接进入库
void EnQueue(StopCar *S,WaitCarLink *W,int number,int time)
{
WaitCar *p;
p=(WaitCar*)malloc(sizeof(WaitCar));
if((S->top+1)<n)//车库没有满
{
S->data[S->top+1].number=number;
//strcpy(sc->data[sc->top+1].car,car);
S->data[S->top+1].arr_time=time;
S->top++;
printf("\n车牌号是%d的车停的位置是:%d\n",number,S->top+1);
}
else//满了,进便道
{
p->data.number=number;
p->data.arr_time=time;
p->next=NULL;
if(W->rear==NULL)
W->front=W->rear=p;
else
{
W->rear->next=p;
W->rear=p;
}
printf("车牌号是%d的车在便道上的位置是%d\n",number,j+1);
j++;
}
}
//出栈时从便道进入车库
void WaitToStop(StopCar *sc,WaitCarLink *wcl,int time)//将便道上的车进入车库,车库栈sc,便道队列wcl
{
if(wcl->front==NULL)//便道没有车
printf("便道没有车!\n");
else{
//便道上的车出队
WaitCar *S;//这个等待的车设为S
S=wcl->front;//S指向队头
wcl->front=S->next;//S指向的队头出队
if(wcl->rear==S)//如果S是最后一个元素,则让队尾指针指向队头,防止变成野指针
wcl->rear=wcl->front;
wcl->rear=NULL;
printf("%d出便道成功!\n",S->data.number);
//进栈
sc->data[sc->top+1].number=S->data.number;//车牌号
sc->data[sc->top+1].arr_time=time;//时间
printf("%d进库成功!\n",S->data.number);
k++;
sc->top++;
}
}
void DepartureCar(StopCar *sc,int car,int time)//离开函数
{
//先确定有没有车和有没有出库的车,如果有,则前面的车出栈到临时的栈里,等需要出来的车出来之后,在顺序进栈
TemporaryCar tc;//临时停车临时栈
InitTemporaryCar(&tc);
int timesum;//计算时间
//int i=sc->top;// 如果一辆车,sc的top是0,,,,top怎么改变,先减在加,可以变
if(sc->top==-1)
printf("车库没有车!无法离开\n");
else if(IfTheCar(sc,car))
{
printf("车库里没有此车!\n");
}
else
{
while(sc->data[sc->top].number!=car)//j将所需移动之前的车移入临时栈
{
tc.data [tc.top+1].number=sc->data[sc->top].number;
tc.data [tc.top+1].arr_time=sc->data[sc->top].arr_time;
tc.top++;
sc->top--;
}
timesum=time-sc->data[sc->top].arr_time;
printf("车牌是%d在停车停留的时间是%d,停车费用是%d\n",sc->data[sc->top].number,timesum,timesum*cost);
sc->top--;//将此车出栈
while(tc.top!=-1)
{
sc->data[sc->top+1].number=tc.data[sc->top].number;
sc->data[sc->top+1].arr_time=tc.data[sc->top].arr_time;
tc.top--;
sc->top++;
}
}
}
int IfTheCar(StopCar *sc,int car)//判断是否有输入的这辆车
{
int i=0;
while(i<sc->top+1)
{
if(sc->data[i].number==car)
return 0;
i++;
}
return 1;
}
void DisplayStack(StopCar *p)
{
printf("停车场停车信息如下(先进的车辆在最右端):\n");
if(p->top==-1)
printf("停车场中没有车");
while(p->top!=-1)
{
printf("%d",p->data[p->top].number);
p->top--;
}
p->top++;
printf("\n");
}
//展示临时车道的车牌号
void DisplayQueue(WaitCarLink *s)
{
int a;
printf("临时通道停车信息如下(先进的车辆在最右端):\n");
if(s->front==NULL)
printf("临时通道内没车");
WaitCar*p;
p=s->front;//S指向队头
a=l;
while(p!=NULL)
{
printf("%d ",p->data.number);
p=p->next;
l++;
}
l=a;
printf("\n");
}
。