#include<iostream>
//#include<stdio.h>
using namespace std;
#define MAX_STOP 5
#define MAX_PAVE 100
typedef struct{
int license;
char *state;
}car;
typedef struct{
car stop[MAX_STOP];
int top_s;
}stopping;
typedef struct{
car pave[MAX_PAVE];
int front,rear;
int num;
}pavement;
typedef struct{
car buffers[MAX_STOP];
int top_b;
}buffer;
static int end_num=1;
stopping *Init_stop(){
stopping *cars;
cars=new stopping;
cars->top_s=-1;
return cars;
}
buffer *Init_out(){
buffer *outcars;
outcars=new buffer;
outcars->top_b=-1;
return outcars;
}
pavement *Init_pave(){
pavement *pavecar;
pavecar=new pavement;
pavecar->front=pavecar->rear=MAX_PAVE-1;
pavecar->num=0;
return pavecar;
}
int Push_car(stopping *cars,car n){
if(cars->top_s==MAX_STOP-1)
return 0;
else
{
cars->top_s++;
cars->stop[cars->top_s]=n;
return 1;
}
}
int Out_car(stopping *cars,int i){
car m;
buffer *outcar;
outcar=Init_out();
while(cars->top_s>=i)
{
outcar->top_b++;
outcar->buffers[outcar->top_b]=cars->stop[cars->top_s];
m=cars->stop[cars->top_s];
cars->top_s--;
cout<<"牌照为JF00"<<m.license<<"暂时存入临时停车位."<<endl;
}
cars->top_s--;
while(outcar->top_b>-1)
{
Push_car(cars,outcar->buffers[outcar->top_b]);
m=outcar->buffers[outcar->top_b];
outcar->top_b--;
cout<<"牌照为JF00"<<m.license<<"的汽车停入停车位的"<<cars->top_s+1<<"号车位!"<<endl;
}
return 1;
}
int In_pavecar(pavement *pavecar,car j){
if(pavecar->num==MAX_PAVE){
cout<<"队满"<<endl;
return 0;
}
else{
pavecar->rear=(pavecar->rear+1)%MAX_PAVE;
pavecar->pave[pavecar->rear]=j;
pavecar->num++;
return 1;
}
}
int Push_pavecar(pavement *pavecar,stopping *cars){
car m;
if(pavecar->num==0){
cout<<"队空!"<<endl;
return 0;
}
else{
pavecar->front=(pavecar->front+1)%MAX_PAVE;
cars->top_s++;
cars->stop[cars->top_s]=pavecar->pave[pavecar->front];
m=pavecar->pave[pavecar->front];
cout<<"便道上牌照为JF00"<<m.license<<"的车停入停车场的"<<cars->top_s+1<<"号车位!"<<endl;
pavecar->num--;
return 1;
}
}
int Empty_pavement(pavement *q){
if(q->num==0)
return 1;
else
return 0;
}
int Empty_car(stopping *cars){
if(cars->top_s==-1)
return 1;
else
return 0;
}
void welcome()
{
cout<<" ● 欢迎使用本程序● "<<endl;
cout<<" 本程序为停车场的模拟管理程序,有车到来时请按C键。"<<endl;
cout<<" 然后根据屏幕提示进行相关操作,有车要走时请按l键。"<<endl;
cout<<" 然后根据屏幕提示进行相关操作,显示当前停车状况请按 S 键。"<<endl;
cout<<" 然后根据屏幕提示进行相关操作,要退出程序请按Q键。"<<endl;
cout<<" 请选择您要做的操作!"<<endl;
}
void come(stopping *cars,pavement *stopcars)
{
car n;
n.license=end_num;
end_num++;
// n.state="s";
// cout<<n.state<<endl;
if(Push_car(cars,n))
cout<<"牌照为JF00"<<n.license<<"的汽车停入停车位的"<<cars->top_s+1<<"号车位!"<<endl;
else
{
if(In_pavecar(stopcars,n))
cout<<"牌照为JF00"<<n.license<<"的汽车停入便道的"<<stopcars->num<<"号车位!"<<endl;
}
}
void leave(stopping *cars,int l,pavement *stopcars)
{
if(cars->top_s==-1)
cout<<"停车场没有汽车!" <<endl;
Out_car(cars,l);
if(cars->top_s<=MAX_STOP&&!Empty_pavement(stopcars))
Push_pavecar(stopcars,cars);
}
void display(stopping *cars,pavement *stopcars)
{
car m;
int i,j,t;
i=cars->top_s;
j=stopcars->num;
t=stopcars->front;
if(Empty_car(cars))
cout<<"停车位暂无汽车!"<<endl;
else
{
while(cars->top_s>-1)
{
m=cars->stop[cars->top_s];
cout<<"车牌号为JF00"<<m.license<<"停在"<<cars->top_s+1<<"号车位。"<<endl;
cars->top_s--;
}
}
if(Empty_pavement(stopcars))
cout<<"便道上暂无汽车!"<<endl;
else
{
while(stopcars->num!=0)
{
stopcars->front=(stopcars->front+1)%MAX_PAVE;
m=stopcars->pave[stopcars->front];
cout<<"车牌号为JF00"<<m.license<<"停在便道的"<<stopcars->front+1<<"号车位"<<endl;
stopcars->num--;
}
}
cars->top_s=i;
stopcars->num=j;
stopcars->front=t;
}
void main()
{
stopping *cars;
pavement *stopcars;
int l;
char key;
cars=Init_stop();
stopcars=Init_pave();
do
{
welcome();
key=getchar();
if(key=='C'||key=='c')
{
come(cars,stopcars);
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
while(getchar()!='\n')
{
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
}
}
else if(key=='L'||key=='l')
{
cout<<"请输入要离开的车位号:";
cin>>l;
if((l>cars->top_s+1)||l<-1)
{
if(cars->top_s==-1)
{
cout<<"停车场没有汽车!"<<endl;
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
while(getchar()!='\n')
{
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
}
continue;
}
cout<<"输入错误!请重新输入:";
cin>>l;
}
leave(cars,l,stopcars);
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
while(getchar()!='\n')
{
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
}
}
else if(key=='S'||key=='s')
{
display(cars,stopcars);
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
while(getchar()!='\n')
{
cout<<"请按回车键,继续程序进行。。。"<<endl;
getchar();
}
}
}
while((key!='Q')&&(key!='q'));
delete cars;
delete stopcars;
cout<<"谢谢使用本程序。。。";
}