c++ 实现停车场管理系统
#include<string.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 5
#define STACK_INCREMENT 2
typedef 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)
{
printf("%s %d:%d %d",S.parkcar[j].num,S.parkcar[j].arrive[0],S.parkcar[j].arrive[1],j+1);
printf("\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("请输入当前小时:");
scanf("%d",&ch.arrive[0]);
printf("请输入当前分钟:");
scanf("%d",&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 1;
else return 0;
}
void PopSqStack(SqStack &S, LinkQueue &Q)
{
int i;
if (S.base == S.top)
{
printf("停车场没有车!\n");
exit(0);
}
SqStack sq;
printf("请输入您的停车位置:\n");
scanf("%d",&i);
sq.base = sq.top = 0;
while (S.top > i)
sq.parkcar[sq.top++] = S.parkcar[--S.top];
int n = --S.top;
printf("请输入当前小时:\n");
scanf("%d",&S.parkcar[n].leave[0]);
printf("请输入当前分钟:\n");
scanf("%d",&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);
printf("你本次停车的费用为%lf:",money);
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");
scanf("%d",&S.parkcar[S.top].arrive[0]);
printf("请输入当前分钟:%d\n");
scanf("%d",&S.parkcar[S.top].arrive[1]);
S.top++;
}
}
car Register()
{
car c;
printf("请输入您的车牌号:");
scanf("%s",&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;
menu();
while(1)
{
printf("请继续输入你的选择:");
scanf("%d",&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("对不起,您的输入有误,已强制退出,请重新登录!");
system("pause");
}
}
return 0;
}