#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAXS
20//最大停车车位
#define PRICE 8//单价
//车的结构体
struct Cars
{
char numbers[11];//车牌号
int time;//入库时间 以分计算
};
//定义栈的结构体
typedef struct
{
struct Cars *base;
struct Cars *top;
int stacksize;
}Sqstack;
//定义队列元素
typedef struct QNode
{
struct Cars data;
struct QNode *next;
}QNode, *Queueptr;
//定义队列
typedef struct
{
Queueptr front;//对头
Queueptr rear;//队尾
}LinkQueue;
void InitQueue( LinkQueue &Q );//初始化队列1
void EnQueue( LinkQueue &Q );//进队列2
void DeQueue( LinkQueue &Q, struct Cars &temp );//出对列3
void Get_time(int &time);//获取时间4
void InitStack( Sqstack &S );//初始化栈5
void Push( Sqstack &S, struct Cars temp);//进栈6
void Gettop( Sqstack S, struct Cars &temp );//获取栈顶元素7
void Pop( Sqstack &S );//出栈8
void Charge( struct Cars temp );//计算应缴纳的费用 和 存放的总时间9
void Stop( Sqstack &S, LinkQueue &Q );//存车
void Take( Sqstack &S, LinkQueue &Q );//取车
void Print( Sqstack S);
int main()
{
Sqstack s;
LinkQueue q;
InitQueue( q );
InitStack( s );
for(;;)
{
int i;
printf("请根据提示信息进行操作\n");
printf("请按\"1\"键进行存车\n");
printf("请按\"2\"键进行取车\n");
//printf("请按\"3\"打印信息(停车场)\n");
printf("请按\"4\"键退出系统\n");
scanf("%d", &i);
getchar();
switch(i)
{
case 1:Stop( s, q ); break;
case 2:Take( s, q ); break;
//case 3:Print(s);
break;
case 4:exit(0); break;
}
}
return 0;
}
//初始化队列1
void InitQueue( LinkQueue &Q )
{
Q.front = Q.rear = (Queueptr) malloc (sizeof(QNode));
if( !Q.front )
exit(0);
Q.front->next = NULL;
}
//进队列2
void EnQueue( LinkQueue &Q )
{
Queueptr p;
p = (Queueptr) malloc (sizeof(QNode));
if( !p )
exit(0);
printf("输入车牌号:");
gets(p->data.numbers);
Get_time( p->data.time );
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}
//出对列3
void DeQueue( LinkQueue &Q, struct Cars &temp )
{
if( Q.front==Q.rear )//判断是否对空
return ;
strcpy(temp.numbers , Q.front->next->data.numbers);
temp.time = Q.front->next->data.time;
Q.front = Q.front->next;//对头指向后继
}
//获取时间4
void Get_time(int &tim)
{
struct tm *local;
time_t t;
t=time(NULL);
local = localtime(&t);
tim = local->tm_min+60*local->tm_hour;
}
//初始化栈5
void InitStack( Sqstack &S )
{
S.base = (struct Cars*) malloc (MAXS*sizeof(struct Cars));
if( !S.base )
exit(0);
S.top = S.base;
S.stacksize = MAXS;
}
//进栈6
void Push( Sqstack &S, struct Cars temp)
{
if( S.top-S.base>=S.stacksize )//判断是否栈满
return;
strcpy(S.top->numbers , temp.numbers);
Get_time( S.top->time );
++S.top;
}
//获取栈顶元素7
void Gettop( Sqstack S, struct Cars &temp )
{
if( S.base==S.top )
return;
strcpy(temp.numbers , (S.top-1)->numbers);
temp.time = (S.top-1)->time;
}
//出栈8
void Pop( Sqstack &S, struct Cars &temp )
{
if( S.top==S.base )
return;
strcpy(temp.numbers , (S.top-1)->numbers);
temp.time = (S.top-1)->time;
--S.top;
}
void Print( Sqstack S )
{
if( S.top==S.base )
return;
struct Cars temp;
while( S.top!=S.base )
{
Pop(S,temp);
puts(temp.numbers);
}
}
//计算应缴纳的费用 和 存放的总时间9
void Charge( struct Cars temp )
{
int tim;
Get_time(tim);//出栈时刻的时间
tim = tim - temp.time;//存放的时间间隔
printf("存放的总时间为:%d\n", tim);
printf("应缴纳的费用为:%d\n", tim*PRICE);
}
//存车
void Stop( Sqstack &S, LinkQueue &Q )
{
struct Cars temp;
EnQueue( Q );//先进栈
while( S.top-S.base<S.stacksize && Q.front!=Q.rear )//只要栈不满 同时 队不为空 就进栈
{
DeQueue( Q, temp );
Push( S, temp);
}
}
//取车
void Take( Sqstack &S, LinkQueue &Q )
{
struct Cars temp;
char string[11];
//
定义一个中间的栈 用来出栈时用
Sqstack medium;
InitStack(medium);
printf("输入要取走的车牌号:");
gets(string);
Gettop( S, temp );
while( strcmp(temp.numbers, string)!=0)
{
if( S.top==S.base )
{
printf("没有车牌号为%s车的信息!\n", string);
break;
}
strcpy(medium.top->numbers , temp.numbers);
medium.top->time = temp.time;
++medium.top;
Pop( S, temp );
Gettop( S, temp );
}
if( strcmp(temp.numbers, string)==0 )
{
Charge(temp);//计算应缴纳的费用 和 存放的总时间
Pop(S,temp);
}
while( medium.top!=medium.base )
{
Pop( medium, temp);
Push( S, temp);
}
while( S.top-S.base<S.stacksize && Q.front!=Q.rear )//只要栈不满 同时 队不为空 就进栈
{
DeQueue( Q, temp );
Push( S, temp);
}
}