#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define D (24*60*60)
#define H (60*60)
#define M (60)
#define OK 1
#define ERROR 0
#define MAX_STACK_SIZE 10
typedef int StackData;
typedef int QueueData;
typedef int ElemType;
typedef struct Node
{
int No;
int Timeinit;
}Node;
typedef struct QueueNode
{
struct Node data;
struct QueueNode* next;
} QueueNode;
typedef struct LinkQueue
{
struct QueueNode *rear, *front;
} LinkQueue;
typedef struct SqStackNode
{
int top;
int bottom;
struct Node stack_array[MAX_STACK_SIZE+1] ;
}SqStackNode ;
SqStackNode* InitStack()
{
SqStackNode *S=(SqStackNode *)malloc(sizeof(SqStackNode));
S->bottom=S->top=0;
return (S);
}
int FullStack(SqStackNode *S)
{
return S->top==MAX_STACK_SIZE;
}
int pushStack(SqStackNode *S,Node data)
{
if(FullStack(S))
{
return ERROR;
}
S->top++ ;
(S->stack_array[S->top]).No=data.No ;
(S->stack_array[S->top]).Timeinit=data.Timeinit;
return OK;
}
int popStack(SqStackNode *S,Node *data)
{
if(S->top==0)
{
return ERROR;
}
(*data).No=(S->stack_array[S->top]).No;
(*data).Timeinit=(S->stack_array[S->top]).Timeinit;
S->top--;
return OK;
}
int FinfStack(SqStackNode *S,Node data)
{
int i;
if(S->top==0)
{
return ERROR;
}
for(i=1;i<=S->top;i++)
{
if(S->stack_array[i].No == data.No)
{
return OK;
}
}
return ERROR;
}
LinkQueue* InitQueue (void)
{
LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) );
Q->rear=Q->front=NULL;
return Q;
}
int QueueEmpty ( LinkQueue *Q )
{
return Q->front == NULL;
}
int GetFrontQueue ( LinkQueue *Q, Node *data )
{
if ( QueueEmpty (Q) ) return 0;
(*data).No = (Q->front->data).Timeinit; return 1;
}
int EnQueue ( LinkQueue **Q, Node data)
{
QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) );
(p->data).No = data.No;
(p->data).Timeinit = data.Timeinit;
p->next = NULL;
if ( (*Q)->front == NULL )
{
(*Q)->front = (*Q)->rear = p;
}
else
{
(*Q)->rear = (*Q)->rear->next = p;
}
return 1;
}
int DeQueue ( LinkQueue **Q, Node *data)
{
if ( QueueEmpty (*Q) )
{
return 0;
}
QueueNode *p = (*Q)->front;
(*data).No = p->data.No;
(*data).Timeinit = p->data.Timeinit;
(*Q)->front = (*Q)->front->next;
if ((*Q)->front == NULL) (*Q)->rear = NULL;
free (p);
return 1;
}
int now_time(void)
{
time_t t1;
time(&t1);
int time=t1%D;
return time;
}
Parking(LinkQueue **Q,SqStackNode *S)
{
int i,time_now;
Node data;
printf("停入车的车牌:\n");
scanf(" %d",&data.No);
for(i=1;i<=S->top;i++)
{
if(S->stack_array[i].No == data.No)
{
printf("已有此号\n");
return ;
}
}
EnQueue(Q,data);
while(!QueueEmpty(*Q))
{
if(FullStack(S))
{
printf("等待中\n");
break;
}
else
{
DeQueue(Q,&data);
data.Timeinit=now_time();
pushStack(S,data);
printf("已停入\n");
}
}
return ;
}
leaving(SqStackNode *S,SqStackNode *B,LinkQueue **Q)
{
if(S->bottom == S->top)
{
printf("车位已空:\n");
}
else
{
Node data;
int i,h,m,s;
float charge;
int time_now,parking_time;
printf("要离开的车号:\n");
scanf(" %d",&i);
data.No=i;
if(!FinfStack(S,data))
{
printf("查无此车\n");
return ;
}
else
{
while(S->stack_array[S->top].No != i)
{
popStack(S,&data);
pushStack(B,data);
}
popStack(S,&data);
time_now=now_time();
parking_time=time_now-data.Timeinit;
h = parking_time/H;
parking_time = parking_time%H;
m = parking_time/M;
s = parking_time%M;
charge = 20*h+0.5*(m+1);
printf("出停车场的车号:%d 停车时间:%d:%d:%d 总费用(20¥/h):%g¥\n",data.No,h,m,s,charge);
while(B->bottom != B->top)
{
popStack(B,&data);
pushStack(S,data);
}
while(!FullStack(S)&&(!QueueEmpty(*Q)))
{
DeQueue(Q,&data);
data.Timeinit=now_time();
pushStack(S,data);
}
}
}
}
situation(SqStackNode *S,LinkQueue **Q)
{
Node data;
int i;
int time_now,parking_time;
int h,m,s;
struct QueueNode *p;
int wait_count=0;
p=(*Q)->front;
if(p == NULL)
{
printf("等待中的车辆 :0\n");
}
else
{
do
{
wait_count++;
p=p->next;
}while(p!=NULL);
printf("等待中的车辆:%d\n",wait_count);
}
printf("车牌号: ");
for(i=1;i<=S->top;i++)
{
printf("%-10d",S->stack_array[i].No);
if(S->stack_array[i].No == data.No)
{
return OK;
}
}
printf("\n已停时间:");
for(i=1;i<=S->top;i++)
{
time_now = now_time();
parking_time = time_now - S->stack_array[i].Timeinit;
h = parking_time/H;
parking_time = parking_time%H;
m = parking_time/M;
s = parking_time%M;
printf("%02d:%02d:%02d ",h,m,s);
}
printf("\n");
}
int main()
{
int i;
Node data;
SqStackNode *park;
SqStackNode *back;
LinkQueue *wait;
park=InitStack();
back=InitStack();
wait=InitQueue();
while(1)
{
printf("----------停车系统---------\n");
printf(" 1.停车 \n");
printf(" 2.出停车场 \n");
printf(" 3.状态 \n");
printf(" 4.退出 \n");
scanf(" %d",&i);
switch(i)
{
case 1:
{
Parking(&wait,park);
setbuf(stdin,NULL);
getchar();
break;
}
case 2:
{
leaving(park,back,&wait);
setbuf(stdin,NULL);
getchar();
break;
}
case 3:
{
situation(park,&wait);
setbuf(stdin,NULL);
getchar();
break;
}
case 4:
{
return 0;
}
default:
{
break;
}
}
}
return 0;
}