停车场项目是我们学完栈与队列后的一个综合项目,下面贴一下具体要求。
停车场项目需求
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)
功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、查询场内车辆信息、查询等候车辆信息、退出系统。
(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。
头文件
#ifndef _PARKLOT_H_
#define _PARKLOT_H_
#define maxsize 10
#define success 100001
#define failure 100002
#define waiting 100003
typedef int elemtype;
struct parknode //parknode
{
char data[20];
elemtype intime;
struct parknode *next;
};
typedef struct parknode Park;
typedef Park* LinkPark;
struct stack
{
LinkPark top;
int count;
};
typedef struct stack Stack;
struct waitnode //waitnode
{
char data[20];
struct waitnode *next;
};
typedef struct waitnode Wait;
typedef Wait* LinkWait;
struct queue
{
LinkWait front;
LinkWait rear;
};
typedef struct queue Queue;
struct record //memory record
{
char data[20];
elemtype intime;
elemtype outtime;
struct record *next;
};
typedef struct record Record;
struct recordstack
{
Record *top;
int count;
};
typedef struct recordstack RecordStack;
int InitStack(Stack **S);
int InitQueue(Queue *Q);
int InitRecordStack(RecordStack **R);
int CheckIn(Stack *P, Queue *W, elemtype t, int i);
int CheckOut(Stack *P, Stack *T, RecordStack *R, elemtype t);
int AutoIn(Stack *P, Stack *T, Queue *W, elemtype i, int l);
int SearchInfo(RecordStack *R, Stack *P, Queue *W);
int SearchAll(RecordStack *R, Stack *P, Queue *W);
int SearchPark(Stack *P, elemtype timenow);
int SearchWait(Queue *W);
#endif
接口函数
#include <stdio.h>
#include "ParkLot.h"
#include <stdlib.h>
#include <string.h>
int InitStack(Stack **S)
{
(*S) = (Stack *)malloc(sizeof(Stack));
if(NULL == (*S))
{
return failure;
}
(*S)->top = NULL;
(*S)->count = 0;
return success;
}
int InitQueue(Queue *Q)
{
Q->front = (LinkWait)malloc(sizeof(Wait));
if(NULL == Q->front)
{
return failure;
}
Q->rear = Q->front;
Q->rear->next = NULL;
Q->rear->data[20] = 0;
return success;
}
int InitRecordStack(RecordStack **R)
{
(*R) = (RecordStack *)malloc(sizeof(RecordStack));
if(NULL == (*R))
{
return failure;
}
(*R)->top = NULL;
(*R)->count = 0;
return success;
}
int CheckIn(Stack *P, Queue *W, elemtype t, int i)
{
LinkPark p = (LinkPark)malloc(sizeof(Park));
LinkWait w = (LinkWait)malloc(sizeof(Wait));
char name[20] = {0};
scanf("%s", name);
if(strlen(name) > 20)
{
printf("Please Input right number!\n");
return failure;
}
int k = P->count;
if(NULL == p || NULL == w)
{
return failure;
}
if(k < i )
{
strcpy(p->data, name);
p->next = P->top;
p->intime = t;
P->top = p;
P->count++;
return success;
}
else
{
strcpy(w->data, name);
w->next = NULL;
W->rear->next = w;
W->rear = w;
return waiting;
}
}
int CheckOut(Stack *P, Stack *T, RecordStack *R, elemtype t)
{
char name[20] = {0};
scanf("%s", name);
int k = P->count;
Record *r = (Record *)malloc(sizeof(Record));
if(NULL == r)
{
return failure;
}
while(k > 0)
{
LinkPark p = P->top;
LinkPark tmp;
if(strcmp(name, p->data) != 0)
{
tmp = p;
p = p->next;
P->top = p;
P->count--;
tmp->next = T->top;
T->top = tmp;
T->count++;
}
else
{
strcpy(r->data, name);
r->intime = p->intime;
r->outtime = t;
r->next = R->top;
R->top = r;
R->count++;
P->top = p->next;
free(p);
P->count--;
return success;
}
k--;
}
return failure;
}
int AutoIn(Stack *P, Stack *T, Queue *W, elemtype i, int l)
{
int j = T->count;
char name[20];
while(j > 0)
{
LinkPark tmp = T->top;
T->top = tmp->next;
T->count--;
tmp->next = P->top;
P->top = tmp;
P->count++;
j--;
}
if(W->front->next != NULL)
{
sleep(1);
printf("Waiting car is parking....New coming car please wait!\n");
sleep(2);
LinkWait w = W->front->next;
strcpy(name, w->data);
W->front->next = w->next;
free(w);
if(NULL == W->front->next)
{
W->rear = W->front;
}
LinkPark p =(LinkPark)malloc(sizeof(Park));
if(NULL == p)
{
return failure;
}
strcpy(p->data, name);
p->intime = i;
p->next = P->top;
P->top = p;
P->count++;
return success;
}
return failure;
}
int SearchInfo(RecordStack *R, Stack *P, Queue *W)
{
Record *r = R->top;
LinkPark p = P->top;
LinkWait w = W->front->next;
char name[20];
scanf("%s", name);
if(strlen(name) > 20)
{
printf("Please Input right number!\n");
return failure;
}
while(r != NULL)
{
if(strcmp(name, r->data) == 0)
{
printf("Licence Plate:%s\nCheckInTime:%d\nCheckOutTime:%d\nParkingTime:%ds\n", r->data, r->intime, r->outtime, r->outtime - r->intime);
return success;
}
r = r->next;
}
while(p != NULL)
{
if(strcmp(name, p->data) == 0)
{
printf("Licence Plate:%s\nCheckInTime:%d\nThis car now is in parklots!\n", p->data, p->intime);
return success;
}
p = p->next;
}
while(w != NULL)
{
if(strcmp(name, w->data) == 0)
{
printf("Licence Plate:%s\nThis car is waiting for parking!\n", w->data);
return success;
}
w = w->next;
}
return failure;
}
int SearchAll(RecordStack *R, Stack *P, Queue *W)
{
Record *r = R->top;
LinkPark p = P->top;
LinkWait w = W->front->next;
printf("The histroy:\n");
while(r != NULL)
{
printf("Licence Plate:%s CheckInTime:%d CheckOutTime:%d ParkingTime:%ds\n", r->data, r->intime, r->outtime, r->outtime - r->intime);
r = r->next;
}
printf("The now car here:\n");
while(p != NULL)
{
printf("Licence Plate:%s CheckInTime:%d \n", p->data, p->intime);
p = p->next;
}
printf("The waiting car:\n");
while(w != NULL)
{
printf("Licence Plate:%s \n", w->data);
w = w->next;
}
return success;
}
int SearchPark(Stack *P, elemtype timenow)
{
LinkPark p =P->top;
printf("Parking Information:\n");
while(p != NULL)
{
printf("Licence Plate:%s CheckInTime:%d ParkingTime:%ds \n", p->data, p->intime, timenow - p->intime);
p = p->next;
}
return success;
}
int SearchWait(Queue *W)
{
LinkWait w = W->front->next;
printf("The Waiting Car Information:\n");
while(w != NULL)
{
printf("Licence Plate:%s \n", w->data);
w = w->next;
}
return success;
}
主函数
#include <stdio.h>
#include "ParkLot.h"
#include <stdlib.h>
int main()
{
Stack *park;
Stack *tmp;
Queue wait;
RecordStack *record;
int ret, n;
elemtype intime, outtime, nt;
if(InitStack(&park) != success || InitStack(&tmp) != success || InitQueue(&wait) != success || InitRecordStack(&record) != success)
{
printf("Init failed!\n\n");
}
else
{
printf("Init success\n\n");
}
printf("Please Set the Maxsize of ParkLots:\n");
scanf("%d",&n);
while(n < 0)
{
printf("Please Input Right Number!\n");
scanf("%d",&n);
}
printf("Set Success!\n");
sleep(1);
system("clear");
while(1)
{
printf("******************************************************************************\n\n");
printf("***********************************WELCOME!***********************************\n\n");
printf("******************************************************************************\n\n");
printf("***1.进车登记 2.出车登记*********\n");
printf("***3.按车牌号查询信息 4.查询出入记录*****\n");
printf("***5.查询场内车辆信息 6.查询等候车辆信息**\n");
printf("***7.退出系统 ***\n");
printf("******************************************************************************\n\n");
printf("******************************************************************************\n\n");
int choice;
scanf("%d", &choice);
if(choice > 7 || choice < 1)
{
printf("Error!\n");
return 1;
}
switch (choice)
{
case 1:
system("clear");
printf("Please Input Incar's Licence Number:\n");
intime = time(NULL);
ret = CheckIn(park, &wait, intime, n);
if(waiting == ret)
{
printf("Parklot Full !Please wait!\n\n");
}
else if(success == ret)
{
printf("Register in Success!\n\n");
}
else
{
printf("Register in Failed!\n\n");
}
printf("The Left Carport is :%d\n\n",n - park->count);
break;
case 2:
system("clear");
printf("Pleaese Input Outcar's Licence Number:\n");
outtime = time(NULL);
ret = CheckOut(park, tmp, record, outtime);
if(success == ret)
{
printf("Register out Success!\n\n");
}
else
{
printf("Register out Failed! The Car is not here!\n\n");
int j = tmp->count;
char name[20];
while(j > 0)
{
LinkPark tmp1 = tmp->top;
tmp->top = tmp1->next;
tmp->count--;
tmp1->next = park->top;
park->top = tmp1;
park->count++;
j--;
}
sleep(1);
break;
}
intime = time(NULL);
ret = AutoIn(park, tmp, &wait, intime, n);
if(success == ret)
{
printf("WaitingCar has Parked Success!\n");
}
printf("The Left Carport is :%d\n\n",n - park->count);
break;
case 3:
system("clear");
printf("Please Input the licence plate you want to search:\n");
ret = SearchInfo(record, park, &wait);
if(failure == ret)
{
printf("Search failed!This car haven't been here!\n\n");
}
else
{
printf("Search Success!\n\n");
}
break;
case 4:
ret = SearchAll(record, park, &wait);
if(failure == ret)
{
printf("Search Failed!\n\n");
}
else
{
printf("Search All Information Success!\n\n");
}
break;
case 5:
nt = time(NULL);
ret = SearchPark(park, nt);
if(failure == ret)
{
printf("Search Failed!\n\n");
}
else
{
printf("Search Parking Information Success!\n\n");
}
break;
case 6:
ret = SearchWait(&wait);
if(failure == ret)
{
printf("Search Failed!\n\n");
}
else
{
printf("Search Waiting Information Success!\n\n");
}
break;
case 7:
exit(1);
break;
default:
printf("Error!\n");
break;
}
}
return 0;
}