public.h
#ifndef _PUBLIC_H_
#define _PUBLIC_H_
#include
typedef struct _Data
{
int park_place;//车位号
int park_num;//停车号
int plate_num;//车牌号
time_t start;//1900年到现在的秒数
}Data;
typedef enum {FALSE, TRUE} BOOL;
#endif// _PUBLIC_H_
carparking.h
#ifndef _CARPARKING_H_
#define _CARPARKING_H_
#define N 10
int g_stack_len = 0;//停车区长度
int g_queue_len = 0;//等候区长度
int g_park_num = 10000;//停车号
enum MenuOption{PARK = '1', LEAVE, VIEW,QUIT};//菜单
void menu();
void Display();
void Park(Stack *park, Queue *wait);
void Add(Data *data);
void View(Stack *park);
void Leave(Stack *park, Stack *GiveWay, Queue *wait);
#endif //_CARPARKING_H_
stack.h
#ifndef _STACK_H_
#define _STACK_H_
#include "public.h"
typedef struct node_s
{
Data data;
struct node_s *next;
}Node_s;
typedef struct stack
{
Node_s *top;
}Stack;
// 初始化栈
void Init_s(Stack *s);
// 判断空栈
BOOL Empty_s(Stack *s);
// 入栈
void Push_s(Stack *s, Data data);
// 出栈
void Pop_s(Stack *s);
// 获取栈顶元素
Data GetTop_s(Stack *s);
#endif // _STACK_H_
queue.h
#ifndef _QUEUE_H_
#define _QUEUE_H_
#include "public.h"
typedef struct node_q
{
Data data;
struct node_q *next;
}Node_q;
typedef struct queue
{
Node_q *front; // 队头指针
Node_q *rear; // 队尾指针
}Queue;
// 初始化队列
void Init_q(Queue *s);
// 判断空队列
BOOL Empty_q(Queue *s);
// 入队列
void Push_q(Queue *s, Data data);
// 出队列
void Pop_q(Queue *s);
// 获取队头元素
Data GetTop_q(Queue *s);
#endif // _QUEUE_H_
main.c
#include
#include "stack.h"
int main()
{
menu();
return 0;
}
carparking.c
#include
#include
#include "stack.h"
#include "Queue.h"
#include "public.h"
#include "carparking.h"
//菜单函数
void menu()
{
Stack park;
Init_s(&park);
Stack GiveWay;
Init_s(&GiveWay);
Queue wait;
Init_q(&wait);
while(1)
{
system("clear");
Display();//显示菜单
char buf[10];
fgets(buf, 10, stdin);//获取指令
system("clear");
switch (buf[0])
{
case PARK:
Park(&park, &wait);
break;
case LEAVE:
Leave(&park, &GiveWay, &wait);
break;
case VIEW:
View(&park);
break;
case QUIT:
return;
default:
printf("\t\t指令错误!\n");
break;
}
printf("\t\t按ENTER键返回\n");
getchar();
}
}
//显示菜单函数
void Display()
{
printf("\t\t****************************\n");
printf("\t\t* 欢迎来到二狗停车场 *\n");
printf("\t\t****************************\n");
printf("\t\t*本停车场总共%d个车位 *\n", N);
if (N > g_stack_len)
printf("\t\t*空闲车位:%d个 *\n", N-g_stack_len);
else if (N == g_stack_len)
printf("\t\t*所有车位已满,%d辆车排队中 *\n", g_queue_len);
printf("\t\t****************************\n");
printf("\t\t*1、停车 *\n");
printf("\t\t*2、离开 *\n");
printf("\t\t*3、查看停车场停车情况 *\n");
printf("\t\t*4、退出 *\n");
printf("\t\t****************************\n");
}
//停车
void Park(Stack *park, Queue *wait)
{
if (NULL == park || NULL == wait)
return;
Data data;
data.park_num = ++g_park_num;
srand((unsigned int)time(NULL));
data.plate_num = rand()%90000+10000;
if (N > g_stack_len)
{
time (&data.start);
struct tm * lt = localtime (&data.start);
g_stack_len++;//停车车辆+1
data.park_place = g_stack_len;//进入相应车位
Push_s(park, data);
printf ("\t\t车位:%d\n",data.park_place);
printf ("\t\t停车号:%d\n",data.park_num);
printf ("\t\t车牌号:%d\n",data.plate_num);
printf ( "\t\t停车时间:%d/%d/%d %d:%d:%d\n", \
lt->tm_year+1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
printf("\t\t停车成功!\n");
}
else
{
Push_q(wait, data);
g_queue_len++;//等候车辆+1
printf("\t\t您的停车号为%d\n", data.park_num);
printf("\t\t停车场已满,您是等候区第%d位司机\n", g_queue_len);
}
}
//离开
void Leave(Stack *park, Stack *GiveWay, Queue *wait)
{
if (NULL == park || NULL == GiveWay || NULL == wait)
return;
if (Empty_s(park) == TRUE)
{
printf("\t\t木有车\n");
return;
}
printf("\t\t请输入您的停车号:");
int park_num;
int stack_len = g_stack_len;
scanf("%d", &park_num);
getchar();
while (park->top)
{
if (park->top->data.park_num == park_num)
{
time_t t;
// 获取时间戳
time (&t);
struct tm * lt = localtime (&t);
printf ("\t\t车位:%d\n",park->top->data.park_place);
printf ("\t\t停车号:%d\n",park->top->data.park_num);
printf ("\t\t车牌号:%d\n",park->top->data.plate_num);
printf ("\t\t离开时间:%d/%d/%d %d:%d:%d\n", \
lt->tm_year+1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
printf("\t\t停车时间:%lds\n", t-park->top->data.start);
printf("\t\t离开成功!\n\n");
Pop_s(park);
while(GiveWay->top)
{
Push_s(park, GetTop_s(GiveWay));
Pop_s(GiveWay);
}
if (TRUE == Empty_q(wait))
{
g_stack_len--;
return;
}
else
{
wait->front->data.start = t;
wait->front->data.park_place = g_stack_len;//进入相应车位
printf("\t\t等候区车进入停车场\n");
printf ("\t\t车位:%d\n",wait->front->data.park_place);
printf ("\t\t停车号:%d\n",wait->front->data.park_num);
printf ("\t\t车牌号:%d\n",wait->front->data.plate_num);
printf ("\t\t停车时间:%d/%d/%d %d:%d:%d\n", \
lt->tm_year+1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
printf("\t\t停车成功!\n");
Push_s(park, GetTop_q(wait));
Pop_q(wait);
g_queue_len--;
return;
}
}
else
{
park->top->data.park_place--;
Push_s(GiveWay, GetTop_s(park));
Pop_s(park);
}
}
printf("\t\t没有找到这辆车,请重新输入\n");
while(GiveWay->top)
{
Push_s(park, GetTop_s(GiveWay));
Pop_s(GiveWay);
}
}
//查看
void View(Stack *park)
{
if (NULL == park)
return;
if (Empty_s(park) == TRUE)
{
printf("\t\t木有车\n");
return;
}
printf("\t\t车位\t\t停车号\t\t车牌号\t\t停车时长\n");
Node_s *tmp = park->top;
while (tmp)
{
printf("\t\t%d", tmp->data.park_place);
printf("\t\t%d", tmp->data.park_num);
printf("\t\t%d", tmp->data.plate_num);
time_t t;
// 获取时间戳
time (&t);
printf("\t\t%lds\n", t-(tmp->data.start));
tmp = tmp->next;
}
}
queue.c
#include
#include "Queue.h"
void Init_q(Queue *q)
{
if (NULL == q)
return;
q->front = NULL;
q->rear = NULL;
}
BOOL Empty_q(Queue *q)
{
if (NULL == q)
return FALSE;
if (NULL == q->front)
return TRUE;
return FALSE;
}
void Push_q(Queue *q, Data data)
{
if (NULL == q)
return;
Node_q *node = (Node_q *)malloc(sizeof(Node_q)/sizeof(char));
if (NULL == node)
return;
node->data = data;
node->next = NULL;
if (q->rear != NULL)
{
q->rear->next = node;
q->rear = node;
}
else
{
q->rear = node;
q->front = node;
}
}
void Pop_q(Queue *q)
{
if (NULL == q)
return;
if (Empty_q(q) == TRUE)
return;
Node_q *tmp = q->front;
q->front = tmp->next;
free(tmp);
if (q->front == NULL)
q->rear = NULL;
}
Data GetTop_q(Queue *q)
{
if (NULL == q)
return;
if (Empty_q(q) == TRUE)
exit(-1); // 程序退出
return q->front->data;
}
stack.c
#include
#include "stack.h"
void Init_s(Stack *s)
{
if (NULL == s)
return;
s->top = NULL;
}
BOOL Empty_s(Stack *s)
{
if (NULL == s)
return FALSE;
if (NULL == s->top)
return TRUE;
return FALSE;
}
void Push_s(Stack *s, Data data)
{
if (NULL == s)
return;
Node_s *node = (Node_s *)malloc(sizeof(Node_s)/sizeof(char));
if (NULL == node)
return;
node->data = data;
node->next = s->top;
s->top = node;
}
void Pop_s(Stack *s)
{
if (NULL == s)
return;
if (Empty_s(s) == TRUE)
return;
Node_s *tmp = s->top;
s->top = tmp->next;
free(tmp);
}
Data GetTop_s(Stack *s)
{
if (NULL == s)
return;
if (Empty_s(s) == TRUE)
exit(-1); // 程序退出
return s->top->data;
}