📄 4--数据结构-停车场管理程序.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#define N 2 //--------停车场容量----------------
#define M 0.05 //--------停车场内每分钟单价--------
#define O 0.02 //--------便道每分钟停车单价--------
#define True 1
#define False 0;
typedef struct time
{
int hour;
int min;
}Time; /*时间结点*/
typedef struct
{
time hh,mm;
int num ; //--------车牌号--------------------
int arrtime; //--------到达/离开时间-------------
}ELEMTP; //--------顺序栈的数据元素类型------
typedef struct
{
ELEMTP elem[N];
int top;
}SqStack; //--------顺序栈类型----------------
typedef struct node
{
int num; //--------车牌号/便道上的车辆数量---
struct node *next;
}QNode; //--------链队列的数据元素类型------
typedef struct
{
QNode *front, *rear;
}LQueue; //--------链队列类型----------------
void InitStack_Sq (SqStack *s); //--------初始化栈------------------
int Push_Sq(SqStack *s,ELEMTP x); //--------入栈----------------------
ELEMTP Pop_Sq(SqStack *s); //--------出栈----------------------
void InitQueue_L(LQueue *q); //--------初始化队列----------------
void EnQueue_L (LQueue *q,int num1); //--------入队列--------------------
int DelQueue_L(LQueue *q); //--------出队列--------------------
void Incar(SqStack *s1,LQueue *q,ELEMTP x){ //--------车辆x进入停车场-----------
int f;
f=Push_Sq(s1,x);
if(f == 0){ //--------停车场栈s1已满入便道q-----
EnQueue_L(q,x.num);
cout<<"车牌号为"<<x.num<<"的车辆停在便道第"<<q->front->num<<"号车位上"<<endl;
}
else
cout<<"车牌号为"<<x.num<<"的车辆驶入停车场第"<<s1->top<<"号车位上"<<endl;
};//-------- Incar --------
void Outcar(SqStack *s1,SqStack *s2, LQueue *q,ELEMTP x,ELEMTP y){
double Money; //--------车辆x离开停车场
int n,f,A1,A2,B1,B2;
QNode *p;
f=0;
while ((s1->top>0) && (f!=1)){ //--------在栈s1中寻找车辆x
y=Pop_Sq(s1);
if (y.num!=x.num) n=Push_Sq(s2,y);
else f=1;
}
if (y.num==x.num){ //--------寻找到车辆x------
//-----------收费计算------------------
A1=x.hh.hour;
A2=y.hh.hour;
B1=x.mm.min;
B2=y.mm.min;
Money=((A1-A2)*60+B1-B2)*M;
cout<<"车牌号为"<<y.num<<"的车辆收费"<<Money<<"元"<<endl;
while (s2->top>0){ //--------将栈s2中的车辆倒回到栈s1中--
y=Pop_Sq(s2);
f=Push_Sq(s1,y);
}
n=DelQueue_L(q);
if (n!=NULL){ //--------便道q上的第一辆车入栈s1------
y.num=n;
y.arrtime=x.arrtime;
f=Push_Sq(s1,y);
cout<<"便道上车牌为"<<y.num<<"的车辆驶入停车场第"<<s1->top<<"号车位上\n"<<endl;
}
}
else{ //--------栈s1中未找到车辆x--------------
while (s2->top>0){ //--------将栈s2中的车辆倒回到栈s1中-----
y=Pop_Sq(s2);
f=Push_Sq(s1,y);
}
p=q->front; //--------在便道q上找到车辆x--------------
f=0;
while (f==0 && p->next!=NULL)
if (p->next->num!=x.num)
p=p->next;
else{
A1=x.hh.hour;
A2=y.hh.hour;
B1=x.mm.min;
B2=y.mm.min;
p->next=p->next->next;
q->front->num--;
if (p->next==NULL)
q->rear=q->front;
cout<<A1<<endl;
cout<<A2<<endl;
cout<<B1<<endl;
cout<<B2<<endl;
Money=((A1-A2)*60+B1-B2)*O;
cout<<"车牌号为"<<x.num<<"的车辆收费"<<Money<<"元"<<endl;
f=1;
}
if (f==0)
cout<<"输入数据错误,停车场和便道上均无"<<x.num<<"号车"<<endl;
}
}//-------- Outcar --------
void Display(SqStack *s1, LQueue *q){ //--------显示停车场的状况--------
int k; QNode *p;
cout<<"停车场状况:"<<endl;
if(s1->top!=0){
cout<<"车位 车牌"<<endl;
for(k=0;k<s1->top;k++)
cout<<" "<<k+1<<" "<<s1->elem[k].num<<endl;
}
else cout<<"停车场没有车辆"<<endl;
cout<<"便道状况:"<<endl;
if(q->front->num){
cout<<"车位 车牌"<<endl;
for(k=1,p=q->front->next;p;p=p->next)
cout<<" "<<k++<<" "<<p->num<<endl;
}
else cout<<"便道没有车辆"<<endl;
} //-------- Display --------
void main()
{
char ch1,ch2;
SqStack *s1,*s2;
LQueue *q;
ELEMTP x,y;
s1=(SqStack *) malloc (sizeof(SqStack));
s2=(SqStack *) malloc (sizeof(SqStack));
q=(LQueue *) malloc (sizeof (LQueue));
InitStack_Sq(s1);
InitStack_Sq(s2);
InitQueue_L (q);
cout<<"-----------------------停车场管理程序----------------------\n";
do{
cout<<"1--查看停车场状况 2--车辆到达 3--车辆离开 4--程序结束\n";
cout<<"请选择:";
cin>>ch1;
switch (ch1)
{
case '1':
Display(s1,q);
cout<<"---------------------------------------------------------------\n";
break;
case '2':
cout<<"输入车牌号:";
cin>>x.num;
cout<<"输入时间(hh:mm)";
cin>>x.hh.hour;
cin>>ch2;
while (ch2 != ':')
{
cout<<"输入到达时间(小时:分钟)";
cin>>x.hh.hour;
cin>>ch2;
};
while (ch2 != ':');
cin>>x.mm.min;
Incar(s1,q,x);
cout<<"---------------------------------------------------------------\n";
break;
case '3':
cout<<"输入车牌号:";
cin>>x.num;
cout<<"输入离开时间(hh:mm)";
cin>>x.hh.hour;
cin>>ch2;
while (ch2 != ':')
{
cout<<"输入离开时间(小时:分钟)";
cin>>x.hh.hour;
cin>>ch2;
};
while (ch2 != ':');
cin>>x.mm.min;
Outcar(s1,s2,q,x,y);
cout<<"---------------------------------------------------------------\n";
break;
case '4':
cout<<"程序结束"<<endl;
break;
default:
cout<<"输入数据错误,重新输入"<<"\n\n\n";
cout<<"---------------------------------------------------------------\n";
break;
cin>>ch1;
}
}
while (ch1 !='4');
}//--------main--------
void InitStack_Sq (SqStack *s)
{
s->top=0;
}
int Push_Sq(SqStack *s,ELEMTP x)
{
if (s->top==N)
return (0);
else
{
s->elem[s->top]=x;s->top++;
return(1);
}
}
ELEMTP Pop_Sq(SqStack *s)
{
ELEMTP x;
if (s->top==0)
{
x.num=NULL;
x.arrtime=NULL;
return(x);
}
else
{
s->top--;
return (s->elem[s->top]);
}
}
void InitQueue_L(LQueue *q)
{
q->front=(QNode *)malloc(sizeof(QNode));
q->rear=q->front;
q->front->next=NULL;
q->front->num=0;
}
void EnQueue_L (LQueue *q,int num1)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
p->num=num1;
p->next=NULL;
q->rear->next=p;
q->rear=p;
q->front->num++;
}
int DelQueue_L(LQueue *q)
{
QNode *p;
int n;
if (q->front==q->rear)
return (NULL);
else
{
p=q->front->next;
q->front->next=p->next;
if (p->next==NULL)
q->rear=q->front;
n=p->num;
free(p);
q->front->num--;
return(n);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -