/*停车场程序-我靠制作*/
#include<iostream>
#include<string>
#define MAX_STOP 7 //停车位个数
#define MAX_PAVE 100 //便道不限制车位的个数,就定义为100
using namespace std;
//描述汽车的结构体
typedef struct
{
char license_plate[7];//汽车牌照号码,定义为一个字符指针类型。
char state;//汽车当前状态,字符s表示停放在停车位上,字符p表示停放在便道上,每辆车的初始状态用字符i来进行表示。
}Car;
//模拟停车位的栈
typedef struct
{
Car STOP[MAX_STOP];//7个位置用来存放汽车对象的栈。
int top;//栈顶指针
}Stopping;
Stopping *s;
//模拟便道的队列
typedef struct
{
Car PAVE[MAX_PAVE]; //用队列来存放汽车对象。
int front,rear;//对头,队尾指针。
int num;
}Pavement;
Pavement *p;
//辅助栈,用来存放开出停车位汽车(后面的)所有汽车的栈
typedef struct
{
Car BUFFER[MAX_STOP];
int top;
}Buffer;
Buffer *b;
/*----------------------------栈的操作-------------------------------*/
//初始化停车位栈
Stopping * Init_Stopping()
{
s=new Stopping;
if(!s)
{
cout<<"空间不足!!!"<<endl;
return NULL;
}
else
{
s->top=-1;
return s;
}
}
//判断是否为空
int Empty_Stopping(Stopping *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
//入栈
int Push_Stopping(Stopping *s,Car c)
{
if(s->top==MAX_STOP-1)
return 0;
else
{
s->top++;
s->STOP[s->top]=c;
return 1;
}
}
//出栈
int Pop_Stopping(Stopping *s,Car *c)
{
if(Empty_Stopping(s))
return 0;
else
{
*c=s->STOP[s->top];
s->top--;
return 1;
}
}
//取栈顶
Car * Top_Stopping(Stopping *s)
{
if(Empty_Stopping(s))
return NULL;
else
return &s->STOP[s->top];
}
//判断栈是否为满
int Full_Stopping(Stopping *s)
{
if(s->top==MAX_STOP-1)
return 1;
else
return 0;
}
/*----------辅助栈----------*/
//初始化辅助栈
Buffer * Init_Buffer()
{
b=new Buffer;
if(!b)
{
cout<<"空间不足!!!"<<endl;
return NULL;
}
else
{
b->top=-1;
return b;
}
}
//判断辅助栈是否空
int Empty_Buffer(Buffer *b)
{
if(b->top==MAX_STOP)
return 1;
else
return 0;
}
//入栈
int Push_Buffer(Buffer *b,Car c)
{
if(b->top==MAX_STOP-1)
return 0;
else
{
b->top++;
b->BUFFER[b->top]=c;
return 1;
}
}
//出栈
int Pop_Buffer(Buffer *b,Car *c)
{
if(Empty_Buffer(b))
return 0;
else
{
*c=b->BUFFER[b->top];
b->top--;
return 1;
}
}
//取栈顶
Car *Top_Buffer(Buffer *b)
{
if(Empty_Buffer(b))
return NULL;
else
return &b->BUFFER[b->top];
}
/*-------------------------队列的操作--------------------------------*/
//初始化队列
Pavement *Init_Pavement()
{
p=new Pavement;
p->front=p->rear=-1;
p->num=0;
return p;
}
//入队
int In_Pavement(Pavement *p,Car c)
{
if(p->num==MAX_PAVE-1)
{
cout<<"便道满!!"<<endl;
return 0;
}
else
{
p->rear++;
p->PAVE[p->rear]=c;
p->num++;
return 1;
}
}
//出队
int Out_Pavement(Pavement *p,Car *c)
{
if(p->num==0)
{
cout<<"没车了!!!"<<endl;
return 0;
}
else
{
p->front++;
*c=p->PAVE[p->front];
p->num--;
return 1;
}
}
//取对头
Car *Top_Pavement(Pavement *p)
{
if(p->num==0)
{
cout<<"没车了!!!"<<endl;
return NULL;
}
else
{
p->front++;
return &p->PAVE[p->front];
}
}
/*-------------------------有关停车场的相关操作----------------------*/
//车来的方法
void car_come()
{
cout<<"请输入车牌号:"<<endl;
Car *car=new Car;
cin>>car->license_plate;
if(Full_Stopping(s)!=1)
{
Push_Stopping(s,*car);
cout<<"牌照为"<<car->license_plate<<"的汽车停入停车位的"<<s->top+1<<"号车位!"<<endl;
}
else
{
In_Pavement(p,*car);
cout<<"牌照为"<<car->license_plate<<"的汽车停入便道位的"<<p->rear+1<<"号车位!"<<endl;
}
}
//车走的方法
void car_leave()
{
Car *c;//存取Pop_Stopping栈弹出的Car对象
Car *c0;
Car *c1;
char car_num[7];
cout<<"请输入要走的车的车牌号码:"<<endl;
cin>>car_num;
Pop_Stopping(s,c);
while(strcmp(car_num,c->license_plate)!=0)
{
Push_Buffer(b,*c);//把车牌号不相等的车放入缓冲栈
Pop_Stopping(s,c);
}
if(strcmp(car_num,c->license_plate)==0&&s->top!=-1)
{
cout<<"牌照为"<<c->license_plate<<"的汽车从停车厂开走;"<<endl;
while(Empty_Buffer(b)!=1)
{
Pop_Buffer(b,c0);
Push_Stopping(s,*c0);
}
//便道的车进入停车位
Out_Pavement(p,c1);
Push_Stopping(s,*c1);
cout<<"牌照为"<<c1->license_plate<<"的汽车从便道上停入停车位的"<<s->top+1<<"车位"<<endl;
}
/*
if(s->top==-1)
{
cout<<"没有找到要开走的车!!!"<<endl;
while(Empty_Buffer(b)!=-1)
{
Pop_Buffer(b,c);
Push_Stopping(s,*c);
}
}
*/
}
//显示状态
void car_display()
{
Car *c;
Car *c1;
cout<<"停车位情况:"<<endl;
int top_b=s->top;//用于保存最初的栈顶值
int front_b=p->front;//用于保存最初队列头指针
if(Empty_Stopping(s)!=1)
{
while(s->top!=-1)
{
c=Top_Stopping(s);
cout<<s->top+1<<"号车位----"<<c->license_plate<<endl;
s->top--;
}
s->top=top_b;//还原栈顶值
}
else
cout<<"还没有车进入停车位!!!"<<endl;
cout<<"便道上情况:"<<endl;
if(p->num!=0)
{
while(p->front!=p->rear)
{
c1=Top_Pavement(p);
cout<<p->front+1<<"号车位----"<<c1->license_plate<<endl;
}
p->front=front_b;//还原头指针
}
else
cout<<"还没有车进入便道上!!!"<<endl;
}
void welcome()
{
cout<<" ● 欢迎使用本程序●"<<endl;
cout<<"本程序为停车场的模拟管理程序,有车到来时请按c键。"<<endl;
cout<<"然后根据屏幕提示进行相关操作,有车要走时请按l键。"<<endl;
cout<<"然后根据屏幕提示进行相关操作,要查询状态请按r键。"<<endl;
cout<<"然后根据屏幕提示进行相关操作,要退出程序请按q键。"<<endl;
}
int main()
{
s=Init_Stopping();
p=Init_Pavement();
b=Init_Buffer();
char key;
int m=100;
while(m)
{
welcome();
cout<<"请选择c,l,r,q"<<endl;
cin>>key;
switch(key)
{
case 'c': car_come(); break;
case 'l': car_leave();break;
case 'r': car_display();break;
case 'q': exit(0);
}
m--;
}
return 0;
}