# 数据结构课程设计之停车场管理系统

0x00 主要思想

0x01 程序源代码

`//停车场管理系统#include<string.h>#include<ctype.h>#include<malloc.h>#include<limits.h>#include<stdio.h>#include<stdlib.h>#include<io.h>#include<math.h>#include<process.h>#include<iostream>using namespace std;#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define STACK_INIT_SIZE 20#define STACK_INCREMENT 2typedef int SElemType;typedef int Status;//定义车的基本信息typedef struct{int arrive[2];int leave[2];char num[10];}car;//定义队列typedef struct QNode{car data;QNode *next;}QNode, *QueuePtr;typedef struct{QueuePtr front;QueuePtr rear;}LinkQueue;//定义栈typedef struct StackNode{SElemType data;struct StackNode *next;}StackNode, *StackPtr;typedef struct SqStack{int base;int top;car parkcar[STACK_INIT_SIZE];int stacksize;}SqStack;//-------------------------------------------------------//构造一个空栈一个空队列即创建一个停车场void CreatePark(SqStack &S, LinkQueue &Q){S.base = S.top = 0;Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));if (!Q.front)exit(0);Q.front->next = NULL;printf("恭喜停车场创建成功！/n");}//-------------------------------------------------------//-------------------------------------------------------//遍历栈和队列即显示停车情况//停车场的情况void ShowStack(SqStack &S){int j = S.base;while (j < S.top){cout << S.parkcar[j].num << "" << S.parkcar[j].arrive[0] << "：" << S.parkcar[j].arrive[1]<<""<<j+1<<"/n";j++;}}//临时停车点的情况void ShowQueue(LinkQueue &Q){QueuePtr p = Q.front->next;while (p){car c = p->data;printf("%s%d:%d",c.num,c.arrive[0],c.arrive[1]);if (p != Q.rear){p = p->next; }else{Q.rear = Q.front;p = NULL;}}}//总体情况的显示void ShowInformation(SqStack &S,LinkQueue &Q){if (S.base != S.top){printf("车牌号停车时间停车位置/n");ShowStack(S);}else{ShowQueue(Q);}}//-------------------------------------------------------//-------------------------------------------------------//一些基本操作：入栈，入队//入栈即汽车进入停车场void PushSqStack(SqStack &S, car ch){printf("请输入当前小时：");cin >> ch.arrive[0];printf("请输入当前分钟：");cin >> ch.arrive[1];S.parkcar[S.top++] = ch;printf("已将您的车停好，车的位置是：%d/n", S.top);}//入队列即进入临时停车点void PushQueue(LinkQueue &Q, car ch){QueuePtr p = (QueuePtr)malloc(sizeof(QNode));if (!p){exit(0);}p->data = ch;p->next = NULL;Q.rear->next = p;Q.rear = p;printf("对不起，停车场已满，请您在临时停车区等待！");ShowQueue(Q);}//-------------------------------------------------------//-------------------------------------------------------//出栈，出队列//出队列即进入停车场car PopQueue(LinkQueue &Q){car ch;QueuePtr p;p = Q.front->next;ch = p->data;Q.front->next = p->next;if (Q.rear == p){Q.rear = Q.front;}free(p);return ch;}//判断队列是否为空int QueueEmpty(LinkQueue &Q){if (Q.front == Q.rear)return 0;return 1;}//出栈即离开停车场void PopSqStack(SqStack &S, LinkQueue &Q){int i;if (S.base == S.top){printf("停车场没有车！/n");exit(0);}SqStack sq;printf("请输入您的停车位置：/n");cin>>i;sq.base = sq.top = 0;while (S.top > i)sq.parkcar[sq.top++] = S.parkcar[--S.top];int n = --S.top;printf("请输入当前小时：/n");cin >> S.parkcar[n].leave[0];printf("请输入当前分钟：/n");cin >> S.parkcar[n].leave[1];int time = (S.parkcar[n].leave[0] - S.parkcar[n].arrive[0]) * 60 + (S.parkcar[n].leave[1] - S.parkcar[n].arrive[1] );double money = time*0.05;printf("您的停车时长为：%d分钟/n", time);cout << "你本次停车的费用为：" << money << "元" << endl;printf("/n");printf("/n");printf("谢谢您使用本系统，欢迎下次光临！/n");while (sq.top > sq.base)S.parkcar[S.top++] = sq.parkcar[--sq.top];if (QueueEmpty(Q)){S.parkcar[S.top] = PopQueue(Q);printf("车牌号为%s的车从临时停车点进入停车场，停车位置为%d/n", S.parkcar[S.top].num, S.top + 1);printf("请输入当前小时：%d/n");cin >> S.parkcar[S.top].arrive[0];printf("请输入当前分钟：%d/n");cin >> S.parkcar[S.top].arrive[1];S.top++;}}//-------------------------------------------------------//-------------------------------------------------------//构建整个停车场//登记车辆信息car Register(){car c;printf("请输入您的车牌号：");cin >> c.num;c.arrive[0] = 0;c.arrive[1] = 0;c.leave[0] = 0;c.leave[1] = 0;return c;}//判断一辆车到达后是否需要构造队列void Judge(SqStack &S, LinkQueue &Q){car  ch = Register();if (S.top == STACK_INIT_SIZE)  //如果栈满，推入队列，否则推入栈{PushQueue(Q, ch);}else{PushSqStack(S, ch);}}//-------------------------------------------------------//功能目录函数void menu(){printf("-------------------------------------------/n");printf("|                                         |/n");printf("|         欢迎使用停车场管理系统          |/n");printf("|                                         |/n");printf("|                                         |/n");printf("|            请选择您需要的功能           |/n");printf("|               1.创建停车场              |/n");printf("|               2.车进停车场              |/n");printf("|               3.车出停车场              |/n");printf("|               4.查看停车情况            |/n");printf("|               0.退出系统                |/n");printf("|                                         |/n");printf("-------------------------------------------/n");}//主函数int main(){SqStack S;LinkQueue Q;int i, j = 0;bool flag = false;    do {system("CLS");menu();if (j == 0){printf("请输入你的选择：");}else{printf("请继续输入你的选择：");}cin>>i;switch (i){case 1:CreatePark(S,Q);system("pause");break;case 2:Judge(S, Q);system("pause");break;case 3:PopSqStack(S,Q);system("pause");break;case 4:ShowInformation(S,Q);system("pause");break;case 0:printf("已退出，谢谢使用！");return 0;default:printf("对不起，您的输入有误，已强制退出，请重新登录！");i = 0;system("pause");}} while (i != 0);return 0;}`