停车场管理系统(C语言)【附源码及报告】
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include <malloc.h>
#define stackSize 3
#define scanf scanf_s
#pragma warning(disable:4996);
typedef int ElementType;
//定义车辆信息的结构体
typedef struct {
int BNo;//车号
int arrivetime;//到达时间
int pushtime;//停车时间
int departuretime;//离开时间
int location;//位置
}BusInf;
//栈定义
typedef struct {
BusInf elem[stackSize];
int top;//指示
}SeqStack;
//初始化栈
void InitStack(SeqStack *s)
{
s->top = -1;
}
//判断栈空--为1表示空--位0表示非空
int IsEmpty(SeqStack *s)
{
if (s->top == -1)
{
return 1;
}
else {
return 0;
}
}
//判断栈满--0未满--1栈满
int IsFull(SeqStack *s)
{
if (s->top == stackSize - 1)
{
return 1;
}
else
return 0;
}
//进站--1表示入栈--0表示栈满
int push(SeqStack *s, BusInf bus)
{
if (IsFull(s) == 1)
{
return 0;
}
else
{
s->top++;
s->elem[s->top] = bus;//车辆入栈,通俗讲就是栈顶的车辆结构体=定义的车结构体
return 1;
}
}
//出栈--1表示出栈,0表示栈空--用指针带出出栈的值
int pop(SeqStack *s, BusInf *bus)
{
if (IsEmpty(s) == 1)//栈满情况
{
return 0;
}
else
{
*bus = s->elem[s->top];//栈顶的车辆赋给bus
s->top--;
return 1;
}
}
//队列
typedef struct Node {
BusInf Bus;
struct Node * next;
}LinkQueueNode;
typedef struct {
LinkQueueNode *front;//头指针
LinkQueueNode *rear;//尾指针
}LinkQueue;
//初始化队列--将Q初始化为一个空的链队列
int InitQueue(LinkQueue *Q)
{
Q->front = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if (Q->front != NULL)
{
Q->rear = Q->front;
Q->front->next = NULL;
Q->rear->next = NULL;
return 1;
}
else return 0;
}
//入队操作
int EnterQueue(LinkQueue *Q, BusInf bus)
{
//新建一个节点,插入队尾
LinkQueueNode *NewNode = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if (NewNode != NULL)
{
NewNode->Bus = bus;//将传参过来的车辆数据赋给结点车辆数据
NewNode->next = NULL;
Q->rear->next = NewNode;//链接起来,让尾结点下一个指向定义的新结点,再把新结点作为尾结点
Q->rear = NewNode;//将最后一个插入的节点设为尾结点
return 1;
}
else {
return 0;
}
}
//出队操作
int DeleteQueue(LinkQueue *Q, BusInf *bus) {
LinkQueueNode *p;//定义一个新的结点p
if (Q->front == Q->rear) {
return 0;
}
else {
p = Q->front->next;
Q->front->next = p->next;//将队头更新,跳过了p,后面free(p)
*bus = p->Bus;
//判断这个时候是否队列为空,是的话首尾指针相等
if (Q->rear == p) {
Q->front = Q->rear;
}
//释放节点P
free(p);
return 1;
}
}
//查找栈中有无车牌信息--temp代表出栈的位置....-2代表无车牌
int searchBusNo(SeqStack *s, BusInf bus)
{
SeqStack *p = NULL;
p = s;
int temp = p->top;//top为栈顶指示
while (temp >= 0)
{
if (p->elem[temp].BNo == bus.BNo)
return temp;
temp--;
}
return -2;//如果top=-1,那么temp=-2.
}
void showBus(SeqStack *s) {
//输出栈内车辆
printf("*******************************************************************************");
printf("已停靠车辆:\n");
printf(" 车牌号 到达时间 停车时间 停车位置\n");
int location = 0;
for (int temp = 0; temp <= s->top; temp++)
{
location++;
s->elem[temp].location = location;
printf(" %4d%15d%16d%16d\n", s->elem[temp].BNo, s->elem[temp].arrivetime, s->elem[temp].pushtime, s->elem[temp].location);
}
printf("*******************************************************************************");
getchar();
getchar();
}
//显示队列里的车
void showQBus(LinkQueue *l) {
LinkQueueNode *p;
p = (LinkQueueNode*)malloc(sizeof(LinkQueueNode));
p = l->front->next;//将p结点放到front后面去
printf("等待车辆:\n");
printf(" 车牌号 到达时间\n");
while (p != NULL)
{
printf(" %4d%15d\n", p->Bus.BNo, p->Bus.arrivetime);
printf("*******************************************************************************");
getchar();
if (p->next == NULL)
return;
p = p->next;
}
}
//输入车辆信息并且入栈或入队
void inputCarData(SeqStack *s, LinkQueue *l) {
BusInf bus;
printf("请输入车牌号:");
scanf("%d", &bus.BNo);
//判断车牌号是否重复
if (searchBusNo(s, bus) != -2) {
//因为如果栈内已经存在车牌号,说明已经入栈,那么不会返回-2,则提示重复
printf("车牌号重复!\n");
printf("请重新输入车牌信息:");
scanf("%d", &bus.BNo);
}
printf("请输入到达时间:");
scanf("%d", &bus.arrivetime);
if (bus.arrivetime < 0 || bus.arrivetime>24)
{
printf("时间输入不合法(0-24)重新输入:\n");
scanf("%d", &bus.arrivetime);
}
bus.pushtime = bus.arrivetime;
//车辆进栈--先判断栈是否满
if (IsFull(s) == 1) {
showBus(s);//先输出栈内车辆数据
//栈满的情况,下面来的进队列中
EnterQueue(l, bus);
showQBus(l);//栈满后进队列输出队列内的车辆数据
}
//栈没满的情况
else {
push(s, bus);//压栈
//显示进栈车辆信息
showBus(s);
}
}
//离开停车位
void leaveStack(SeqStack *s, SeqStack *se, LinkQueue *Q)
{
if (IsEmpty(s) == 1)//如果栈空
{
printf("此时没有车辆停靠!");
return;
}
int pay = 2;//单价
int temp = 0;//停车位
int leavetime = 0;//离开时间
BusInf bus;
printf("请输入离开的车号:");
scanf("%d", &temp);
bus.BNo = temp;
int m = searchBusNo(s, bus);//查找车在栈内的位置
while (m == -2)//等于-2说明栈内没有这辆车
{
printf("输入错误重新输入");
scanf("%d", &temp);
bus.BNo = temp;
m = searchBusNo(s, bus);
}
printf("请输入离开时间:");
scanf("%d", &leavetime);
while (leavetime <= s->elem[m].arrivetime)
{
printf("离开时间必须大于停车时间!重新输入:");
scanf("%d", &leavetime);
}
//显示出栈的车辆
printf("*******************************************************************************");
printf("离去车辆信息\n");
printf(" 车牌号 停车费 离开时间\n");
s->elem[m].departuretime = leavetime;//栈内m位置的离开时间
printf("%4d%14d%15d", s->elem[m].BNo, (leavetime - s->elem[m].arrivetime)*pay, s->elem[m].departuretime);
printf("\n*******************************************************************************");
getchar();
getchar();
//这时m接收的就是出栈的位置,共2种情况出栈车辆在栈顶和不在栈顶,在栈顶直接出去,不在的话需要前面的车让位进入辅助栈se,等出栈完毕再归位
//离开的车在栈顶时
if (m == s->top)
{
BusInf *b;
b = (BusInf*)malloc(sizeof(BusInf));
pop(s, b);//出栈
}
//不在栈顶
else
{
for (int i = s->top; i >= m; i--) {
BusInf *b = (BusInf*)malloc(sizeof(BusInf));
//先入栈再出战
if (i == m)
{
pop(s, b);
}
else {
push(se, s->elem[i]);//先将排在前面的车入se栈
pop(s, b);//等前面的车出完了,再出栈
}
}
for (int j = se->top; j >= 0; j--)//从辅助栈内依次入停车位栈
{
BusInf *b = (BusInf*)malloc(sizeof(BusInf));
pop(se, b);//出se栈
push(s, *b);//进s栈
}
}
//判断队列是否有等待车辆--有的话对头入栈
if (Q->rear != Q->front) {
//队列不为空,队中存在车辆
BusInf *b = (BusInf*)malloc(sizeof(BusInf));
*b = Q->front->next->Bus;
DeleteQueue(Q, b);//出队
b->pushtime = leavetime;//得到离开时间
push(s, *b);//将出队的车进栈
}
}
//菜单选择
void havecar(SeqStack *s)
{
int x;
x = stackSize - s->top - 1;
if (x == 0)
printf("车位已满\n");
printf("剩余车位:%d", x);
getchar();
printf("*******************************************************************************\n");
}
int menu()
{
char n;
//printf("*******************************************************************************");
printf(" %27c1---停车%15c\n", '*', ' ');
printf(" %27c2---离开%15c\n", '*', ' ');
printf(" %27c3---停车情况%11c\n", '*', ' ');
printf(" %27c4---车位余量%11c\n", '*', ' ');
printf(" %27c5---结束程序%11c\n", '*', ' ');
printf("*******************************************************************************");
printf("\n请选择: ");
do {
n = getch();
} while (n<'1' || n>'5');
printf("\n");
return(n - 48);
}
int main()
{
//车辆栈
SeqStack *s = (SeqStack*)malloc(sizeof(SeqStack));
InitStack(s);
//车辆在路边停靠的队列
LinkQueue *l = (LinkQueue *)malloc(sizeof(LinkQueue));
InitQueue(l);
//辅助栈存储让位的信息
SeqStack *se = (SeqStack*)malloc(sizeof(SeqStack));
InitStack(se);
while (1)
{
switch (menu()) {
case 1:inputCarData(s, l);
break;
case 2:leaveStack(s, se, l);
break;
case 3:showBus(s);
break;
case 4:havecar(s);
break;
case 5:return 0;
}
}
return 0;
}
源码及报告链接:源码及报告