停车场项目需求
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门
外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车
要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)
功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、
查询场内车辆信息、查询等候车辆信息、退出系统。
(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。
头文件1.h
#define a
#define FAILURE 1000
#define SUCCESS 1001
#define PRICE 0.2
#define MAX 5
struct data
{
char num[20];
double time;
double len;
struct data *next;
};
typedef struct data Data;
typedef Data *LinkList;
struct node
{
char num[20];
struct node *next;
};
typedef struct node Node;
struct wait
{
Node *front;
Node *rear;
};
typedef struct wait Wait;
struct stack
{
Node *top;
int count;
};
typedef struct stack Stack;
int DataInit(LinkList *s);
int QueueInit(Wait *L);
int StackInit(Stack **s);
int Push(Stack *s,char* e);
char* Pop(Stack *s);
int StackLength(Stack *s);
char* GetTop(Stack *s);
char* DelQueue(Wait *L);
#endif
主函数
#include <stdio.h>
#include "1.h"
#include <stdlib.h>
#include <string.h>
int Search(LinkList in,LinkList out)
{
char name[20]={0};
LinkList p=in->next;
LinkList q=out->next;
printf("please input the num:\n");
scanf("%s",name);
while(p!=NULL)
{
if(strcmp(p->num,name)==0)
{
printf("Have In!\n");
printf("Num :%s ,Time :%.2lf!\n",p->num,p->time);
break;
}
p=p->next;
}
if(p==NULL)
{
printf("Have Not In!\n");
}
while(q!=NULL)
{
if(strcmp(q->num,name)==0)
{
printf("Have Leave!\n");
printf("Num :%s,Time :%.2lf,Len :%.0lf!\n",q->num,q->time,q->len);
break;
}
q=q->next;
}
if(q==NULL)
{
printf("Still in parking!\n");
}
return SUCCESS;
}
int TraverseStack(Stack *s,LinkList in)
{
Node *t=s->top;
while(t!=NULL)
{
LinkList p=in->next;
while(p!=NULL)
{
if(strcmp(p->num,t->num)==0)
{
printf("Num :%s,Time :%.2lf!\n",p->num,p->time);
break;
}
p=p->next;
}
t=t->next;
}
}
int main()
{
int i,ret;
int l;
char name[20]={0};
LinkList in , out;
Wait s;
Stack *p,*tmp;
char *e;
DataInit(&in);
DataInit(&out);
QueueInit(&s);
StackInit(&p);
StackInit(&tmp);
while(1)
{
printf("***************************************\n");
printf("***************************************\n");
printf("***********1.进车登记******************\n");
printf("***********2.出车登记******************\n");
printf("***********3.按车牌号查询车辆信息******\n");
printf("***********4.查询出入记录**************\n");
printf("***********5.查询场内车辆信息**********\n");
printf("***********6.查询等候车辆信息**********\n");
printf("***********7.退出系统******************\n");
printf("***************************************\n");
printf("***************************************\n");
printf("please input you choice:\n");
scanf("%d",&i);
switch(i)
{
case 1:
l=StackLength(p);
while(l<MAX && QueueLength(s)==0)
{
printf("Please input the in num:\n");
scanf("%s",name);
Push(p,name);
In(&in);
break;
}
/*while(l<MAX && QueueLength(s)!=0)
{
e=DelQueue(&s);
printf("the car will in is:%s\n",e);
Push(p,e);
In(&in);
break;
}*/
while(l>=MAX)
{
printf("The parking is Full! Please input the wait num:\n");
scanf("%s",name);
EnterQueue(&s,name);
break;
}
break;
case 2:
l=StackLength(p);
printf("Len is :%d\n",l);
while(l>0)
{
printf("the num you want out:\n");
scanf("%s",name);
while(strcmp(GetTop(p),name) != 0)
{
Push(tmp,Pop(p));
}
Pop(p);
ret=Out(&out,in);
while(ret==FAILURE)
{
printf("Please input again:\n");
ret=Out(&out,in);
}
while(tmp->top!=NULL)
{
Push(p,Pop(tmp));
}
printf("Top is :%s\n",GetTop(p));
break;
}
while(l-1<MAX && QueueLength(s)!=0)
{
e=DelQueue(&s);
printf("the car will in is:%s\n",e);
Push(p,e);
In(&in);
break;
}
break;
case 3:
Search(in,out);
break;
case 4:
printf("All in car's informatin:\n");
Traverse(in);
printf("\n\n");
printf("All out car's information:\n");
Traverse(out);
printf("\n\n");
break;
case 5:
TraverseStack(p,in);
break;
case 6:
TraverseQueue(&s);
printf("\n");
printf("There are %d cars are waiting!\n",QueueLength(s));
break;
case 7:
exit(1);
break;
default :
printf("Unkown Input!\n");
break;
}
}
return 0;
}
接口函数
#include <stdio.h>
#include "1.h"
#include <stdlib.h>
#include <string.h>
int DataInit(LinkList *s)
{
(*s)=(LinkList)malloc(sizeof(Data));
if((*s)==NULL)
{
return FAILURE;
}
(*s)->next=NULL;
return SUCCESS;
}
int QueueInit(Wait *L)
{
L->front=(Node *)malloc(sizeof(Node));
if(L->front ==NULL)
{
exit(1);
}
L->front->next=NULL;
L->rear=L->front;
return SUCCESS;
}
int StackInit(Stack **s)
{
(*s)=(Stack *)malloc(sizeof(Stack));
if((*s)==NULL)
{
exit(1);
}
(*s)->top=NULL;
(*s)->count = 0;
return SUCCESS;
}
int Push(Stack *s,char *e)
{
Node *p =(Node *)malloc(sizeof(Node));
if(p==NULL)
{
exit(1);
}
strcpy(p->num,e);
p->next=s->top;
s->top=p;
s->count ++;
return SUCCESS;
}
int StackLength(Stack *s)
{
return (s->count);
}
char* Pop(Stack *s)
{
char *e=(char *)malloc(sizeof(char)*20);
if(s->top!=NULL)
{
Node *p=s->top;
s->top=p->next;
strcpy(e,p->num);
free(p);
s->count --;
}
return e;
}
char* GetTop(Stack *s)
{
if(s->top!=NULL)
{
return s->top->num;
}
}
int EnterQueue(Wait *L,char *e)
{
Node *n=(Node *)malloc(sizeof(Node));
if(n==NULL)
{
exit(1);
}
strcpy(n->num,e);
n->next=NULL;
L->rear->next=n;
L->rear=n;
return SUCCESS;
}
char* DelQueue(Wait *L)
{
char *e=(char *)malloc(sizeof(char)*20);
Node *tmp=L->front->next;
if(tmp!=NULL)
{
strcpy(e,tmp->num);
L->front->next=tmp->next;
free(tmp);
}
if(L->front->next==NULL)
{
L->rear=L->front;
}
return e;
}
int TraverseQueue(Wait *L)
{
printf("The Waiting num :\n");
char *e=(char *)malloc(sizeof(char)*20);
Node *tmp=L->front->next;
while(tmp!=NULL)
{
strcpy(e,tmp->num);
printf("%s ",e);
tmp=tmp->next;
}
return SUCCESS;
}
int QueueLength(Wait L)
{
int count=0;
while(L.front->next!=NULL)
{
count++;
L.front=L.front->next;
}
return count;
}
int Traverse(LinkList s)
{
LinkList p=s->next;
if(p==NULL)
{
return FAILURE;
}
while(p!=NULL)
{
printf("Num : %s ,Time : %.2lf ,Len : %.0lf \n ",p->num,p->time,p->len);
p=p->next;
}
return SUCCESS;
}
int In(LinkList *s)
{
LinkList p=(*s);
LinkList n=(LinkList)malloc(sizeof(Data));
printf("please input num:\n");
scanf("%s",n->num);
printf("please input time:\n");
scanf("%lf",&(n->time));
n->next=p->next;
p->next=n;
p=p->next;
return SUCCESS;
}
int Out(LinkList *s,LinkList L)
{
LinkList q=L->next;
LinkList p=(*s);
LinkList n=(LinkList)malloc(sizeof(Data));
printf("please input num:\n");
scanf("%s",n->num);
while(q!=NULL)
{
if(strcmp(q->num,n->num)==0)
{
printf("please input time:\n");
scanf("%lf",&(n->time));
n->len=(n->time-q->time)*60;
n->next=p->next;
p->next=n;
p=p->next;
break;
}
q=q->next;
}
if(q==NULL)
{
printf("no this car!\n");
return FAILURE;
}
return SUCCESS;
}