模拟停车场管理系统(数据结构)
其实这是期末作业啦…新人瑟瑟发抖。(手动滑稽
PS:工程下载:https://download.csdn.net/download/weixin_41918712/10506307
作业要求
模拟停车场如下图所示:
停车场是可停放n辆汽车的狭长通道,停车场内只有一条单行线通道可走车,有一个大门可供汽车进出。
基本要求:
1.进站:若车场内已停满n辆汽车,则后来的汽车只能在门外通道上等候,一旦有车开走,则排在通道上的第一辆车即可开入。
2.出站:当停车场内某辆汽车要离开时,必须按它在停车场停留的时间长短缴纳费用;如果通道前面有汽车在缴纳费用,则要在其后面排队等候。
3.假设出、入便道上一次只能排m辆汽车,本题按n=7,m=3进行模拟,时间计算到分钟,每小时停车费3元。
假设初始时停车场内停了6辆汽车,没有排队等候进站或出站的汽车。
踩坑记录
- 一开始想让队列和顺序表都引用“People.h”,但一直报错,百度后才知道自己写的头文件不能被二次引用;
- 在重复性检测上倒是很顺利,想到了数组检测,试运行后居然一次成功了。但有个bug,进出的队列均满员过的情况下,一旦再有空位会出现车牌号可以重复的情况。后来想起来应该是数组容量不够。果不其然就是这个原因…极限情况下有13个数据,而我的数组容量才10;
- 一开始是用车牌号作为顺序表的顺序的,后来发现这是在给自己挖坑。
- …………..
效果图
开始/退出界面
停车/空场
排队停车/排队满员
排队结算/结算满员
结算完毕
源码
PS:新建工程然后添加头文件和cpp应该不用说了吧..
People.h
//People.h
#include<iostream>
using namespace std;
typedef struct
{
int place;
int num;
int time;
}peo;
int x1[7]={110,111,112,113,114,115};
int x2[13]={1,2,3,4,5,6};
void Getp(peo &e){//输入停车位
cout<<"停车位置:";
cin>>e.place;
if((e.place>116 || e.place<110)){
cout<<"该停车位不存在,请输入110~116的数字"<<endl;
Getp(e);
}
else{
for(int x=0;x<7;x++){
if(x1[x]==e.place){
cout<<"该停车位已被占用了哦~还请另找车位~"<<endl;
Getp(e);
}
}
}
}
void Getn(peo &e){//输入车牌
cout<<"车牌号码:";
cin>>e.num;
for(int x=0;x<13;x++){
if(x2[x]==e.num){
cout<<"该牌车辆已在场,请重新确认您的车牌号后再输入哦~"<<endl;
Getn(e);
}
}
}
void Gett(peo &e){//输入时间
cout<<"停车时间:";
cin>>e.time;
if(e.time>2359){
cout<<"时间有误,请检查后重新输入哦~"<<endl;
Gett(e);
}
}
void Getn1(peo &e,int i){//输入队列进的车牌
e.num=i;
}
void Gettt1(peo &e){//进队时间合体
Getn(e);
for(int x=0;x<13;x++){
if(x2[x]==0){
x2[x]=e.num;
break;
}
}
}
void Get1(peo &e){//时间位置号码合体
Getp(e);
Getn(e);
Gett(e);
for(int x=0;x<7;x++){//将信息记录入数组
if(x1[x]==0){
x1[x]=e.place;
break;
}
}
for(int x=0;x<13;x++){
if(x2[x]==0){
x2[x]=e.num;
break;
}
}
}
void Out1(peo &e)
{
cout<<"停车位置:"<<e.place<<" "<<"车牌号码:"<<e.num<<" "<<"停车时间:"<<setfill('0')<<setw(4)<<e.time<<endl;
}
void Gettt(int i)//队列钱
{
int j;
cin>>j;
if(j<i){
cout<<"时间有误,请检查后重新输入哦~"<<endl;
Gettt(i);
}
else{
cout<<"本次停车费用为:"<<0.05*(j-i)<<"分~"<<endl;
}
}
SqList.h
//SqList.h
//表--停车
#include<malloc.h>
#include<stdio.h>
#include"People.h"
#define MaxSize 7
typedef peo ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}SqList; //顺序表类型
void CreateList(SqList*&L,ElemType a[],int n)//建立
{
int i;
L=(SqList*)malloc(sizeof(SqList));
for(i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void InitList(SqList*&L)//初始化
{
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
}
void DestroyList(SqList *&L)//销毁
{
free(L);
}
bool ListEmpty(SqList *L)//检查空
{
return(L->length==0);
}
int ListLength(SqList *L)//长度
{
return(L->length);
}
void DispList(SqList *L)//输出
{
int i;
if(ListEmpty(L)){
cout<<" 没生意啊好心酸TvT~"<<endl;
return;
}
for(i=0;i<L->length;i++) Out1(L->data[i]);//stu类型
printf("\n");
}
bool GetElem(SqList *L,int i,ElemType &e)//求值
{
if (i<1 || i>L->length) return false;
e=L->data[i-1];
return true;
}
int LocateElem(SqList *L,ElemType e)//查找
{
int i=0;
while (i<L->length && L->data[i].num!=e.num) i++;
if(i>=L->length) return 0;
else return i+1;
}
bool ListInsert(SqList *&L,int i,ElemType e)//插入
{
int j;
if(i<1||i>L->length+1) return false;
i--;
for(j=L->length;j>i;j--) L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
return true;
}
bool ListDelete(SqList *&L,int i,ElemType &e)//删除
{
int j;
if(i<1||i>L->length) return false;
i--;
e=L->data[i];
for(j=i;j<L->length-1;j++) L->data[j]=L->data[j+1];
L->length--;
return true;
}
LinkQueue.h
//LinkQueue.h
//排队等候队列
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
typedef int ElemType1;
typedef struct qnode{
ElemType1 data;
struct qnode *next;
}DataNode;
typedef struct{
DataNode *front;
DataNode *rear;
}LinkQuNode;
void InitQueue(LinkQuNode *&q){//初始化
q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
q->front=q->rear=NULL;
}
void DestroyQueue(LinkQuNode *&q){//销毁
DataNode *pre=q->front,*p;
if(pre!=NULL){
p=pre->next;
while(p!=NULL){
free(pre);
pre=p;
p=p->next;
}
free(pre);
}
free(q);
}
bool QueueEmpty(LinkQuNode *q){//判断空否
return(q->rear==NULL);
}
void EnQueue(LinkQuNode *&q,ElemType1 e){//进队
DataNode *p;
p=(DataNode *)malloc(sizeof(DataNode));
p->data=e;
p->next=NULL;
if(q->rear==NULL) q->front=q->rear=p;
else{
q->rear->next=p;
q->rear=p;
}
}
bool DeQueue(LinkQuNode *&q,ElemType1 &e){//出队
DataNode *t;
if(q->rear==NULL) return false;
t=q->front;
if(q->front==q->rear) q->front=q->rear=NULL;
else q->front=q->front->next;
e=t->data;
free(t);
return true;
}
int QueueLength(LinkQuNode *q){//队列的长度
int length=0;
if(QueueEmpty(q)) return length;
else if(!QueueEmpty(q)){
DataNode *p;
p=q->front;
while(p){
length++;
p=p->next;
}
return length;
}
}
void DispQueue(LinkQuNode *q)//输出等待
{
DataNode *t=q->front;
while(t!=NULL)
{
printf("车牌号:%d\n",t->data);
t=t->next;
}
}
main.cpp
//main.cpp
#include<iomanip>
#include"LinkQueue.h"
#include"SqList.h"
int main(int argc, char** argv)
{
LinkQuNode *q1,*q2,*q3;//队列
SqList *L;//链表
InitQueue(q1);//进队
InitQueue(q2);//出队结算
InitQueue(q3);//出队结算
InitList(L);//链表
int i=0;
ElemType e;//表自定类型对象
ElemType1 e1,e2,e3;//队列自定类型对象
int x11[]={110,111,112,113,114,115,116};//车位重复性检测
int x22[]={1,2,3,4,5,6,7};//车牌检测
peo x33[7];//一开始进场6辆车
for(int j=0;j<6;j++){
x33[j].place=x11[j];
x33[j].num=x22[j];
x33[j].time=0;
}
CreateList(L,x33,6);//初始6辆车
cout<<"===========欢迎使用slyarh的停车场==========="<<endl<<endl;
cout<<" 本停车场最多可停放7辆汽车"<<endl<<endl;
while(i!=4){
cout<<"================slyarh的系统================"<<endl<<endl;
cout<<" 当前已停放"<<ListLength(L)<<"辆汽车~"<<endl;
DispList(L);
cout<<"您可以选择:"<<endl<<endl;
cout<<" 1:入站"<<" "<<"2:出站"<<" "<<"3:出库汽车计费"<<" "<<"4:退出系统"<<endl<<endl;
cout<<"请选择:";
cin>>i;
switch(i){
case 1:
if(ListLength(L)==7){//停车场顺序表长度满7
if(QueueLength(q1)==3) cout<<" 目前停车场已满,还请另寻他处停车~"<<endl<<endl;
else if(QueueLength(q1)!=3){
cout<<" 目前停车场已满,还请在通道内等待~"<<endl<<endl;
Gettt1(e);//车牌
e1=e.num;
EnQueue(q1,e1);//进队等
}
}
else{
if(ListLength(L)==0) cout<<" 您是本停车场的第一位顾客^v^"<<endl;
else{
cout<<"当前已被使用的车位有:";
for(int x=0;x<ListLength(L);x++){//遍历显示被用的车位
for(int y=0;y<7;y++){
if(L->data[x].place==x1[y]) cout<<x1[y]<<" ";
}
}
}
cout<<endl<<"请输入入站汽车的相关信息:"<<endl;
Get1(e);//获取车牌车位时间
ListInsert(L,ListLength(L)+1,e);//插入停车场
}
cout<<"当前停放的车辆有:"<<endl;
DispList(L);//显示停的车
if(!QueueEmpty(q1)){//候车区非空
cout<<"当前候车区停放的车辆有:"<<endl;
DispQueue(q1);
}
else cout<<"当前候车区暂无车辆~"<<endl;
if(!QueueEmpty(q2)){//结算区非空
cout<<"当前等待结算离开的车辆有:"<<endl;
DispQueue(q2);
}
else cout<<"当前暂无等待结算离开的车辆~"<<endl;
break;
case 2:
if(QueueLength(q2)==3) cout<<"目前等待结算的车辆太多了~待其出库后您方可出站~!"<<endl;
else{
if(ListEmpty(L)) cout<<" 没生意啊好心酸TvT~"<<endl;
else{
cout<<endl<<"请输入要出站的车的车牌号:";
ElemType i1;
cin>>i1.num;//车牌
if(ListDelete(L,LocateElem(L,i1),e)){//查找并删除,e是被删的车
cout<<"车牌号为"<<e.num<<"的汽车已驶入结算区~"<<endl;
e2=e.num;//获取车牌
EnQueue(q2,e2);//进结算队等
e3=e.time;
EnQueue(q3,e3);//获取该车最开始进入停车场时间
if(!QueueEmpty(q1)){//进队列非空
DeQueue(q1,e1);//出队(车牌
Getn1(e,e1);//车牌获取
cout<<"车牌为"<<e.num<<"的车辆现可以入库~!还请确定入库时间:"<<endl;
Gett(e);//进场时间
ListInsert(L,ListLength(L)+1,e);//插入
}
else{
for(int x=0;x<7;x++){//车位删除
if(x1[x]==e.place){
x1[x]=0;
break;
}
}
}
}
else cout<<"不存在此车辆哦~请检查后重新输入~~"<<endl;
if(ListEmpty(L)) cout<<" 没生意啊好心酸TvT~"<<endl;
else{
cout<<"当前停放的车辆有:"<<endl;
DispList(L);
}
}
}
if(!QueueEmpty(q1)){
cout<<"当前候车区停放的车辆有:"<<endl;
DispQueue(q1);
}
else cout<<"当前候车区暂无车辆~"<<endl;
if(!QueueEmpty(q2)){
cout<<"当前等待结算离开的车辆有:"<<endl;
DispQueue(q2);
}
else cout<<"当前暂无等待结算离开的车辆~"<<endl;
break;
case 3:
if(QueueEmpty(q2)) cout<<"当前暂无等待结算离开的车辆~"<<endl;
else{
DeQueue(q2,e2);//出队(车牌
cout<<"车牌为"<<e2<<"的车辆现进行费用结算~!还请输入当前时间:";
DeQueue(q3,e3);//出队(时间
Gettt(e3);//算钱
cout<<"车牌号为"<<e2<<"的汽车已驶出停车场~~~!"<<endl;
for(int x=0;x<13;x++){//出场的车销毁车牌
if(x2[x]==e2){
x2[x]=0;
break;
}
}
}
if(!QueueEmpty(q1)){//非空
cout<<"当前候车区停放的车辆有:"<<endl;
DispQueue(q1);
}
else cout<<"当前候车区暂无车辆~"<<endl;
if(!QueueEmpty(q2)){
cout<<"当前等待结算离开的车辆有:"<<endl;
DispQueue(q2);
}
else cout<<"当前暂无等待结算离开的车辆~"<<endl;
break;
case 4:
cout<<endl<<" slyarh竭诚为您服务,欢迎下次再来~"<<endl;
//销毁
DestroyList(L);
DestroyQueue(q1);
DestroyQueue(q2);
DestroyQueue(q3);
break;
default:
cout<<"您的输入有误,请检查后重新输入~"<<endl;
}
}
return 0;
}
✎﹏﹏₯㎕《晴天》故事的小黄花…﹍﹍﹍﹍﹍﹍
还没有评论,来说两句吧...