您的位置:首页 > 理论基础 > 数据结构算法

数据结构简单的停车管理系统

2014-07-19 15:08 435 查看
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
using namespace std;

#define MAX_STOP 5      //停车场里能停放的汽车的数目的最大数目
#define MAX_PAVE 100    //便道上能停放的汽车的数目的最大数目
/*
*表示汽车的状态的结构体结构体的变量为CAR
*/
typedef struct
{
char *license_plate;     //汽车牌照号码,定义为一个字符指针类型
char state;                //汽车当前状态,字符s表示停放在停车位上
}CAR,*CA;                       //字符p表示停放在便道上,每辆车的初始状态用字符i来表示

////////////////////////////////////////////////////////////////
/*
*停车场里的车子的信息
*/
typedef struct
{
CAR STOP[MAX_STOP];         //各汽车信息的存储空间
int top;                //用来指示栈位置的静态指针
}STOPPING,*STOP;

////////////////////////////////////////
/*
*由于便道上的汽车是先进先出的所以用一个队列来表示
*该结构体用来对便道生的汽车进行管理
*/
typedef struct
{
CAR PAVE[MAX_PAVE];          //个汽车的存储空间
int  front,rear;             //用来指示对头和队尾的静态指针
}PAVEMENT,*PAVEM;

////////////////////////////////////////////////////////////////////////
/*
*该结构体的功能是把从停车场里退出来的车子的信息存储
用栈来表示他的后进先出
*/
typedef struct
{
CAR BUFFER[MAX_STOP];          //各汽车的信息存储空间
int top;             //用来指示栈顶的静态指针
}BUF,*BUFF;

///////////////////////////////////////////////////////////////////////
/*
*本函数的作用是用来对着个程序的使用的说明
*用于程序的功能介绍和操作提示模块
*/
void welcome()
{
printf("    \2欢迎使用本程序\2\n");
printf("    \2本程序为停车场的模拟管理程序,有车到来请按【C】\n");
printf("    \2然后根据屏幕提示相进行关的操作,有车要走时请按【L】\n");
printf("    \2然后根据屏幕提示相进行关的操作,查看停车场状态和便道上的状态请按【D】\n");
printf("    \2然后根据屏幕提示进行相关操作,退出程序是请按【Q】\n");
printf("    \2请选择你要做的操作!!!!\n");

}

////////////////////////////////////////////////////////////////////////////
/*
*该函数是用来初始化停车位的栈
*/
void init_stopping(STOP &p)
{
p=new STOPPING;                        //为停车位的指针开辟空间是它指向一个停车位的栈的空间
p->top=-1;                          //初始化的时候把栈顶的静态指针赋值为-1
for(int i=0;i<MAX_STOP;i++)             //使用循环语句分别为的汽车这个元素赋值初始化
{
p->STOP[i].license_plate="null";         //当这辆车没有停在停车场的时候它的牌照初始化为null
p->STOP[i].state='n';                   //车没有停到这个位置的时候,把汽车信息的状态赋值为n
}
}
////////////////////////////////////////////////////////////////////////////
/*
*该函数是用来对辅助栈的初始化
*/
void init_buff(BUFF &p)
{
p=new BUF;                        //为停车位的指针开辟空间是它指向一个停车位的栈的空间
p->top=-1;                             //初始化的时候把栈顶的静态指针赋值为-1
for(int i=0;i<MAX_STOP;i++)             //使用循环语句分别为的汽车这个元素赋值初始化
{
p->BUFFER[i].license_plate="null";       //初始化为null
p->BUFFER[i].state='s';                  //汽车信息的状态赋值为s
}

}

////////////////////////////////////////////
/*
*初始化便道队列
*/
void init_pavement(PAVEM &p)
{
p=new PAVEMENT;                    //开辟空间
p->front=p->rear=-1;                     //使队首和队尾的静态的指针初始化为-1
for(int i=0;i<MAX_PAVE;i++)                   //利用循环给队列的元素附初始化的值
{
p->PAVE[i].license_plate="";              //初始化为空
p->PAVE[i].state='n';                //初始化为n
}
}

///////////////////////////////////////////////////////
/*
*当有车子来的时候进行处理的函数
车子来的时候首先进路便道,要是便道没有车子的话那么在进路入停车场
要停车场的车子已经满了则不进路入停车场
*/
void come(PAVEM &p,STOP &q)
{
system("cls");
printf("\2\3 有车子想进入停车场的话先进入便道请输入y或者Y,没有车子想进入的话输入n或者N \2\3\n");
char ch,*car=new char();
cin>>ch;
getchar();
ch=tolower(ch);
//cout<<ch<<endl;
if(ch=='y')
{

cout<<"请输入车子的牌照"<<endl;
cin>>car;
getchar();
p->rear=(p->rear+1)%MAX_PAVE;
if(p->front==p->rear)
{
cout<<"由于停车的人太多了,便道上的车子已经满了不能在停车了,抱歉\n";
return ;
}
p->PAVE[p->rear].license_plate=car;
p->PAVE[p->rear].state='p';

if(q->top==4)
{
cout<<"停车场的车子已经满了不能进入停车场,对你带来的不便请见谅"<<endl;
cout<<"\2\3我将把你的车子停到便道去,一旦有空的车位,马上给你停进去,请输入回车键返回\2\3"<<endl;
}
else
{
q->top++;
p->front=(p->front+1)%MAX_PAVE;
q->STOP[q->top].license_plate=p->PAVE[p->front].license_plate;
q->STOP[q->top].state='s';
//cout<<q->STOP[q->top].license_plate<<"  "<<q->STOP[q->top].state<<endl;
cout<<"\3\3牌照为"<<q->STOP[q->top].license_plate<<"的汽车进入停车场的"<<(q->top+1)<<"号车位\3\3"<<endl;
cout<<"\2\3按回车键继续程序的运行\2\3"<<endl;

}
getchar();
system("cls");
}
else
{
return ;
}
}

////////////////////////////////////////////
void leave(PAVEM &p,STOP &q,BUFF &b)
{
system("cls");
if(q->top==-1)
{
cout<<"亲,目前停车场还没有停车,所以不会有车子要退出去,3秒后我将退出次函数"<<endl;
Sleep(1000);
cout<<"\a"<<"☆";
Sleep(1000);
cout<<"\a"<<"★";
Sleep(1000);
cout<<"\a"<<"☆"<<endl;
return ;
}
cout<<"请你输入你要离开的车子的车牌照,或者停在几号车位的车子离开"<<endl;
int i;
cout<<"输入1则指定离开车子的车牌照号,输入2则指定位置的车子离开"<<endl;
cin>>i;
while(i!=1&&i!=2)
{
cout<<"你的输入有误请从新输入。。。。亲"<<endl;
cin>>i;
}
if(i==1)
{
int k;
cout<<"亲!!请输入你要指定退出停车场的车子的牌照号码!\n";
char *ch=new char();
k=0;
cin>>ch;
int t=0;
for(int i=0;i<=q->top;i++)
{
if(strcmp(q->STOP[i].license_plate,ch)==0)
{
t=1;
k=i;
break;
}
}
while(t!=1)
{
cout<<"在停车场没有找到你要找的车牌号,继续找请输入y或者N,退出则输入n或者N"<<endl;
char c;
cin>>c;
c=tolower(c);
if(c=='y')
{
cout<<"亲!!请输入你要指定退出停车场的车子的牌照号码!\n";
char *ch=new char();
k=0;
cin>>ch;
int t=0;
for(int i=0;i<=q->top;i++)
{
if(strcmp(q->STOP[i].license_plate,ch)==0)
{
t=1;
k=i;
break;
}
}
}
else
{
cout<<"亲,按照你的要求我们将在3秒后退出次操作"<<endl;
Sleep(1000);
cout<<"\a"<<"☆";
Sleep(1000);
cout<<"\a"<<"★";
Sleep(1000);
cout<<"\a"<<"☆"<<endl;
system("cls");
return ;
}
}
cout<<"已经在停车场找到了你所描述的车子,将会马上退出停车场\n"<<endl;
for(;q->top>k;q->top--)
{
b->top++;
b->BUFFER[b->top].license_plate=q->STOP[q->top].license_plate;
cout<<"牌照为"<<q->STOP[q->top].license_plate<<"的汽车暂时退出停车车位"<<endl;
}
cout<<"牌照为"<<q->STOP[q->top].license_plate<<"的汽车从停车场开走了\n";
q->top--;
for(;b->top>-1;b->top--)
{
q->top++;
q->STOP[q->top].license_plate=b->BUFFER[b->top].license_plate;
cout<<"牌照为"<<q->STOP[q->top].license_plate<<"的汽车停回停车位的"<<(q->top+1)<<"车位"<<endl;
}
while(q->top<4&&p->front!=p->rear)
{
q->top++;
p->front=(p->front+1)%MAX_PAVE;
q->STOP[q->top].license_plate=p->PAVE[p->front].license_plate;
q->STOP[q->top].state='s';
cout<<"牌照号为"<<q->STOP[q->top].license_plate<<"的汽车从便道进入停车的"<<(q->top+1)<<"车位"<<endl;
}
cout<<"按回车键继续程序的运行\n";
getchar();
getchar();
}
if(i==2)
{
cout<<"亲请输入你要让停车场几号位置的车走,输入位置的的序号1--5"<<endl;
int x;
cin>>x;
while(x<1||x>5)
{
cout<<"你输入的位置有误请从新输入位置号为1到5号!!!!!!"<<endl;
cin>>x;
}
if(x-1>q->top)
{
cout<<"这没的你要退出的车子,回车键将返回主函数"<<endl;
}
else
{
cout<<"亲!!!已经找到了你要退出的车的位置,马上把车子退出停车场"<<endl;
for(;q->top>x-1;q->top--)
{
b->top++;
b->BUFFER[b->top].license_plate=q->STOP[q->top].license_plate;
cout<<"牌照为"<<q->STOP[q->top].license_plate<<"的汽车暂时退出停车车位"<<endl;
}
cout<<"牌照为"<<q->STOP[q->top].license_plate<<"的汽车从停车场开走了\n";
q->top--;
for(;b->top>-1;b->top--)
{
q->top++;
q->STOP[q->top].license_plate=b->BUFFER[b->top].license_plate;
cout<<"牌照为"<<q->STOP[q->top].license_plate<<"的汽车停回停车位的"<<(q->top+1)<<"车位"<<endl;
}
while(q->top<4&&p->front!=p->rear)
{
q->top++;
p->front=(p->front+1)%MAX_PAVE;
q->STOP[q->top].license_plate=p->PAVE[p->front].license_plate;
q->STOP[q->top].state='s';
cout<<"牌照号为"<<q->STOP[q->top].license_plate<<"的汽车从便道进入停车的"<<(q->top+1)<<"车位"<<endl;
}
cout<<"按回车键继续程序的运行\n";
getchar();
getchar();
}
}
system("cls");
}
///////////////////////////////////////////////////////////////////
void display(PAVEM &p,STOP &q)
{
if(q->top>-1)
{
cout<<"停车位的   情况:"<<endl;
for(int i=0;i<=q->top;i++)
{
cout<<(i+1)<<"车位----"<<q->STOP[i].license_plate<<endl;
}
}
else
{
cout<<"   停车场没的有停车!!"<<endl;
}
if(p->front!=p->rear)
{
cout<<"便道上的    情况"<<endl;
for(int i=p->front+1,t=1;i<=p->rear;i++,t++)
{
cout<<t<<"位置----"<<p->PAVE[i].license_plate<<endl;
}
}
else
{
cout<<"便道上没有停车"<<endl;
}
cout<<"按回车键程序继续运行"<<endl;
getchar();
getchar();
system("cls");
}

//////////////////////////////////////////////////////////////////
int main()
{
system("color 3E");
char ch;
//welcome();
STOP p;
BUFF q;
PAVEM  b;
init_stopping(p);
init_buff(q);
init_pavement(b);
do
{
welcome();
cin>>ch;
if(ch=='C'||ch=='c')
come(b,p);
else if(ch=='l'||ch=='L')
leave(b,p,q);
else if(ch=='d'||ch=='D')
display(b,p);
}while(ch!='q'&&ch!='Q');
cout<<"     \2\1谢谢使用停车管理!!!!\n\n"<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: