#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define MAXSIZE 1024
static int maxsize;
typedef char ElemType;
typedef struct
{
char num[10];//车牌
time_t starttime,endtime;//进入推出时间
}car; //车
typedef struct Stack
{
int top;
car a;
struct Stack *next;
}Stack;
typedef struct
{
char a[10];
}dat;
typedef struct
{//队列结构定义
dat data[MAXSIZE];
int front;
int rear;
}SequenQueue;
void menu(Stack *cheku,SequenQueue* paidui);//开始菜单
SequenQueue* InitQueue();//申请一个空队
int EmptyQueue(SequenQueue* Q);//判断队空
int FullQueue(SequenQueue* Q);//判断队满
int EnQueue(SequenQueue* Q, ElemType *e);//入队
int DeQueue(SequenQueue* Q, ElemType *e); //出队
Stack* build();//建链表
void tingche(Stack *cheku,SequenQueue* paidui);
int fullstack(Stack *cheku);
void likai(Stack *cheku,SequenQueue* paidui);
void chakan(Stack *cheku,SequenQueue* paidui);
int main()
{
Stack *cheku=build();
SequenQueue* paidui=InitQueue();
printf("请输入车库最大容量:");
scanf("%d",&maxsize);
system("cls");
menu(cheku,paidui);
return 0;
}
void menu(Stack *cheku,SequenQueue* paidui)
{
printf("********** 欢迎来停车 ! **********\n");
printf("********** 请选择一项 **********\n");
printf("********** 1 : park. **********\n");
printf("********** 2 : leave. **********\n");
printf("********** 3 : view. **********\n");
printf("********** 4 : exit. **********\n");
int option;
scanf("%d",&option);
system("cls");
switch(option)
{
case 1:
{
tingche(cheku,paidui);
menu(cheku,paidui);
break;
}
case 2:
{
likai(cheku,paidui);
menu(cheku,paidui);
break;
}
case 3:
{
chakan(cheku, paidui);
menu(cheku,paidui);
break;
}
case 4:
{
printf("********** 欢迎再次使用,谢谢! **********\n");
break;
}
default:{
printf("********** 请输入正确的指令! **********\n");
Sleep(1000);
menu(cheku,paidui);
system("cls");
break;
}
}
}
int fullstack(Stack *cheku)
{
if(cheku->top<maxsize-1)
return 1;
else
return 0;
}
SequenQueue* InitQueue()
{
SequenQueue* Q = NULL;
Q = (SequenQueue*)malloc(sizeof(SequenQueue));
Q->front = Q->rear = 0;
return Q;
}
int DeQueue(SequenQueue* Q, ElemType *e)
{
if(EmptyQueue(Q))
return FALSE;
else
{
strcpy(e,Q->data[Q->front].a);
Q->front=(Q->front+1)%MAXSIZE;
return TRUE;
}
}
int EnQueue(SequenQueue* Q, ElemType *e)
{
if(FullQueue(Q))
{
printf("等待的车辆太多,请下次再来");
return FALSE;
}
strcpy(Q->data[Q->front].a,e);
Q->rear = (Q->rear+1)%MAXSIZE;
return TRUE;
}
int FullQueue(SequenQueue* Q)
{
if((Q->rear+1)%MAXSIZE==Q->front)
{
return TRUE;
}
else
{
return FALSE;
}
}
int EmptyQueue(SequenQueue* Q)
{
if(Q->front == Q->rear)
return TRUE;
else
return FALSE;
}
Stack* build(Stack *cheku,SequenQueue* paidui)
{
Stack* a;
a=(Stack*)malloc(sizeof(Stack));
a->top=-1;
return a;
}
void tingche(Stack *cheku,SequenQueue* paidui)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
printf("请输入车牌号\n");
fflush(stdin);
gets(p->a.num);
if(fullstack(cheku))
{
p->next=cheku->next;
cheku->next=p;
p->a.starttime=time(NULL);
cheku->top++;
printf("停车成功\n");
Sleep(1000);
system("cls");
}
else
{
printf("车库已满请在门口等待\n");
EnQueue(paidui,p->a.num);
Sleep(1000);
system("cls");
}
}
void likai(Stack *cheku,SequenQueue* paidui)
{
char m[10];
Stack *p,*q;
char e[10];
int n=0;
p=cheku;
if(cheku->top==-1)
{
printf("车库为空\n");
Sleep(1000);
system("cls");
}
else
{
printf("请输入离开的车牌:\n");
fflush(stdin);
gets(m);
while(p->next!='\0')
{ double money;
if(strcmp(p->next->a.num,m)==0)
{
q=p->next;
p->next=q->next;
q->a.endtime=time(NULL);
money=(q->a.endtime-q->a.starttime)*0.00139;
char tmp1[100],tmp2[100];
strftime(tmp1,sizeof(tmp1),"%Y-%m-%d %H:%M:%S",localtime(&q->a.endtime));
strftime(tmp2,sizeof(tmp2),"%Y-%m-%d %H:%M:%S",localtime(&q->a.starttime));
printf("停车时间:%s\n",tmp2);
printf("离开时间:%s\n",tmp1);
printf("共停%ds\n",q->a.endtime-q->a.starttime);
printf("收费%.5lf元(一小时五元)\n",money);
Sleep(3000);
free(q);
system("cls");
cheku->top--;
n++;
if(EmptyQueue(paidui)==0)
{
DeQueue(paidui,e);
Stack *d=(Stack *)malloc(sizeof(Stack));
strcpy(d->a.num,e);
d->a.starttime=time(NULL);
d->next=cheku->next;
cheku->next=d;
cheku->top++;
printf("已将等待的第一辆车进入停车场");
Sleep(1000);
system("cls");
}
break;
}
p=p->next;
}
if(n==0)
{
printf("未找到该车辆信息请重试");
Sleep(1000);
system("cls");
}
}
}
void chakan(Stack *cheku,SequenQueue* paidui)
{
if(cheku->top==maxsize-1)
{
printf("车库已满,共有%d的车辆在等候",(paidui->rear-paidui->front+MAXSIZE)%MAXSIZE);
Sleep(1000);
system("cls");
}
else
{
printf("车库还有%d个空位",maxsize-cheku->top-1);
Sleep(1000);
system("cls");
}
}
|