停车场管理系统代码(c++)
- 格式:doc
- 大小:60.00 KB
- 文档页数:8
#include <iostream>#include <string>using namespace std;const int MAX_STOP=4; //定义停车场最大停车数const int MAX_PLATE=10; //定义车牌号最大长度//数据结构定义//定义存储汽车信息的结构体typedef struct{char license_plate[MAX_PLATE];//汽车牌照号码,定义为一个字符指针类型char state; //汽车当前状态,字符p表示停放在停车位上,字符s表示停放在便道上,每辆车的初始状态用字符i来进行表示}CAR;//定义模拟停车场的栈结构typedef struct{CAR STOP[MAX_STOP]; //汽车信息的存储空间int top; //用来指示栈顶位置的静态指针}SeqStack;//定义模拟便道的队列结构typedef struct node{CAR W AIT; //汽车信息的存储空问struct node *next; //用来指示队列位置的动态指针}QNode; //链队列节点的类型//定义链队列的收尾指针typedef struct{QNode *front,*rear;}LQueue; //将头尾指针封装在一起的链队//函数声明int Empty_LQueue(LQueue *q); //判队空int LeaveCheck(SeqStack parking,char *license_plate); //检查离开的车是否在停车场中int QueueLength(LQueue *q); //判队长度int Out_LQueue(LQueue *&sidewalk,char *license_plate); //出队操作int StackEmpty(SeqStack parking); //判断栈是否为空int StackFull(SeqStack parking); //判断栈是否为满int StackPop(SeqStack &parking); //出栈操作int StackTop(SeqStack parking, char *license_plate); //取栈项元素void Car_come(SeqStack &parking,LQueue *&sidewalk); //有车到来时的操作void Car_leave(SeqStack &parking,LQueue *&sidewalk); //有车离开的操作void Display(SeqStack parking); //显示停车场内的所有信息调试时用void InitStack(SeqStack &parking); //初始化栈void InitList(LQueue *&sidewalk); //初始化队列void In_LQueue(LQueue *&sidewalk, char *license_plate); //进队操作void Input_Check(char *license_plate); ////检验输入的车牌是否合法void StackPush(SeqStack &parking,char *license_plate); //进栈操作void main(){//定义变量SeqStack parking;LQueue *sidewalk= NULL;char *choice= new char;int flag=1; //定义一个变量判断是否退出//初始化一个为空的停乍场InitStack(parking);//初始化一个为空的便道InitList(sidewalk);//运行界面及功能选择while(flag){cout<<" 停车场模拟管理系统"<<endl;cout<<"**************************************************"<<endl;cout<<"\t有车到来时请按[C]键"<<endl<<endl;cout<<"\t有车要走时请按[l]键"<<endl<<endl;cout<<"\t查看停车场请按[D]键"<<endl<<endl;cout<<"\t要退出系统请按[Q]键"<<endl<<endl;cout<<"**************************************************"<<endl;cout<<"请选择操作:";gets(choice);if(1!=strlen(choice)){cout<<"请正确输入选项!";continue;}else{switch(*choice){case 'c':case 'C':{Car_come(parking,sidewalk);break;}case'l' :case'L':{Car_leave (parking,sidewalk);break;}case'q':case 'Q':{flag=0;break;}case 'd':case 'D':{Display(parking);break;}default:cout<<"选择不正确!请重新选择"<<endl;}}}}//有车到来时的操作void Car_come(SeqStack &parking, LQueue *&sidewalk){//定义变量char license_plate[MAX_PLATE];cout<<"请输入车辆的车牌号码:";Input_Check(license_plate);//判断停车场是否已满,满则进入便道,不满进入停车场if(StackFull(parking)){In_LQueue(sidewalk,license_plate); //进入便道cout<<"停车场已满清在便道等候,您的位置为"<<QueueLength(sidewalk)<<endl;}else{StackPush(parking,license_plate); //进入停车场cout<<"请进入停车场中的"<<parking.top+1<<"号停车位"<<endl;}}//有车离开时的操作void Car_leave(SeqStack &parking, LQueue *&sidewalk){//定义变量SeqStack tmpparking; //定义临时停车场char leave_license_plate[MAX_PLATE];//要离开的车牌号char license_plate[MAX_PLATE]; //存放从停车场中读出来的车牌信息InitStack(tmpparking); //初始化临时停车场//判断停车场中是否有车if(StackEmpty(parking)){cout<<"当前停车场中没有车"<<endl;return; //退出子函数}cout<<"请输入要离开的车牌照:";Input_Check(leave_license_plate);cout<<"当前停车场中有"<<parking.top+1<<"辆车"<<endl;if(LeaveCheck(parking,leave_license_plate))//判断车是否在停车场中{//车在停车场中cout<<"您的车在"<<LeaveCheck(parking,leave_license_plate)<<"号车位上"<<endl;while(StackTop(parking,license_plate)&&(strcmp(parking.STOP[parking.top].license_plate, leave_license_plate)!=0)){strcpy(parking.STOP[parking.top].license_plate, license_plate);cout<<"牌照为"<<license_plate<<"的车暂时退出停车场"<<parking.top+1<<"号位"<<endl;StackPush(tmpparking,license_plate); //停车场中的车暂时退出进入临时停车场StackPop(parking); //出栈}cout<<"牌照为"<<license_plate<<"的车离开停车场"<<parking.top+1<<"号位"<<endl;StackPop(parking); //出栈//将临时停车场巾的车停回停车场while(StackEmpty(tmpparking)!=1){StackTop(tmpparking, license_plate);StackPush(parking, license_plate);cout<<"牌照为"<<license_plate<<"的车进入停车场"<<parking.top+1<<"号位"<<endl;license_plate[0]='\0';StackPop(tmpparking);if(parking.top+1==MAX_STOP-1)//判断车离开前停车场是否停满if(QueueLength(sidewalk))//如果停满则判断便道上是否有车{//便道中有车则从便道中停入停车场Out_LQueue(sidewalk, license_plate); //出队StackPush(parking, license_plate);//入栈cout<<"在便道中牌照为"<<license_plate<<"的车进入停车场"<<parking.top+1<<"号"<<endl;}else//车不在停车场中cout<<"您的车不在停车场中!"<<endl;}}}//初始化顺序栈void InitStack(SeqStack &parking){parking.top= -1;}int StackEmpty(SeqStack parking){if(parking.top == -1)return 1;elsereturn 0;}//判栈满int StackFull(SeqStack parking){if(parking.top == MAX_STOP-1)return 1;elsereturn 0;}//入栈void StackPush(SeqStack &parking , char *license_plate ){parking.top++;strcpy(parking.STOP[parking.top].license_plate,license_plate);parking.STOP[parking.top].state ='p';}//出栈返回栈顶指针int StackPop(SeqStack &parking){if(StackEmpty(parking))return 0;elsereturn parking.top--;}int StackTop(SeqStack parking, char *license_plate ){if(StackEmpty(parking))return 0;else{strcpy(license_plate, parking.STOP[parking.top].license_plate);return 1;}}//显示所有void Display(SeqStack parking){if(parking.top==-1)cout<<"停车场为空"<<endl;else{while(parking.top!=-1){cout<<"车牌号为:"<<parking.STOP[parking.top].license_plate;cout<<",停在"<<parking.top+1<<"号车位上"<<endl;parking.top--;}}}//初始化队列void InitList(LQueue *&sidewalk){sidewalk = (LQueue *)malloc(sizeof(LQueue));sidewalk->front=sidewalk->rear=NULL;}//入队void In_LQueue(LQueue *&sidewalk,char *license_plate){QNode *car_on_sidewalk;car_on_sidewalk= (QNode *)malloc(sizeof(QNode)); //为新节点开辟新空问strcpy(car_on_sidewalk->WAIT.license_plate,license_plate);//将数据写入节点car_on_sidewalk->W AIT.state= 's'; //写入停车信息car_on_sidewalk->next= NULL;if(Empty_LQueue(sidewalk)) //队空则创建第一个节点sidewalk->front= sidewalk->rear=car_on_sidewalk;else{//队非空插入队尾sidewalk->rear->next = car_on_sidewalk;sidewalk->rear = car_on_sidewalk;}}//判队空int Empty_LQueue(LQueue *q){if(q->front == NULL)return 1;elsereturn 0;}//判队长度返回队长int QueueLength(LQueue *q){QNode *p=q->front;int i=0;while(p!=NULL){i++;p=p->next;}return i;}//出队成功返回l队空返回0int Out_LQueue(LQueue *&sidewalk,char *license_plate){QNode *car_on_sidewalk;if(Empty_LQueue(sidewalk)) //如果队空返回0return 0;car_on_sidewalk= sidewalk->front;strcpy(license_plate,car_on_sidewalk->WAIT.license_plate);//取出队头元素if(sidewalk->front==sidewalk->rear) //队中只有一个元素sidewalk->front=sidewalk->rear=NULL; //删除元素elsesidewalk-> front= sidewalk-> front->next;//队头指针后移free(car_on_sidewalk); //释放指针return 1;}//检查离开的车是否在停车场中返同车在停车场中位置不在则返同0int LeaveCheck(SeqStack parking,char *license_plate){int flag= parking.top+1;//定义变量记录当前车在停车场中位置if(StackEmpty(parking))return 0;else{//查找离开车所在位置while(parking.top!=-1&&strcmp(parking.STOP[parking.top].license_plate, license_plate)!=0){flag--;parking.top--;}return flag;}}//检验输入的车牌是否合法void Input_Check(char *license_plate){int flag=1;int i;string tmpstr;while(flag){cin>>tmpstr;getchar();if(tmpstr.length()<MAX_PLATE){for(i=0;i<10;i++)license_plate[i]=tmpstr.c_str()[i];flag=0;}elsecout<<"输入有误,请重新输入";}}。