/* 文件名:depot.c
* 作者:王永超;
* 版本:1.0;
* 摘要:停车管理系统;
*/
#include<stdio.h>
#include<stdlib.h>
/* 文件名:depot.c
* 作者:王永超
* 摘要:实现停车管理系统的主函数
*/
#include"Stack.h"
#include"Queue.h"
#include"input.h"
//队列遍历函数
void visit(ElemType e)
{
printf("%d\n",e);
}
//栈的遍历函数
State search(ElemType e)
{
printf("%d\n",e);
}
//停车函数
void stopCar(SeStack *stop,QueueLink *wait,ElemType e)
{
int stopsize;//停车场空位
int waitsize;//等待车辆数
int flag;//选择标志符
printf("**********************\n");
printf("欢迎使用停车管理服务:\n");
printf("\n");
//查询停车场空位
stopsize = STACK_INIT_SIZE-SeStackLength(stop);
printf("停车场空位:%d\n",stopsize);
if(stopsize > 0)//判断是否能进行停车
{
printf("你可以进行停车!");
printf("请输入你的车牌号(三位车牌号码):");
e = gain();
while(e == 0)
{
printf("请输入你的车牌号(三位车牌号码):");
e = gain();
}
//限制车牌为三位
if(e <100 || e > 999)
{
printf("本车牌不合法,有疑问请联系车管所!\n");
printf("\n");
}else{
//判断套牌问题
ElemType *m;
m = stop->base;
//查询栈中相同车牌号
while(m < stop->top)
{
if(*m == e)
{
printf("停车场内发现相同的车牌号!\n");
printf("请下车接受检查!\n");
printf("\n");
return;
}
m++;
}
printf("正在进行停车:\n");
if(SeStackPush(stop,e))//判断栈是否添加成功
{
printf("停车成功!\n");
}else{
printf("停车失败!\n");
}
}
}else{
//查询前方等待停车的人数
printf("停车场已满!\n");
waitsize = QueueLength(wait);
printf("前方等待人数:%d\n",waitsize);
printf("是否进入等待队列(是1/否2):");
//判断选项
flag = gain();
while(flag == 0)
{
printf("是否进入等待队列(是1/否2):");
flag = gain();
}
if(1 == flag)
{
//输入车牌
printf("请输入你的车牌号(三位车牌号码):\n");
e = gain();
while(e == 0)
{
printf("请输入你的车牌号(三位车牌号码):\n");
e = gain();
}
if(e <100 || e > 999)
{
printf("本车牌不合发,有疑问请联系车管所!\n");
}else{
//停车场套牌查询
ElemType *m;
m = stop->base;
while(m < stop->top)//队列的相同车牌
{
if(*m == e)
{
printf("停车场内发现相同的车牌号!\n");
printf("已报警处理,请下车接受检查!\n");
return;
}
m++;
}
//等待队列套牌查询
QueuePtr l;
l = wait->front->next;
while(l)
{
if(l->date == e)
{
printf("等待队列里发现相同车牌号!\n");
printf("已报警处理,请下车接受检查!\n");
return;
}
l = l->next;
}
printf("正在进入等待队列:\n");
if(QueueEn(wait,e))//进入等待队列
{
printf("停车等待中!\n");
printf("\n");
}else{
printf("进入等待队列失败!\n");
}
}
}else if(0 == flag)
{
printf("正在离开!\n");
printf("\n");
}else{
printf("未开通该服务!\n");
}
}
}
//取车函数
void pickCar(SeStack *stop,SeStack *pick,QueueLink *wait,ElemType e)
{
printf("欢迎使用取车服务:");
//查询停车场内是否有车辆(判空)
if(SeStackEmpty(stop))
{
printf("停车场内没有车辆!\n");
}else{
printf("请输入你的车牌号(三位车牌号):");
e = gain();
while(e == 0)
{
printf("请输入你的车牌号(三位车牌号):\n");
e = gain();
}
if(e < 100 || e > 999)
{
printf("本车牌不合法,有疑问请联系车管所!\n");
}else{
//查询车辆所在的位置
int position;//车辆的位置
int control;//调度车辆
ElemType *p;
position = SeStackLength(stop);
printf("%d",position);
p = stop->base;
//遍历查找车辆位置
while(p < stop->top)
{
if(*p == e)
{
break;
}
position--;
p++;
}
//如果停车场内没有该车辆
if(stop->top == p)
{
printf("停车场内没有该车辆!\n");
}else{
printf("车辆的位置为:%d\n",position);
//取车
if(1 == position)//车辆在第一位,直接取走
{
SeStackPop(stop,&e);//取走第一辆车
printf("车牌号%d的车辆驶离停车场!\n",e);
//如果等待队列有车入一辆
if(!QueueEmpty(wait))//判断等待队列是否有车
{
//等待队列有车
QueueDelete(wait,&e);//等待队列第一辆车的驶离
printf("车牌号为%d的车辆驶入停车驶入停车场!\n",e);
SeStackPush(stop,e);//停车场新停入车辆
}
}else{ //车辆不在门口位置
//将车辆前边的转入调度车场
for(control = 1;control < position;control++ )
{
SeStackPop(stop,&e);
SeStackPush(pick,e);//将停车场的车驶入调度车场
}
//调度完毕取车
SeStackPop(stop,&e);//取车
printf("车牌号%d的车辆驶离停车场!\n",e);
//将调度车场车驶回停车场
for(control = 1;control < position;control++ )
{
SeStackPop(pick,&e);
SeStackPush(stop,e);//将停车场的车驶入调度车场
}
//判断等待队列是否有车,有则驶入一辆
if(!QueueEmpty(wait))//判断等待队列是否有车
{
//等待队列有车
QueueDelete(wait,&e);//等待队列第一辆车的驶离
printf("车牌号为%d的车辆驶入停车驶入停车场!\n",e);
SeStackPush(stop,e);//停车场新停入车辆
}
}
}
}
}
}
int main(void)
{
ElemType e;//车辆信息
int count;//存放选择项代码
SeStack *stop; //停车场栈
SeStack *pick; //调度场栈
QueueLink *wait;//等待场队列
//创建队列和栈
stop = (SeStack*)malloc(sizeof(SeStack));
pick = (SeStack*)malloc(sizeof(SeStack));
wait = (QueueLink*)malloc(sizeof(QueueLink));
if(!stop)
{
printf("创建失败!\n");
exit(0);
}
if(!pick)
{
printf("创建失败!\n");
exit(0);
}
if(!wait)
{
printf("创建失败!\n");
exit(0);
}
//初始化*stop和*pick
stop = SeStackInit(stop);
pick = SeStackInit(pick);
wait = QueueInit(wait);
//主界面
while(1)
{
printf("----------------------------------\n");
p