最近学习数据结构的栈,队列,链表知识,遂做一个简单的停车场管理系统。
#include <iostream>
using namespace std;
#define maxsize 14
#define n 3
#define fee 10
//车辆信息
struct car
{
char bb;
int num;
int time;
} ;
//停车场栈
typedef struct stack
{
struct car G[n];
int top;
}SqStack;
//临时让出车信息
struct rangweicar
{
int num;
int time;
};
//临时停车场栈 虚拟
typedef struct stackk
{
struct rangweicar H[maxsize];
int topp;
}SqStackk;
//便道链表队列
typedef struct AQNODE
{
int data;
AQNODE *next;
}AQNODE;
typedef struct linkqueue
{
AQNODE *front, *rear;
int geshu;
}LinkQueue;
//对到达车辆进行操作
void A_cars(SqStack *s,LinkQueue *q,struct car a)
{
AQNODE *t;
if(s->top!=n-1)//停车场没有满,车辆进入
{
(s->top)++;
(s->G[s->top]).bb=a.bb;
(s->G[s->top]).num=a.num;
(s->G[s->top]).time=a.time;
}
else
{
cout<<"停车场已满"<<endl;
t=(AQNODE*)malloc(sizeof(AQNODE));
t->data=a.num;
t->next=NULL;
q->rear->next=t;
q->rear=t;
q->geshu++;
}
}
//车辆离开
int D_cars(SqStack *s,LinkQueue *q,struct car d)
{
int i,j,x,y;
AQNODE *p;
SqStackk *k;
if(d.num=(s->G[s->top]).num)
{
x=d.time-(s->G[s->top]).time;
y=fee*x;
cout<<"停车时间"<<x<<"小时,"<<"停车费用"<<y<<"元"<<endl;
if(q->geshu==0)
{
cout<<"便道为空"<<endl;
(s->top)--;
return 0;
}
else
{
p=q->front->next;
q->front->next=p->next;
(s->G[s->top]).num=p->data;
(s->G[s->top]).time=d.time;
delete p;
q->geshu--;
if(q->front->next==NULL)
{
q->rear=q->front;
}
return 1;
}
}
else
{
for(i=0;i<(s->top);i++)
{
if((s->G[i]).num!=d.num)
continue;
else
break;
}
if(i>=(s->top))
{
cout<<"error"<<endl;
return -1;
}
x=d.time-(s->G[i]).time;
y=fee*x;
cout<<"停车时间"<<x<<"小时,"<<"停车费用"<<y<<"元"<<endl;
k=(SqStackk*)malloc(sizeof(SqStackk));
k->topp=-1;
for(j=(s->top);j<i;j--)
{
k->topp++;
(k->H[k->topp]).num=(s->G[j]).num;
(k->H[k->topp]).time=(s->G[j]).time;
s->top--;
}
s->top--;//车辆离开
while(k->topp>=0)
{
s->top++;
(s->G[s->top]).bb='A';
(s->G[s->top]).num=(k->H[k->topp]).num;
(s->G[s->top]).time=(k->H[k->topp]).time;
k->topp--;
}
if(q->geshu==0)
{
cout<<"便道为空"<<endl;
return 2;
}
else
{
s->top++;
p=q->front->next;
q->front->next=p->next;
(s->G[s->top]).num=p->data;
(s->G[s->top]).time=d.time;
delete p;
q->geshu--;
if(q->front->next==NULL)
q->rear=q->front;
return 3;
}
}
}
//判断车辆状态并执行相关操作
void Judge_Output(SqStack *s,LinkQueue *q,struct car r)
{
if((r).bb=='e'||(r).bb=='E')
cout<<"stop!"<<endl;
else if((r).bb=='p'||(r).bb=='P')
cout<<"停车场车辆数:"<<(s->top)+1<<endl;
else if((r).bb=='w'||(r).bb=='W')
cout<<"便道中车辆数:"<<q->geshu<<endl;
else if((r).bb=='a'||(r).bb=='A')
A_cars(s,q,r);
else if((r).bb=='d'||(r).bb=='D')
D_cars(s,q,r);
else cout<<"error!"<<endl;
}
void main()
{
SqStack *s;
LinkQueue *q;
AQNODE *p;
struct car aa[maxsize];
int i;
s=(SqStack*)malloc(sizeof(SqStack));
s->top=-1;
q=(LinkQueue*)malloc(sizeof(LinkQueue));
p=(AQNODE*)malloc(sizeof(AQNODE));
p->next=NULL;
q->front=q->rear=p;
q->geshu=0;
cout<<"停车场管理系统"<<endl;
cout<<endl;
cout<<"A(a)车辆到达;D(d)车辆离开;P(p)停车场车辆总数;W(w)便道车辆总数;E(e)退出"<<endl;
cout<<endl;
for(i=0;i<maxsize;i++)
{
cout<<"请输入汽车状态,车牌号和时间:"<<endl;
cin>>aa[i].bb>>aa[i].num>>aa[i].time;
Judge_Output(s,q,aa[i]);
if(aa[i].bb=='E'||aa[i].bb=='e')
break;
}
}
有什么不足,欢迎指正哦。不懂的地方也可以问问我。