#include
#include
typedef struct Car{//用Car结构体模拟车
int brand;//车牌号(这里采用整数型)
int hour;//车辆进停车场的时间(小时)
int min;//车辆进停车场的时间(分钟)
struct Car* next;//指针区域,指向下一辆车(仅在链队列中有用!)
int mark;
}CAR,*PCAR;
typedef struct Stack{//用Stack结构体模拟停车场的车库
struct Car cars[5];//车数组,数组里面的每个元素表示一辆车,车库中只能停5辆车
int top;//栈顶指针,另外,当栈顶指针为-1时,表示栈为空
}STACK,*PSTACK;
typedef struct Queue{//用Queue结构体模拟停车场中的便道,当车库满时,那么你只能把车停到便道
PCAR front;//头指针,指向对头元素
PCAR rear;//尾指针,指向队尾元素
}QUEUE,*PQUEUE;
typedef struct Park{//用Park结构体模拟这个停车场
PSTACK stack;
PQUEUE queue;
struct Car monitor[5];//记录仪,如果有一个车辆进入停车场成功,那么把这个车辆的车牌号放到该数组中,因为栈无法进行遍历操作,但是数组可以
int num;//记录车库中车辆的数目,栈中车的数量,也是monitor的数量
}PARK,*PPARK;
//相关函数(方法)
void init_Stack(PSTACK);//初始化一个栈,那么你需要把这个栈的结构体指针给我传进来
void init_Queue(PQUEUE);//初始化一个链队列,那么你需要把这个队列的结构体指针给我传进来
int push_Stack(PSTACK,CAR);//压栈函数,你要压入哪个栈,那么你把这个栈传给我,你要压栈哪辆车,你就把车的指针传给我,压栈成功返回1否则返回0
CAR pop_Stack(PSTACK);//弹栈操作,将栈帧所指的元素,也就是栈顶元素弹出栈中,并返回一个车
void in_Queue(PQUEUE,PCAR);//入队操作,传给我要入的队,和要入队的元素
CAR out_Queue(PQUEUE);//出队操作,把要出队的队传给我,返回这个对头元素
int Stack_is_empty(STACK);//判断栈是否为空,为空返回1,不空返回0
int Queue_is_empty(QUEUE);//判断队列是否为空,为空返回1,不空返回0
int Stack_is_full(STACK);//判断栈是否已满,已满返回1,未满返回0;
int length_Stack(STACK);//计算栈中元素个数
int length_Queue(QUEUE);//计算队列中元素的个数
void init_Park(PPARK);//初始化停车场
void in_Park(PPARK);//进停车场函数
void out_Park(PPARK);//出停车场函数
void show_Park(PARK);//展示一下停车场中的车牌号元素
int main(void){
PPARK p=(PPARK)malloc(sizeof(PARK));
init_Park(p);
int l=-1;
printf("********进车(1)********");
printf("n");
printf("********出车(2)********");
printf("n");
printf("********显示(3)********");
printf("n");
printf("********退出(0)********");
printf("n");
printf("请输入功能号:");
scanf("%d",&l);
printf("n");
while(l!=0){
if(l==1){
in_Park(p);
}
if(l==2){
out_Park(p);
}
if(l==3){
show_Park(*p);
}
if(l==0){
break;
}
l=-1;
printf("********进车(1)********");
printf("n");
printf("********出车(2)********");
printf("n");
printf("********显示(3)********");
printf("n");
printf("********退出(0)********");
printf("n");
printf("请输入功能号:");
scanf("%d",&l);
printf("n");
}
}
void init_Stack(PSTACK p){
p->top=-1;//将栈帧初始化为-1
}
int Stack_is_empty(STACK s){
if(s.top==-1)
return 1;
return 0;
}
int Stack_is_full(STACK s){
if(s.top==4)//以为用数组模拟栈,数组的长度设为5,所以下标最大到4,如果栈帧达到4,则说明栈已满
return 1;
return 0;
}
int push_Stack(PSTACK p,CAR c){
if(Stack_is_full(*p)){
printf("车库已满,后续车辆请进入便道");
return 0;//压栈未成功返回0;
}
//程序能执行到这里,说明栈没有满,还可以继续压栈
p->top++;//栈顶指针加1
p->cars[p->top]=c;//将要压栈的车辆放到栈帧所指向的位置
return 1;//压栈成功返回1
}
CAR pop_Stack(PSTACK p){
if(Stack_is_empty(*p)){
printf("车库中没有车辆,无法继续出库");
}
CAR temp =p->cars[p->top];//先暂存一下要出栈的这个车辆,以便返回
p->top--;
return temp;//返回这个车辆
}
void init_Queue(PQUEUE p){
PCAR pcar=(PCAR)malloc(sizeof(CAR));//头结点,无实际意义
p->front=p->rear=pcar;//头指针和尾指针都指向头结点
pcar->next=NULL;
}
void in_Queue(PQUEUE p,PCAR c){
p->rear->next=c;//尾指针的下一个是要入队的元素,则把它接在尾指针的后面
p->rear=c;//更新尾结点
c->next=NULL;
c->mark=1;
}
CAR out_Queue(PQUEUE p){
if(Queue_is_empty(*p)){
printf("便道中没有车辆了");
}
if(p->front->next==p->rear){
PCAR out =p->front->next;//out 存储这个要出队的元素
out->next=NULL;
p->front=p->rear;
return *out;
}
PCAR out =p->front->next;//out 存储这个要出队的元素
p->front->next=p->front->next->next;
out->next=NULL;
return *out;
}
int Queue_is_empty(QUEUE q){
if(q.rear==q.front){
return 1;
}
return 0;
}
int length_Stack(STACK s){
return s.top+1;
}
int length_Queue(QUEUE q){
PCAR psearch=q.front;
int len=0;
while(psearch!=NULL){
psearch=psearch->next;
len++;
}
return len;
}
void init_Park(PPARK p){
p->num=0;
PSTACK parkstack=(PSTACK)malloc(sizeof(STACK));
init_Stack(parkstack);
PQUEUE parkqueue=(PQUEUE)malloc(sizeof(QUEUE));
init_Queue(parkqueue);
p->stack=parkstack;
p->queue=parkqueue;
}
void in_Park(PPARK p){
PCAR need=(PCAR)malloc(sizeof(CAR));//开辟车辆结点的存储空间
if(p->num<5){
//brand容错判断
printf("请输入你要进入停车场的车牌号:");
scanf("%d",&need->brand);
int flag =0;//默认没有相同
for(int i=0;i<=p->stack->top;i++){//扫描记录仪,查找一下有没有相同的车牌号
if(need->brand==p->monitor[i].brand){
//printf("%d",p->monitor[i].brand);
flag=1;//如果有,显示入库失败
printf("车库中有该车辆,入库失败!");
printf("n");
return;
}
}
//time容错判断
printf("请输入小时:");
scanf("%d",&need->hour);
while(1) {//进入循环
if (need->hour < 0 || need->hour > 24) {//如果小时小于0大于24,则显示非法输入
printf("输入时间不合法,请重新输入");
printf("请输入小时:");
scanf("%d",&need->hour);
continue;
}
if(p->stack->top>=0&&need->hourstack->cars[p->stack->top].hour){//后入库的车辆的时间不能小于前一辆入库的时间
printf("输入时间不合法,请重新输入");
printf("请输入小时:");
scanf("%d",&need->hour);
continue;
}
else{
break;
}
}
printf("请输入分钟:");
scanf("%d",&need->min);
while(1) {
if(need->min<0||need->min>60){//如果分钟小于0大于60,则显示非法输入
printf("输入时间不合法,请重新输入");
printf("请输入分钟:");
scanf("%d",&need->min);
continue;
}
if(need->hour==p->stack->cars[p->stack->top].hour&&need->minstack->cars[p->stack->top].min){//如果后入库的车辆的小时和前一辆车的小时相等那么他的分钟不能小于前一辆车的分钟
printf("输入时间不合法,请重新输入");
printf("请输入分钟:");
scanf("%d",&need->min);
continue;
}
else{//如果程序能执行到这里,说明上面情况都不成立,时间合法
break;
}
}
if(flag==0) {//如果没有相同,则可以入库
push_Stack(p->stack, *need);//将该车辆压到栈中
p->num++;
p->monitor[p->stack->top] = *need;//把该车牌放到记录仪中
return;
}
}
if(p->num>=5){
PCAR temp=p->queue->front->next;
printf("请输入你要进入便道的车牌号:");
scanf("%d",&need->brand);
//便道的车牌容错判断
int flag =0;//默认没有相同
for(int i=0;i<5;i++){//扫描记录仪,查找一下有没有相同的车牌号
//printf("%d",p->stack->cars[i].brand);
if(need->brand==p->monitor[i].brand){
flag=1;//如果有,显示入便道失败
printf("车库中有该车辆,入便道失败!");
printf("n");
return;
}
}
while(temp!=NULL){//如果程序能执行到此处,说明栈中没有相同的车辆,但是便道中有没有相同的车牌号,还不知道
if(need->brand==temp->brand){
flag=1;
printf("车库中有该车辆,入便道失败!");
printf("n");
break;
}
else
temp=temp->next;
}
if(flag==0) {//如果没有相同,则可以入库
in_Queue(p->queue,need);//将该车辆压到队列中
p->num++;
}
}
}
void out_Park(PPARK p){
PSTACK tempstack=(PSTACK)malloc(sizeof(STACK));//在栈和队列的基础上在加一个栈,用来暂存车辆
init_Stack(tempstack);
int outbrand;
CAR car;
CAR ncar;//用来存位于栈顶的车
CAR outcar;//用来存要出库的车
CAR waitcar;
CAR use;
int nowhour;
int nowmin;
int count=0;
printf("请输入你要出库的车牌号:");
scanf("%d",&outbrand);
int flag=0;
while(!Stack_is_empty(*p->stack)){
CAR car=pop_Stack(p->stack);
count++;
if(count!=1&&car.brand==outbrand){
//printf("找到了!");
outcar=car;
flag=1;
break;
}
if(count==1&&car.brand==outbrand){
outcar=car;
ncar=car;
flag=1;
break;
}
else{
if(count==1){
ncar=car;//存车库中最后一辆车的信息
}
push_Stack(tempstack,car);//出栈的这个元素压到存储栈中 ;
printf("车牌号为%d的车辆为其让路",car.brand);
printf("n");
}
}
if(flag==0){//如果车库为空,也没有找到相同的车牌号
printf("没有找到该车辆!");
while(!Stack_is_empty(*tempstack))//如果存车栈的栈不为空,那么需要把车重新压回车库中
{
use=pop_Stack(tempstack);
push_Stack(p->stack,use);
printf("车牌号为%d的让路车辆重新开回车库",use.brand);
printf("n");
}
return;
}
if(flag==1){//如果车库不为空,找到了相同的车牌号
p->num--;
printf("请输入小时:");
scanf("%d",&nowhour);
while(1) {//进入循环
if (nowhour < 0 || nowhour > 24) {//如果小时小于0大于24,则显示非法输入
printf("输入时间不合法,请重新输入");
printf("请输入小时:");
scanf("%d",&nowhour);
}
if(nowhour 60) {//如果小时小于0大于24,则显示非法输入
printf("输入时间不合法,请重新输入");
printf("请输入分钟:");
scanf("%d",&nowmin);
}
if(nowhour>ncar.hour){
break;
}
if(nowminqueue)){//如果队列为空
while(!Stack_is_empty(*tempstack))//如果存车栈的栈不为空,那么需要把车重新压回车库中
{
use=pop_Stack(tempstack);
push_Stack(p->stack,use);
printf("车牌号为%d的让路车辆重新开回车库",use.brand);
printf("n");
}
for(int k=0;k<=p->stack->top+1;k++) {//修改记录仪
if(outcar.brand==p->monitor[k].brand){
for(int i=k;i<=p->stack->top;i++){
p->monitor[i]=p->monitor[i+1];
}
}
}
}
else{//如果队列不为空
while(!Stack_is_empty(*tempstack))//如果存车栈的栈不为空,那么需要把车重新压回车库中
{
push_Stack(p->stack,pop_Stack(tempstack));//把之前让路的车压回来
}
CAR waitcar=out_Queue(p->queue);//便道中等待的车开出
PCAR pcar=&waitcar;//我要修改出栈车的时间,所以我需要获得他的地址
pcar->hour=nowhour;
pcar->min=nowmin;
push_Stack(p->stack,waitcar);//把便道中的第一辆车压入库中
printf("在便道中,车牌号为%d的车辆进入车库中",waitcar.brand);
printf("n");
for(int k=0;k<=p->stack->top+1;k++) {//修改记录仪
if(outcar.brand==p->monitor[k].brand){
for(int i=k;i<=p->stack->top;i++){
p->monitor[i]=p->monitor[i+1];
}
}
}
p->monitor[p->stack->top]=waitcar;
}
}
int c;
c=(nowhour-ncar.hour)*60+(nowmin-ncar.min);//一共停了多少分钟
int m=c/60;
if(m<1){
printf("n");
printf("停车时间不足1小时,不收取费用。");
printf("n");
}
if(m>=1){
printf("n");
printf("您的停车费用为:%d元",2*(c/60));
printf("n");
}
}
void show_Park(PARK p){
if(p.num<=5){
printf("车库中的车辆有:") ;
printf("n");
for(int i=0;i<=p.stack->top;i++){
printf("此车的车牌号是:%d ",p.monitor[i].brand);
printf("此车进来的小时是:%d ",p.monitor[i].hour);
printf("此车进来的分钟是:%d ",p.monitor[i].min);
printf("n");
}
for(int j=p.stack->top+1;j<=4;j++){
printf("n");
}
printf("-----------");
printf("n");
}
else{
printf("车库中的车辆有:") ;
printf("n");
for(int i=0;i<=p.stack->top;i++){
printf("此车的车牌号是:%d ",p.monitor[i].brand);
printf("此车进来的小时是:%d ",p.monitor[i].hour);
printf("此车进来的分钟是:%d ",p.monitor[i].min);
printf("n");
}
PCAR s=(PCAR)malloc(sizeof(CAR));
s=p.queue->front->next;
while(s!=NULL){
printf("%d",s->brand);
printf("n");
s=s->next;
}
}
}