停车场项目需求
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门
外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车
要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)
功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、
查询场内车辆信息、查询等候车辆信息、退出系统。
(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门
外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车
要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)
功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、
查询场内车辆信息、查询等候车辆信息、退出系统。
(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。
ParkingLot.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "ParkingLot.h"
#include <time.h>
#define D (24 * 60 * 60)
#define H (60 * 60)
#define M (60)
void welcome()
{
system("clear");
printf("**********************************************\n");
printf("* *\n");
printf("* 欢迎来到停车场 *\n");
printf("* *\n");
printf("**********************************************\n");
sleep(2);
}
void menu()
{
system("clear");
printf("**********************************************\n");
printf("* *\n");
printf("* 1.进车登记 *\n");
printf("* 2.出车登记 *\n");
printf("* 3.查询停车车辆信息 *\n");
printf("* 4.查询出人车记录 *\n");
printf("* 5.查询场内车辆信息 *\n");
printf("* 6.查询等候车辆信息 *\n");
printf("* 0.退出系统 *\n");
printf("* *\n");
printf("**********************************************\n");
printf("请输入您的选择:\n");
// sleep(5);
}
int now_time(void)
{
time_t cur_t;
time(&cur_t);
int time = cur_t % D;
return time;
}
int InitList(List **l)
{
(*l) = (List *)malloc(sizeof(List));
if (NULL == (*l))
{
return FAILURE;
}
(*l)->next = NULL;
return SUCCESS;
}
int InsertList(List *l, int e)
{
List *p = l, *q;
while (p->next)
{
p = p->next;
}
q = (List *)malloc(sizeof(List));
q->num = e;
q->next = p->next;
p->next = q;
return SUCCESS;
}
int FindList(List *l, int e)
{
List *t = l->next;
while(t)
{
if (t->num == e)
{
return SUCCESS;
}
t = t->next;
}
return FAILURE;
}
int InitStack(Stack **s)
{
(*s) = (Stack *)malloc(sizeof(Stack));
if (NULL == (*s))
{
return FAILURE;
}
(*s)->length = 0;
(*s)->top = NULL;
return SUCCESS;
}
int FullStack(Stack *s)
{
return (s->length == 3) ? SUCCESS : FAILURE;
}
int Push(Stack *s, int e)
{
Node *p = (Node *)malloc(sizeof(Node));
p->num = e;
p->next = s->top;
s->top = p;
s->length++;
return SUCCESS;
}
int Pop(Stack *s)
{
int e;
if (NULL == s->top)
{
return FAILURE;
}
Node *p = s->top;
e = s->top->num;
s->top = p->next;
free(p);
s->length--;
return e;
}
int GetTop(Stack *s)
{
return (s->top->num);
}
int EmptyStack(Stack *s)
{
return (NULL == s->top) ? SUCCESS : FAILURE;
}
int FindStack(Stack *s, Stack *t, int e)
{
while(s->top)
{
if (e == GetTop(s))
{
while(t->top)
{
Push(s, Pop(t));
}
return SUCCESS;
}
Push(t, Pop(s));
}
while(t->top)
{
Push(s, Pop(t));
}
return FAILURE;
}
int InitQueue(Q **q)
{
(*q) = (Q *)malloc(sizeof(Q));
if (NULL == (*q))
{
return FAILURE;
}
Node *p = (Node *)malloc(sizeof(Node));
if (NULL == p)
{
return FAILURE;
}
p->next = NULL;
(*q)->front = (*q)->rear = p;
return SUCCESS;
}
int EnterQueue(Q *q, int e)
{
Node *p = (Node *)malloc(sizeof(Node));
p->num = e;
p->next = NULL;
q->rear->next = p;
q->rear = p;
return SUCCESS;
}
int EmptyQueue(Q *q)
{
return (q->front == q->rear) ? SUCCESS : FAILURE;
}
int DeleteQueue(Q *q)
{
int e;
Node *p = q->front->next;
e = p->num;
q->front->next = p->next;
if (NULL == p->next)
{
q->rear = q->front;
}
free(p);
return e;
}
int FindQueue(Q *q, int e)
{
Node *t = q->front->next;
while(t)
{
if (t->num == e)
{
return SUCCESS;
}
t = t->next;
}
return FAILURE;
}
void ParkInfo(Stack *s, Q *q)
{
system("clear");
int num;
printf("请输入车牌号:\n");
scanf("%d", &num);
if (SUCCESS == FullStack(s))
{
EnterQueue(q, num);
printf("停车场已满,进入等侯区!\n");
}
else
{
Push(s, num);
time_t cur_t;
struct tm* cur_tm;
time(&cur_t);
cur_tm = localtime(&cur_t);
strcpy(s->top->InitTime, asctime(cur_tm));
s->top->ParkTime = now_time();
// printf("%d", s->top->ParkTime);
printf("车牌号 :%d\n", num);
printf("入场时间:%s", s->top->InitTime);
printf("停车成功!\n");
}
}
void LeaveInfo(Stack *s, Stack *t, Q *q, List *l)
{
system("clear");
int num;
int Ptime; //停车时间
int h, m, sec;//时、分、秒
printf("请输入要离开的车牌号:\n");
scanf("%d", &num);
while(s->length)
{
if (num == GetTop(s))
{
break;
}
Push(t,Pop(s));
}
if (NULL == s->top)
{
printf("本停车场没有此车辆!\n");
}
else
{
Ptime = now_time() - (s->top->ParkTime);
// printf("%d\n", s->top->ParkTime);
// printf("%d\n", now_time());
// printf("%d\n", Ptime);
h = Ptime / H;
m = Ptime % H / M;
sec = Ptime % M;
int r = Pop(s);
InsertList(l, r);
while(EmptyStack(t) != SUCCESS)
{
Push(s, Pop(t));
}
printf("车牌号 :%d\n", num);
printf("停车时间:%d:%d:%d\n", h, m, sec);
printf("出车成功!\n\n");
if (FAILURE == EmptyQueue(q))
{
int ret = DeleteQueue(q);
Push(s, ret);
time_t cur_t;
struct tm* cur_tm;
time(&cur_t);
cur_tm = localtime(&cur_t);
strcpy(s->top->InitTime, asctime(cur_tm));
s->top->ParkTime = now_time();
printf("等候车辆开始停车:\n");
printf("车牌号 :%d\n", ret);
printf("入场时间:%s", s->top->InitTime);
printf("停车成功!\n");
}
}
}
void SearchCarInfo(Stack *s, Stack *t, Q *q, List *l)
{
system("clear");
int num;
printf("请输入所需查询车辆信息的车牌号:\n");
scanf("%d", &num);
if (FindQueue(q, num) == SUCCESS || FindList(l, num) == SUCCESS)
{
printf("车牌号 :%d\n", num);
printf("是否来过停车场:是\n"); //包括已经离开和在等候区的车辆
if (FindStack(s, t, num) == SUCCESS)
{
int Ptime, h, m, sec;
printf("是否还在停车场:是\n");
Ptime = now_time() - (s->top->ParkTime);
h = Ptime / H;
m = Ptime % H / M;
sec = Ptime % M;
printf("停车时间 计时:%d:%d:%d\n", h, m, sec);
}
else
{
printf("是否还在停车场:否\n");
}
}
else
{
printf("车牌号 :%d\n", num);
printf("是否来过停车场:否\n");
if (FindStack(s, t, num) == SUCCESS)
{
int Ptime, h, m, sec;
printf("是否还在停车场:是\n");
Ptime = now_time() - (s->top->ParkTime);
h = Ptime / H;
m = Ptime % H / M;
sec = Ptime % M;
printf("停车时间 计时:%d:%d:%d\n", h, m, sec);
}
else
{
printf("是否还在停车场:否\n");
}
}
}
void SearchRecord(Stack *s, Stack *t, Q *q, List *l)
{
system("clear");
List *x = l->next;
while(x)
{
printf("车牌号 : %d\n", x->num);
printf("停车状态: 已离开\n\n");
x = x->next;
}
Node *y = q->front->next;
while(y)
{
printf("车牌号 : %d\n", y->num);
printf("停车状态: 等候中\n\n");
y = y->next;
}
while(s->top)
{
printf("车牌号 : %d\n", s->top->num);
printf("停车状态: 停车中\n\n");
Push(t, Pop(s));
}
while(t->top)
{
Push(s, Pop(t));
}
}
void SearchParkInfo(Stack *s, Stack *t)
{
system("clear");
while(s->top)
{
printf("车牌号 :%d\n\n", s->top->num);
int Ptime, h, m, sec;
Ptime = now_time() - (s->top->ParkTime);
h = Ptime / H;
m = Ptime % H / M;
sec = Ptime % M;
printf("停车时间 计时:%d:%d:%d\n\n", h, m, sec);
Push(t, Pop(s));
}
while(t->top)
{
Push(s, Pop(t));
}
}
void SearchWaitInfo(Q *q)
{
system("clear");
int count;
Node *t = q->front->next;
while(t)
{
count++;
t = t->next;
}
Node *p = q->front->next;
if (NULL == p)
{
printf("没有车辆在等候中!\n");
}
else
{
printf("等候车辆数量:%d\n", count);
printf("等候车牌号 :\n");
while(p)
{
printf("%d\n", p->num);
p = p->next;
}
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "ParkingLot.h"
int main()
{
List *record;
Stack *park;
Stack *back;
Q *wait;
int choice;
int ret;
int flag = 1;
ret = InitList(&record);
if (FAILURE == ret)
{
printf("Init Failure!\n");
}
ret = InitStack(&park);
if (FAILURE == ret)
{
printf("Init Failure!\n");
}
ret = InitStack(&back);
if (FAILURE == ret)
{
printf("Init Failure!\n");
}
ret = InitQueue(&wait);
if (FAILURE == ret)
{
printf("Init Failure!\n");
}
welcome();
while(flag)
{
menu();
scanf("%d", &choice);
switch (choice)
{
case 1:
ParkInfo(park, wait);
break;
case 2:
LeaveInfo(park, back, wait, record);
break;
case 3:
SearchCarInfo(park, back, wait, record);
break;
case 4:
SearchRecord(park, back, wait, record);
break;
case 5:
SearchParkInfo(park, back);
break;
case 6:
SearchWaitInfo(wait);
break;
case 0:
exit(1);
default:
printf("您输入的指令有误,请重新输入!\n");
break;
}
printf("是否继续使用(1/0):\n");
scanf("%d", &flag);
getchar();
}
return 0;
}
ParkingLot.h
#ifndef _PARKINGLOT_H_
#define _PARKINGLOT_H_
#define SUCCESS 1000
#define FAILURE 1001
struct list //记录车辆是否来过停车场
{
int num; //车牌号
int time; //停车时间
struct list *next;
};
typedef struct list List;
struct node //结点信息
{
int num; //车牌号
char InitTime[30]; //入场时间(字符)
int ParkTime; //入场时间(数值)
struct node *next;
};
typedef struct node Node;
struct stack //停车栈,让路栈
{
Node *top;
int length;
};
typedef struct stack Stack;
struct queue //等候队列
{
Node *front;
Node *rear;
};
typedef struct queue Q;
void welcome();
void menu();
int InitList(List **l);
int InsertList(List *l, int e);
int FindList(List *l, int e);
int InitStack(Stack **s);
int FullStack(Stack *s);
int Push(Stack *s, int e);
int Pop(Stack *s);
int GetTop(Stack *s);
int EmptyStack(Stack *s);
int FindStack(Stack *s, Stack *t, int e);
int InitQueue(Q **q);
int EnterQueue(Q *q,int e);
int EmptyQueue(Q *q);
int DeleteQueue(Q *q);
int FindQueue(Q *q, int e);
void ParkInfo(Stack *s, Q *q);
void LeaveInfo(Stack *s, Stack *t, Q *q, List *l);
void SearchCarInfo(Stack *s, Stack *t, Q *q, List *l);
void SearchRecord(Stack *s, Stack *t, Q *q, List *l);
void SearchParkInfo(Stack *s, Stack *t);
void SearchWaitInfo(Q *q);
#endif