1.Prak.h头文件,定义一个栈一个队列和一个链表,栈用来停车栈和让路栈,队列用作等候队列,链表用作存储信息。
#ifndef Park_
#include<time.h>
#define Park_
#define filed 100001
#define success 100002
#define Maxsize 2
typedef int myint;
struct node1
{
char a[20];
time_t timer;
struct node1 *next;
};
typedef struct node1 Node1;
typedef Node1 *Linknode1;
struct stop
{
Linknode1 top;
myint count;
};
typedef struct stop Stop; //停车栈和让车栈
struct node2
{
char b[20];
struct node2 *next;
};
typedef struct node2 Node2;
struct wait
{
Node2 *front;
Node2 *rear;
};
typedef struct wait Wait; //等候队列
struct info
{
char car[20];
time_t gettimer;
time_t outtimer;
time_t stoptimer;
struct info *next; //车辆信息存储链表
};
typedef struct info Info;
int stopinit(Stop *stop);
int waitinit(Wait *wait);
int infoinit(Info *message);
char *Enpark(Stop *stop,Wait *wait,int (*waitlength)(Wait *wait),Info *message);
void PopandPush(Stop *stop,Stop *giveway);
int stacklen(Stop *s);
int Poppark(Stop *stop,Stop *giveway,Wait *wait,Info *message);
void getmessage(Info *message);
void stopmessage(Info *message);
void waitmessage(Info *message);
void showall(Info *message);
#endif
2.Parkinfo.c文件,接口文件,实现主函数调用的函数功能。
#include<stdio.h>
#include”Park.h”
#include<time.h>
#include<stdlib.h>
#include<string.h>
show()
{
system(“clear”);
printf(“\n\n\n\n\n”);
printf(“\t\t\t**********************************************************\n\n”);
printf(“\t\t\t****************** ******************\n”);
printf(“\t\t\t 欢迎进入停车场 \n”);
printf(“\t\t\t****************** ******************\n\n”);
printf(“\t\t\t**********************************************************\n”);
}
showfunc()
{
printf(“\n\n\n\n\n”);
printf(“\t\t***********************************************************************\n\n”);
printf(“\t\t******1.进车登记 2.出车登记******************\n”);
printf(“\t\t******3.查询车辆信息 4.列出场内所有车辆信息******\n”);
printf(“\t\t******5.查询等候车辆信息 6.列出车辆出入记录**********\n”);
printf(“\t\t******7.退出 **********\n”);
printf(“\t\t***********************************************************************\n”);
}
char *times(time_t timer)
{
struct tm *tblock;
tblock=localtime(&timer);
return asctime(tblock);
} //将时间转化为字符串
void getshow(char car[20],time_t gettimer,time_t outtimer,time_t stoptimer)
{
if(gettimer!=0 && outtimer==0 && stoptimer==0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,car);
printf(“\t\t\t***进站时间: %s\n”,times(gettimer));
printf(“\t\t\t***************************************\n”);
}
else if(gettimer==0 && outtimer!=0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,car);
printf(“\t\t\t***离开等后区时间: %s\n”,times(outtimer));
printf(“\t\t\t***************************************\n”);
}
else
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,car);
printf(“\t\t\t***进站时间: %s\n”,times(gettimer));
printf(“\t\t\t***出站时间: %s\n”,times(outtimer));
printf(“\t\t\t***停车时间: %d\n”,stoptimer);
printf(“\t\t\t***************************************\n”);
}
} //用于查找和记录的输出
int stopinit(Stop *s)
{
if(NULL==s)
{
return filed;
}
s->count=0;
s->top=NULL;
return success;
} //停车栈的初始化
int waitinit(Wait *wait)
{
Node2 *n=(Node2 *)malloc(sizeof(Node2));
if(NULL==n)
{
return filed;
}
n->next=NULL;
wait->rear=wait->front=n;
return success;
} //等候队列的初始化
int infoinit(Info *message)
{
message->next=NULL;
message->gettimer=0;
message->outtimer=0;
message->stoptimer=0;
return success;
} //信息存储链表的初始化
char *Enpark(Stop *stop,Wait *wait,int (*waitlength)(Wait *wait),Info *message)
{
Info *L=(Info *)malloc(sizeof(Info));
infoinit(L);
L->next=message->next; //L是存储信息链表
message->next=L;
Info *k=message->next;
char num[20];
system(“clear”);
printf(“\n\n\n\n\n”);
printf(“\t\t\t请输入要进入停车场的车牌: “);
scanf(“%s”,num);
if(stop->count<Maxsize && waitlength(wait)==0)
{
Linknode1 p=(Linknode1)malloc(sizeof(Node1));
strcpy(p->a,num);
strcpy(L->car,num);
p->timer=time(NULL);
L->gettimer=time(NULL);
p->next=stop->top;
stop->top=p;
stop->count++;
getshow(L->car,L->gettimer,L->outtimer,L->stoptimer);
return p->a;
}
else
{
Node2 *n=(Node2 *)malloc(sizeof(Node2));
printf(“\t\t\t停车场已满请在等候区等待!\n\n”);
strcpy(n->b,num);
strcpy(L->car,num);
n->next=NULL;
wait->rear->next=n;
wait->rear=n;
return n->b;
}
} //进入停车栈或者等候队列
void PopandPush(Stop *stop,Stop *giveway)
{
Linknode1 p=stop->top;
stop->top=p->next;
Linknode1 n=(Linknode1)malloc(sizeof(Node1));
strcpy(n->a,p->a);
n->next=giveway->top;
giveway->top=n;
giveway->count++;
free(p);
stop->count– ; //从等候队列出来并进入停车栈
}
int stacklen(Stop *s)
{
return (s->count);
} //计算栈的长度
int Poppark(Stop *stop,Stop *giveway,Wait *wait,Info *message)
{
Info *k=message->next;
Info *u=message->next;
char num[20];
system(“clear”);
printf(“\n\n\n\n\n”);
printf(“\t\t\t请输入要出停车场车辆的车牌号: “);
scanf(“%s”,num);
if(stacklen(stop)==0)
{
printf(“停车场内没有车辆!\n”);
return 0; //判断停车场内有无车辆
}
while((stacklen(stop)!=0)&&strcmp(num,stop->top->a)!=0)
{
PopandPush(stop,giveway);
}
if (stacklen(stop)==0) //判断输入的车牌是否在停车场内
{
printf(“\t\t\t场内没有该车!\n”);
while(stacklen(giveway)!=0) //不在就进入等候队列查找
{
PopandPush(giveway,stop);
} //将查找停车栈时出去的车停回来
Node2 *q=wait->front;
int j=0,m=1;
while(q!=NULL&&(strcmp(num,q->b)!=0)) //开始寻找
{
j++;
q=q->next;
}
if(q==NULL)
{
printf(“\t\t\t等候区内没有该车!\n”);
return 2; //没找到结束函数,说明输入的车牌不在停车场和等候区
}
q=wait->front;
while(q!=NULL&&m<j)
{
q=q->next;
m++;
} //将指针q指向到要出去人的前一个
Node2 *tem=q->next;
q->next=q->next->next;
while(strcmp(num,k->car)!=0)
{
k=k->next;
}
k->outtimer=time(NULL);
k->stoptimer = k->outtimer – k->gettimer; //销毁之前将离开时间和等候时间存入信息链表中
getshow(k->car,k->gettimer,k->outtimer,k->stoptimer);
free(tem); //销毁
printf(“\t\t\t车牌号为 %s 的车辆已从等候区离开!\n”,num);
return 1;
}
Linknode1 p=stop->top;
stop->top=p->next; //如果在停车场内就开始讲这辆车出去
while(strcmp(p->a,k->car)!=0)
{
k=k->next;
}
k->outtimer=time(NULL);
k->stoptimer = k->outtimer – k->gettimer;
getshow(k->car,k->gettimer,k->outtimer,k->stoptimer);
free(p);
stop->count–;
printf(“\t\t\t车牌号为 %s 的车辆已出停车场\n”,k->car);
while(stacklen(giveway)!=0)
{
PopandPush(giveway,stop);
}
if(stop->count<Maxsize && waitlength(wait)!=0) //车辆出停车场之后要判断等候区是否有车,如有车等候区车进入
{
Node2 *n=wait->front->next;
wait->front->next=n->next;
while(strcmp(n->b,u->car)!=0)
{
u=u->next;
}
Linknode1 p=(Linknode1)malloc(sizeof(Node1));
strcpy(p->a,n->b);
u->gettimer=time(NULL);
p->timer=time(NULL);
p->next=stop->top;
stop->top=p;
stop->count++;
getshow(u->car,u->gettimer,u->outtimer,u->stoptimer);
free(n);
if(wait->front->next==NULL)
{
wait->rear=wait->front;
}
printf(“\t\t\t车牌号为 %s 的车辆已从等候区进入停车场\n”,p->a); //从等候区出来进入停车场
}
return success;
}
void getmessage(Info *message)
{
char num[20];
system(“clear”);
printf(“\n\n\n\n\n”);
printf(“\t\t\t请输入你要查找的车牌号: “);
scanf(“%s”,num);
Info *k=message->next;
while(k!=NULL && strcmp(num,k->car)!=0)
{
k=k->next;
}
if(k==NULL)
{
printf(“\t\t\t*********************************************\n”);
printf(“\t\t\t***车牌号为 %s 的车辆没来过停车场!”,num);
printf(“\t\t\t*********************************************\n”);
}
else if(k->gettimer!=0 && k->stoptimer==0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***进站时间: %s\n”,times(k->gettimer));
printf(“\t\t\t***该车辆未出站!\n”);
printf(“\t\t\t***************************************\n”);
}
else if(k->gettimer==0 && k->outtimer==0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***该车辆在等候区等待!\n”);
printf(“\t\t\t***************************************\n”);
}
else if(k->gettimer==0 && k->outtimer!=0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***离开等后区时间: %s\n”,times(k->outtimer));
printf(“\t\t\t***该车只在等候区等过没进入停车场\n”);
printf(“\t\t\t***************************************\n”);
}
else
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***进站时间: %s\n”,times(k->gettimer));
printf(“\t\t\t***出站时间: %s\n”,times(k->outtimer));
printf(“\t\t\t***停车时间: %d\n”,k->stoptimer);
printf(“\t\t\t***该车已出站!\n”);
printf(“\t\t\t***************************************\n”);
} //按车牌查询车辆信息,根据信息判断车辆的状态和输出
}
void stopmessage(Info *message)
{
system(“clear”);
Info *k=message->next;
while(k!=NULL)
{
if(k->gettimer!=0 && k->outtimer==0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***进站时间: %s\n”,times(k->gettimer));
printf(“\t\t\t***该车辆在停车场内!\n”);
printf(“\t\t\t***************************************\n”);
}
k=k->next;
} //遍历在停车中的车辆
}
void waitmessage(Info *message)
{
system(“clear”);
Info *k=message->next;
while(k!=NULL)
{
if(k->gettimer==0 && k->outtimer==0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***该车辆在等候区等待!\n”);
printf(“\t\t\t***************************************\n”);
}
k=k->next;
}
} //遍历在等候区的车辆
void showall(Info *message)
{
system(“clear”);
Info *k=message->next;
while(k!=NULL)
{
if(k->gettimer!=0 && k->stoptimer==0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***进站时间: %s\n”,times(k->gettimer));
printf(“\t\t\t***该车辆未出站!\n”);
printf(“\t\t\t***************************************\n”);
}
if(k->gettimer==0 && k->outtimer==0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***该车辆在等候区等待!\n”);
printf(“\t\t\t***************************************\n”);
}
if(k->gettimer==0 && k->outtimer!=0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***离开等后区时间: %s\n”,times(k->outtimer));
printf(“\t\t\t***该车只在等候区等过没进入停车场\n”);
printf(“\t\t\t***************************************\n”);
}
if(k->gettimer!=0 && k->outtimer!=0)
{
printf(“\t\t\t***************************************\n”);
printf(“\t\t\t***车牌号: %s\n”,k->car);
printf(“\t\t\t***进站时间: %s\n”,times(k->gettimer));
printf(“\t\t\t***出站时间: %s\n”,times(k->outtimer));
printf(“\t\t\t***停车时间: %d\n”,k->stoptimer);
printf(“\t\t\t***该车已出站!\n”);
printf(“\t\t\t***************************************\n”);
}
k=k->next;
}
} //遍历所有车辆的出入记录,包括已经离开的
心得: 在做这次项目的过程中,学到的最重要的一点就是编写程序是的规范化和程序的简洁化,不然在调试过程中会出现森对阻碍。