#ifndef _PARKING_LOT_H_
#define _PARKING_LOT_H_
#define SIZE1 3
#define SIZE2 5
struct car //车辆信息
{
char licensenum[40];
char intotime[30];
char offtime[30];
};
typedef struct car Car;
struct par //停放栈
{
Car stop[SIZE1];
int top;
};
typedef struct par Park;
struct giv //让路栈
{
Car give[SIZE1];
int top;
};
typedef struct giv Give;
struct wai //停放队列
{
Car wait[SIZE2];
int front;
int rear;
};
typedef struct wai Wait;
void menu(Park p, Wait *w);
void situation(Park p,Wait *w);
void parkcar(Park *p, Wait *w);
void leaving(Park *p, Give *g, Wait *w, Car C[]);
void searchcar(Park p, Wait *w, Car C[]);
void searchwait(Wait *w);
void parkinfo(Park p, Wait *w, Car C[]);
void ppush(Park *p, Car c);
Car ppop(Park *p);
void gpush(Give *g, Car c);
Car gpop(Give *g);
Car dewait(Wait *w);
#endif
#include <stdio.h>
#include "ParkingLot.h"
#include <stdlib.h>
#include <string.h>
int COUNT = 0;
int main()
{
Park p;//停车栈
Give g;//让路栈
Wait *w;//停放队列
Car C[100];//记下已经走的车;
p.top = -1;
g.top = -1;//栈初始化
w = (Wait *)malloc(sizeof(Wait));
w->front = w->rear = 0;//队列初始化
welcome();
char choice[20];
while(1)
{
menu(p, w);
scanf("%s", choice);
if (strlen(choice) == 1)//判断输入命令长度是否为1
{
switch(*choice)
{
case '1':
parkcar(&p, w);
break;
case '2':
leaving(&p, &g, w, C);
break;
case '3':
searchcar(p, w, C);
break;
case '4':
searchwait(w);
break;
case '5':
parkinfo(p, w, C);
break;
case '6':
exit(0);
break;
default:
{
while (getchar() != '\n');
printf("INPUT ERROR!\n");
getchar();
break;
}
}//switch
continue;
}
else //命令长度不为1
{
while(getchar() != '\n');
printf("INPUT ERROR!\n");
getchar();
continue;
}
}
return 0;
}
#include <stdio.h>
#include "ParkingLot.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>
extern int COUNT;
void welcome()
{
system("clear");
printf("\t\t************************************************\n\n");
printf("\t\t\t\t WELCOME TO PARKINGLOT\n");
printf("\t\t************************************************\n\n");
sleep(1);
}
void situation(Park p, Wait *w)//当前停车场状况
{
if (p.top >= SIZE1 - 1)
{
printf("\t\t---------------------停车场内已满!");
if ((w->rear + 1) % SIZE2 == w->front)
{
printf(" 等候区已满!----------------------------\n");
getchar();
getchar();
}
else
{
printf(" 还有 %d 个剩余等候车位!-----------------\n", SIZE2 - ((w->rear - w->front + SIZE2) % SIZE2) - 1);
getchar();
getchar();
}
}
else
{
printf("\t\t------------------------停车场内有 %d 个停车位! 有 %d 个等候车位-----------------\n\n", SIZE1 - p.top - 1, SIZE2 - ((w->rear - w->front + SIZE2) % SIZE2) - 1);
}
}
void menu(Park p, Wait *w)
{
system("clear");
printf("\t\t******************************************************************************\n\n");
printf("\t\t******************************目前停车场状况**********************************\n\n");
situation(p, w);
printf("\t\t******************************************************************************\n");
printf("\t\t1、停车\t\t\t\t\t\t2、离开\n");
printf("\t\t3、查询指定车辆信息\t\t\t\t4、查询等候信息\n");
printf("\t\t5、查询场内信息\t\t\t\t\t6、退出\n");
printf("\t\t******************************************************************************\n\n");
printf("请选择:\n");
}
void ppush(Park *p, Car c)//停车进栈
{
p->stop[++p->top] = c;
}
Car ppop(Park *p)//从停车栈出去
{
return p->stop[p->top--];
}
void gpush(Give *g, Car c)//进入让路栈
{
g->give[++g->top] = c;
}
Car gpop(Give *g)//从让路栈出去
{
return g->give[g->top--];
}
Car dewait(Wait *w) //从等候区出去
{
Car e;
e = w->wait[w->front];
w->front = (w->front + 1) % SIZE2;
return e;
}
void parkcar(Park *p, Wait *w) //停车
{
time_t t = time(0);
if (p->top >= SIZE1 - 1)//停车场已满
{
printf("停车场内已满,请到等候区等待!\n");
if ((w->rear + 1) % SIZE2 == w->front)
{
printf("等候区也已经满了,欢迎下次光临!\n");
getchar();
return;
}
else
{
printf("请输入车牌号:\n");
scanf("%s", w->wait[w->rear].licensenum);
w->rear = (w->rear + 1) % SIZE2;
return;
}
}
else
{
printf("请输入车牌号:\n"); //若停车场内不满
scanf("%s", p->stop[p->top + 1].licensenum);
strftime(p->stop[p->top + 1].intotime, 30, "%Y-%m-%d %H:%M:%S", localtime(&t));
p->top++;
return;
}
}
void leaving(Park *p, Give *g, Wait *w, Car C[])//离开
{
char s[30];
int i, count;
time_t t = time(0);
printf("输入要离开的车牌号:\n");
scanf("%s", s);
for (i = 0; i <= p->top; i++)//记录要离开的车的位置
{
if (0 == strcmp(s, p->stop[i].licensenum))
{
count = i;
break;
}
}
if (i == p->top + 1)//找不到车
{
printf("停车区没有车牌号为 %s 的车,请重新输入!\n", s);
getchar();
getchar();
return;
}
for (i = 0; i < p->top - count; i++)//让路完成
{
gpush(g, ppop(p));
}
C[COUNT] = ppop(p);
strftime(C[COUNT].offtime, 30, "%Y-%m-%d %H:%M:%S", localtime(&t));//记录离开的车!
COUNT++;
for (i = 0; i <= g->top; i++)//让路再进完成
{
ppush(p,gpop(g));
}
t = time(0);
if (w->rear != w->front)
{
ppush(p, dewait(w));
printf("\t\t-----------------------------------------------------------------------\n");
printf("\t\t\t\t\t车牌号为 %s 进入停车场\n",p->stop[p->top].licensenum);
printf("\t\t-----------------------------------------------------------------------\n");
strftime(p->stop[p->top].intotime, 30, "%Y-%m-%d %H:%M:%S", localtime(&t));//在等候区进入停车区
getchar();
getchar();
}
else
{
return;
}
}
void searchcar(Park p, Wait *w, Car C[])
{
int i;
char s[20];
printf("请输入要查找的车的车牌号:\n");
scanf("%s", s);
for (i = 0; i<= p.top; i++)
{
if (0 == strcmp(s, p.stop[i].licensenum))
{
printf("\t\t-----------------------------------------------------------------------\n");
printf("\t\t\t\t\t车牌号\t\t\t入场时间\n");//先查询在停车场内的车
printf("\t\t\t\t\t%s", p.stop[i].licensenum);
printf("\t\t\t%s\n", p.stop[i].intotime);
printf("\t\t-----------------------------------------------------------------------\n");
getchar();
getchar();
return;
}
}
i = w->front; //从等候区找
while (i != w->rear)
{
if (0 == strcmp(s, w->wait[i].licensenum))
{
printf("\t\t-----------------------------------------------------------------------\n");
printf("\t\t\t\t\t车牌号\t\t\t状态\n");
printf("\t\t\t\t\t%s", w->wait[i].licensenum);
printf("\t\t\t等待停车\n");
printf("\t\t-----------------------------------------------------------------------\n");
getchar();
getchar();
return;
}
i = (i + 1) % SIZE2;
}
for (i = 0; i < COUNT; i++)
{
if (0 == strcmp(s, C[i].licensenum))
{
printf("\t\t-----------------------------------------------------------------------\n\n");
printf("\t\t车牌号\t\t入场时间\t\t\t离开时间\n");
printf("\t\t%s", C[i].licensenum);
printf("\t\t%s", C[i].intotime);
printf("\t\t%s\n\n", C[i].offtime);
printf("\t\t-----------------------------------------------------------------------\n");
getchar();
getchar();
return;
}
}
printf("没有车牌号为 %s 的车", s);
}
void searchwait(Wait *w) //等候区
{
int i;
printf("\t\t------------------------------------等候区-------------------------------------\n");
if (w->front == w->rear)
{
printf("\t\t\t\t等候区没有车\n");
printf("\t\t---------------------------------------------------------------------------\n");
getchar();
getchar();
}
else
{
printf("\t\t\t\t\t车牌号\t\t\t\t状态\n");
i = w->front;
while (i != w->rear)
{
printf("\t\t\t\t\t%s", w->wait[i].licensenum);
printf("\t\t\t\t等待停车\n\n");
i = (i + 1) % SIZE2;
}
printf("\t\t\t\t\t\t等候区有 %d 辆车\n", (w->rear - w->front + SIZE2) % SIZE2);
}
printf("\t\t-------------------------------------------------------------------------------\n");
getchar();
getchar();
}
void parkinfo(Park p, Wait *w, Car C[])
{
int i;
if (-1 == p.top)
{
printf("停车场内没有车辆!\n");
getchar();
return;
}
printf("\t\t**************************停车场内*********************************\n");
printf("\t\t车牌号\t\t\t入场时间\n");//先查询在停车场内的车
for (i = 0; i<= p.top; i++)
{
printf("\t\t%s", p.stop[i].licensenum);
printf("\t\t\t%s\n\n", p.stop[i].intotime);
}
printf("\t\t*******************************等候区******************************\n");
if (w->front == w->rear)
{
printf("\t\t\t\t\t等候区没有车\n");
}
else
{
printf("\t\t车牌号\t\t\t\t状态\n");
i = w->front;
while (i != w->rear)
{
printf("\t\t%s", w->wait[i].licensenum);
printf("\t\t\t\t等待停车\n\n");
i = (i + 1) % SIZE2;
}
}
printf("\t\t***************************已经离开的车******************************\n");
if (0 == COUNT)
{
printf("\t\t\t\t\t没有离开的车\n\n");
getchar();
getchar();
return;
}
printf("\t\t车牌号\t\t入场时间\t\t\t离开时间\n");
for (i = 0; i < COUNT; i++)
{
printf("\t\t%s", C[i].licensenum);
printf("\t\t%s", C[i].intotime);
printf("\t\t%s\n\n", C[i].offtime);
}
getchar();
getchar();
}