模拟停车场管理系统(数据结构)

太过爱你忘了你带给我的痛 2022-05-23 04:35 306阅读 0赞

其实这是期末作业啦…新人瑟瑟发抖。(手动滑稽

PS:工程下载:https://download.csdn.net/download/weixin_41918712/10506307


作业要求

模拟停车场如下图所示:

停车场是可停放n辆汽车的狭长通道,停车场内只有一条单行线通道可走车,有一个大门可供汽车进出。

70

基本要求:

1.进站:若车场内已停满n辆汽车,则后来的汽车只能在门外通道上等候,一旦有车开走,则排在通道上的第一辆车即可开入。

2.出站:当停车场内某辆汽车要离开时,必须按它在停车场停留的时间长短缴纳费用;如果通道前面有汽车在缴纳费用,则要在其后面排队等候

3.假设出、入便道上一次只能排m辆汽车,本题按n=7,m=3进行模拟,时间计算到分钟,每小时停车费3元。

假设初始时停车场内停了6辆汽车,没有排队等候进站或出站的汽车。


踩坑记录

  1. 一开始想让队列和顺序表都引用“People.h”,但一直报错,百度后才知道自己写的头文件不能被二次引用;
  2. 在重复性检测上倒是很顺利,想到了数组检测,试运行后居然一次成功了。但有个bug,进出的队列均满员过的情况下,一旦再有空位会出现车牌号可以重复的情况。后来想起来应该是数组容量不够。果不其然就是这个原因…极限情况下有13个数据,而我的数组容量才10;
  3. 一开始是用车牌号作为顺序表的顺序的,后来发现这是在给自己挖坑。
  4. …………..

效果图

开始/退出界面

70 1

停车/空场

70 2

排队停车/排队满员

70 3

排队结算/结算满员

70 4

结算完毕

70 5


源码

PS:新建工程然后添加头文件和cpp应该不用说了吧..

People.h

  1. //People.h
  2. #include<iostream>
  3. using namespace std;
  4. typedef struct
  5. {
  6. int place;
  7. int num;
  8. int time;
  9. }peo;
  10. int x1[7]={110,111,112,113,114,115};
  11. int x2[13]={1,2,3,4,5,6};
  12. void Getp(peo &e){//输入停车位
  13. cout<<"停车位置:";
  14. cin>>e.place;
  15. if((e.place>116 || e.place<110)){
  16. cout<<"该停车位不存在,请输入110~116的数字"<<endl;
  17. Getp(e);
  18. }
  19. else{
  20. for(int x=0;x<7;x++){
  21. if(x1[x]==e.place){
  22. cout<<"该停车位已被占用了哦~还请另找车位~"<<endl;
  23. Getp(e);
  24. }
  25. }
  26. }
  27. }
  28. void Getn(peo &e){//输入车牌
  29. cout<<"车牌号码:";
  30. cin>>e.num;
  31. for(int x=0;x<13;x++){
  32. if(x2[x]==e.num){
  33. cout<<"该牌车辆已在场,请重新确认您的车牌号后再输入哦~"<<endl;
  34. Getn(e);
  35. }
  36. }
  37. }
  38. void Gett(peo &e){//输入时间
  39. cout<<"停车时间:";
  40. cin>>e.time;
  41. if(e.time>2359){
  42. cout<<"时间有误,请检查后重新输入哦~"<<endl;
  43. Gett(e);
  44. }
  45. }
  46. void Getn1(peo &e,int i){//输入队列进的车牌
  47. e.num=i;
  48. }
  49. void Gettt1(peo &e){//进队时间合体
  50. Getn(e);
  51. for(int x=0;x<13;x++){
  52. if(x2[x]==0){
  53. x2[x]=e.num;
  54. break;
  55. }
  56. }
  57. }
  58. void Get1(peo &e){//时间位置号码合体
  59. Getp(e);
  60. Getn(e);
  61. Gett(e);
  62. for(int x=0;x<7;x++){//将信息记录入数组
  63. if(x1[x]==0){
  64. x1[x]=e.place;
  65. break;
  66. }
  67. }
  68. for(int x=0;x<13;x++){
  69. if(x2[x]==0){
  70. x2[x]=e.num;
  71. break;
  72. }
  73. }
  74. }
  75. void Out1(peo &e)
  76. {
  77. cout<<"停车位置:"<<e.place<<" "<<"车牌号码:"<<e.num<<" "<<"停车时间:"<<setfill('0')<<setw(4)<<e.time<<endl;
  78. }
  79. void Gettt(int i)//队列钱
  80. {
  81. int j;
  82. cin>>j;
  83. if(j<i){
  84. cout<<"时间有误,请检查后重新输入哦~"<<endl;
  85. Gettt(i);
  86. }
  87. else{
  88. cout<<"本次停车费用为:"<<0.05*(j-i)<<"分~"<<endl;
  89. }
  90. }

SqList.h

  1. //SqList.h
  2. //表--停车
  3. #include<malloc.h>
  4. #include<stdio.h>
  5. #include"People.h"
  6. #define MaxSize 7
  7. typedef peo ElemType;
  8. typedef struct
  9. {
  10. ElemType data[MaxSize];
  11. int length;
  12. }SqList; //顺序表类型
  13. void CreateList(SqList*&L,ElemType a[],int n)//建立
  14. {
  15. int i;
  16. L=(SqList*)malloc(sizeof(SqList));
  17. for(i=0;i<n;i++)
  18. L->data[i]=a[i];
  19. L->length=n;
  20. }
  21. void InitList(SqList*&L)//初始化
  22. {
  23. L=(SqList*)malloc(sizeof(SqList));
  24. L->length=0;
  25. }
  26. void DestroyList(SqList *&L)//销毁
  27. {
  28. free(L);
  29. }
  30. bool ListEmpty(SqList *L)//检查空
  31. {
  32. return(L->length==0);
  33. }
  34. int ListLength(SqList *L)//长度
  35. {
  36. return(L->length);
  37. }
  38. void DispList(SqList *L)//输出
  39. {
  40. int i;
  41. if(ListEmpty(L)){
  42. cout<<" 没生意啊好心酸TvT~"<<endl;
  43. return;
  44. }
  45. for(i=0;i<L->length;i++) Out1(L->data[i]);//stu类型
  46. printf("\n");
  47. }
  48. bool GetElem(SqList *L,int i,ElemType &e)//求值
  49. {
  50. if (i<1 || i>L->length) return false;
  51. e=L->data[i-1];
  52. return true;
  53. }
  54. int LocateElem(SqList *L,ElemType e)//查找
  55. {
  56. int i=0;
  57. while (i<L->length && L->data[i].num!=e.num) i++;
  58. if(i>=L->length) return 0;
  59. else return i+1;
  60. }
  61. bool ListInsert(SqList *&L,int i,ElemType e)//插入
  62. {
  63. int j;
  64. if(i<1||i>L->length+1) return false;
  65. i--;
  66. for(j=L->length;j>i;j--) L->data[j]=L->data[j-1];
  67. L->data[i]=e;
  68. L->length++;
  69. return true;
  70. }
  71. bool ListDelete(SqList *&L,int i,ElemType &e)//删除
  72. {
  73. int j;
  74. if(i<1||i>L->length) return false;
  75. i--;
  76. e=L->data[i];
  77. for(j=i;j<L->length-1;j++) L->data[j]=L->data[j+1];
  78. L->length--;
  79. return true;
  80. }

LinkQueue.h

  1. //LinkQueue.h
  2. //排队等候队列
  3. #include<iostream>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<malloc.h>
  7. using namespace std;
  8. typedef int ElemType1;
  9. typedef struct qnode{
  10. ElemType1 data;
  11. struct qnode *next;
  12. }DataNode;
  13. typedef struct{
  14. DataNode *front;
  15. DataNode *rear;
  16. }LinkQuNode;
  17. void InitQueue(LinkQuNode *&q){//初始化
  18. q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
  19. q->front=q->rear=NULL;
  20. }
  21. void DestroyQueue(LinkQuNode *&q){//销毁
  22. DataNode *pre=q->front,*p;
  23. if(pre!=NULL){
  24. p=pre->next;
  25. while(p!=NULL){
  26. free(pre);
  27. pre=p;
  28. p=p->next;
  29. }
  30. free(pre);
  31. }
  32. free(q);
  33. }
  34. bool QueueEmpty(LinkQuNode *q){//判断空否
  35. return(q->rear==NULL);
  36. }
  37. void EnQueue(LinkQuNode *&q,ElemType1 e){//进队
  38. DataNode *p;
  39. p=(DataNode *)malloc(sizeof(DataNode));
  40. p->data=e;
  41. p->next=NULL;
  42. if(q->rear==NULL) q->front=q->rear=p;
  43. else{
  44. q->rear->next=p;
  45. q->rear=p;
  46. }
  47. }
  48. bool DeQueue(LinkQuNode *&q,ElemType1 &e){//出队
  49. DataNode *t;
  50. if(q->rear==NULL) return false;
  51. t=q->front;
  52. if(q->front==q->rear) q->front=q->rear=NULL;
  53. else q->front=q->front->next;
  54. e=t->data;
  55. free(t);
  56. return true;
  57. }
  58. int QueueLength(LinkQuNode *q){//队列的长度
  59. int length=0;
  60. if(QueueEmpty(q)) return length;
  61. else if(!QueueEmpty(q)){
  62. DataNode *p;
  63. p=q->front;
  64. while(p){
  65. length++;
  66. p=p->next;
  67. }
  68. return length;
  69. }
  70. }
  71. void DispQueue(LinkQuNode *q)//输出等待
  72. {
  73. DataNode *t=q->front;
  74. while(t!=NULL)
  75. {
  76. printf("车牌号:%d\n",t->data);
  77. t=t->next;
  78. }
  79. }

main.cpp

  1. //main.cpp
  2. #include<iomanip>
  3. #include"LinkQueue.h"
  4. #include"SqList.h"
  5. int main(int argc, char** argv)
  6. {
  7. LinkQuNode *q1,*q2,*q3;//队列
  8. SqList *L;//链表
  9. InitQueue(q1);//进队
  10. InitQueue(q2);//出队结算
  11. InitQueue(q3);//出队结算
  12. InitList(L);//链表
  13. int i=0;
  14. ElemType e;//表自定类型对象
  15. ElemType1 e1,e2,e3;//队列自定类型对象
  16. int x11[]={110,111,112,113,114,115,116};//车位重复性检测
  17. int x22[]={1,2,3,4,5,6,7};//车牌检测
  18. peo x33[7];//一开始进场6辆车
  19. for(int j=0;j<6;j++){
  20. x33[j].place=x11[j];
  21. x33[j].num=x22[j];
  22. x33[j].time=0;
  23. }
  24. CreateList(L,x33,6);//初始6辆车
  25. cout<<"===========欢迎使用slyarh的停车场==========="<<endl<<endl;
  26. cout<<" 本停车场最多可停放7辆汽车"<<endl<<endl;
  27. while(i!=4){
  28. cout<<"================slyarh的系统================"<<endl<<endl;
  29. cout<<" 当前已停放"<<ListLength(L)<<"辆汽车~"<<endl;
  30. DispList(L);
  31. cout<<"您可以选择:"<<endl<<endl;
  32. cout<<" 1:入站"<<" "<<"2:出站"<<" "<<"3:出库汽车计费"<<" "<<"4:退出系统"<<endl<<endl;
  33. cout<<"请选择:";
  34. cin>>i;
  35. switch(i){
  36. case 1:
  37. if(ListLength(L)==7){//停车场顺序表长度满7
  38. if(QueueLength(q1)==3) cout<<" 目前停车场已满,还请另寻他处停车~"<<endl<<endl;
  39. else if(QueueLength(q1)!=3){
  40. cout<<" 目前停车场已满,还请在通道内等待~"<<endl<<endl;
  41. Gettt1(e);//车牌
  42. e1=e.num;
  43. EnQueue(q1,e1);//进队等
  44. }
  45. }
  46. else{
  47. if(ListLength(L)==0) cout<<" 您是本停车场的第一位顾客^v^"<<endl;
  48. else{
  49. cout<<"当前已被使用的车位有:";
  50. for(int x=0;x<ListLength(L);x++){//遍历显示被用的车位
  51. for(int y=0;y<7;y++){
  52. if(L->data[x].place==x1[y]) cout<<x1[y]<<" ";
  53. }
  54. }
  55. }
  56. cout<<endl<<"请输入入站汽车的相关信息:"<<endl;
  57. Get1(e);//获取车牌车位时间
  58. ListInsert(L,ListLength(L)+1,e);//插入停车场
  59. }
  60. cout<<"当前停放的车辆有:"<<endl;
  61. DispList(L);//显示停的车
  62. if(!QueueEmpty(q1)){//候车区非空
  63. cout<<"当前候车区停放的车辆有:"<<endl;
  64. DispQueue(q1);
  65. }
  66. else cout<<"当前候车区暂无车辆~"<<endl;
  67. if(!QueueEmpty(q2)){//结算区非空
  68. cout<<"当前等待结算离开的车辆有:"<<endl;
  69. DispQueue(q2);
  70. }
  71. else cout<<"当前暂无等待结算离开的车辆~"<<endl;
  72. break;
  73. case 2:
  74. if(QueueLength(q2)==3) cout<<"目前等待结算的车辆太多了~待其出库后您方可出站~!"<<endl;
  75. else{
  76. if(ListEmpty(L)) cout<<" 没生意啊好心酸TvT~"<<endl;
  77. else{
  78. cout<<endl<<"请输入要出站的车的车牌号:";
  79. ElemType i1;
  80. cin>>i1.num;//车牌
  81. if(ListDelete(L,LocateElem(L,i1),e)){//查找并删除,e是被删的车
  82. cout<<"车牌号为"<<e.num<<"的汽车已驶入结算区~"<<endl;
  83. e2=e.num;//获取车牌
  84. EnQueue(q2,e2);//进结算队等
  85. e3=e.time;
  86. EnQueue(q3,e3);//获取该车最开始进入停车场时间
  87. if(!QueueEmpty(q1)){//进队列非空
  88. DeQueue(q1,e1);//出队(车牌
  89. Getn1(e,e1);//车牌获取
  90. cout<<"车牌为"<<e.num<<"的车辆现可以入库~!还请确定入库时间:"<<endl;
  91. Gett(e);//进场时间
  92. ListInsert(L,ListLength(L)+1,e);//插入
  93. }
  94. else{
  95. for(int x=0;x<7;x++){//车位删除
  96. if(x1[x]==e.place){
  97. x1[x]=0;
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. else cout<<"不存在此车辆哦~请检查后重新输入~~"<<endl;
  104. if(ListEmpty(L)) cout<<" 没生意啊好心酸TvT~"<<endl;
  105. else{
  106. cout<<"当前停放的车辆有:"<<endl;
  107. DispList(L);
  108. }
  109. }
  110. }
  111. if(!QueueEmpty(q1)){
  112. cout<<"当前候车区停放的车辆有:"<<endl;
  113. DispQueue(q1);
  114. }
  115. else cout<<"当前候车区暂无车辆~"<<endl;
  116. if(!QueueEmpty(q2)){
  117. cout<<"当前等待结算离开的车辆有:"<<endl;
  118. DispQueue(q2);
  119. }
  120. else cout<<"当前暂无等待结算离开的车辆~"<<endl;
  121. break;
  122. case 3:
  123. if(QueueEmpty(q2)) cout<<"当前暂无等待结算离开的车辆~"<<endl;
  124. else{
  125. DeQueue(q2,e2);//出队(车牌
  126. cout<<"车牌为"<<e2<<"的车辆现进行费用结算~!还请输入当前时间:";
  127. DeQueue(q3,e3);//出队(时间
  128. Gettt(e3);//算钱
  129. cout<<"车牌号为"<<e2<<"的汽车已驶出停车场~~~!"<<endl;
  130. for(int x=0;x<13;x++){//出场的车销毁车牌
  131. if(x2[x]==e2){
  132. x2[x]=0;
  133. break;
  134. }
  135. }
  136. }
  137. if(!QueueEmpty(q1)){//非空
  138. cout<<"当前候车区停放的车辆有:"<<endl;
  139. DispQueue(q1);
  140. }
  141. else cout<<"当前候车区暂无车辆~"<<endl;
  142. if(!QueueEmpty(q2)){
  143. cout<<"当前等待结算离开的车辆有:"<<endl;
  144. DispQueue(q2);
  145. }
  146. else cout<<"当前暂无等待结算离开的车辆~"<<endl;
  147. break;
  148. case 4:
  149. cout<<endl<<" slyarh竭诚为您服务,欢迎下次再来~"<<endl;
  150. //销毁
  151. DestroyList(L);
  152. DestroyQueue(q1);
  153. DestroyQueue(q2);
  154. DestroyQueue(q3);
  155. break;
  156. default:
  157. cout<<"您的输入有误,请检查后重新输入~"<<endl;
  158. }
  159. }
  160. return 0;
  161. }

✎﹏﹏₯㎕《晴天》故事的小黄花…﹍﹍﹍﹍﹍﹍

发表评论

表情:
评论列表 (有 0 条评论,306人围观)

还没有评论,来说两句吧...

相关阅读

    相关 停车场智能化管理系统

    摘 要 随着科学技术的飞速发展,各行各业都在努力与现代先进技术接轨,通过科技手段提高自身的优势;对于停车场智能化管理系统当然也不能排除在外,随着网络技术的不断成熟,带动了

    相关 停车场管理系统 Java语言

    停车场管理系统 数据结构 Java语言 题目 设停车场是一个可停放n辆汽车的狭长死胡同,南边封口,汽车只能从北边进出。汽车在停车场内按车辆到达时间的先后顺序,最先到

    相关 C++实现数据结构-停车场管理

    设停车场是一个可停放”辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端

    相关 停车场管理系统

    【摘要】:伴随着科技的飞速发展,交通工具的越来越普及。汽车作为人类社会中最主要的交通工具之一,起着重大作用。随着人们生活水平的提高,汽车的数量也与日俱增,于是停车正在成为世界性