📄 停车场.cpp
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#define Max 4//车库容量
#define price 0.05
typedef struct time{
int hour;
int min;
}Time;//时间接点
typedef struct node{
char num[10];
Time reach;
Time leave;
}CarNode;//车辆信息结点
typedef struct NODE{
CarNode *stack[Max+1];
int top;
}seqstackcar;//模拟车站
typedef struct car{
CarNode *data;
struct car*next;
}QueueNode;
typedef struct Node{
QueueNode *head;
QueueNode *rear;
}LinkQueueCar;//模拟便道
void InitStack(seqstackcar*);//初始化栈
int InitQueue(LinkQueueCar*);//初始化便道
int Arrival(seqstackcar*,LinkQueueCar*);//车辆到达处理
void Leave(seqstackcar*,seqstackcar*,LinkQueueCar*);//车辆离开处理
void List(seqstackcar,LinkQueueCar);//显示车辆的存储信息
void main()
{
seqstackcar Enter,Temp;
LinkQueueCar wait;
int ch;
InitStack(&Enter);//初始化车站
InitStack(&Temp);//初始化让路的临时栈
InitQueue(&wait);//初始化通道
cout<<"***************************欢迎进入停车场模拟系统*******************************"<<endl;
while(1)
{
cout<<"1车辆到达 2车辆离开 3列表显示 4退出系统"<<endl;
while(1)
{
cin>>ch;
if(ch>=1&&ch<=4) break;
else
{
cout<<"请选择1|2|3|4"<<endl;
break;
}
}
switch(ch)
{
case 1:Arrival(&Enter,&wait);break;//车辆到达
case 2:Leave(&Enter,&Temp,&wait);break;//车辆离开
case 3:List(Enter,wait);break;//显示列表信息
case 4:cout<<"**********************************模拟结束!*************************************"<<endl;
exit(0);
default :break;
}
}
}
void InitStack(seqstackcar*s)//初始化栈
{
int i;
s->top=0;
for(i=0;i<=Max;i++)
s->stack[s->top]=NULL;
}
int InitQueue(LinkQueueCar*Q)//初始化便道
{
Q->head=(QueueNode*)malloc(sizeof(QueueNode));
if(Q->head!=NULL)
{
Q->head->next=NULL;
Q->rear=Q->head;
return 1;
}
else return -1;
}
void PRINT(CarNode*p,int room)//打印出站的信息
{
int A1,A2,B1,B2;
cout<<"请输入离开时间"<<endl;
cin>>p->leave.hour>>p->leave.min;
cout<<"离开车的车牌号为:"<<p->num<<endl;
cout<<"离开时间为"<<p->leave.hour<<":"<<p->leave.min;
A1=p->reach.hour;
A2=p->reach.min;
B1=p->leave.hour;
B2=p->leave.min;
cout<<"应该交的费用:"<<((B1-A1)*60+(B2-A2))*price;
free(p);
}
int Arrival(seqstackcar *Enter,LinkQueueCar *w)//车辆到达
{
CarNode *p;
QueueNode *t;
p=(CarNode*)malloc(sizeof(CarNode));//建立结点
cout<<"请输入车牌号"<<endl;
cin>>p->num;
if(Enter->top<Max)
{
Enter->top++;
cout<<"车辆在第"<<Enter->top<<"的位置"<<endl;
cout<<"请输入到达时间"<<endl;
cin>>p->reach.hour>>p->reach.min;
Enter->stack[Enter->top]=p;
return(1);
}
else
{
cout<<"该车须在便道等待:"<<endl;
t=(QueueNode*)malloc(sizeof(QueueNode));
t->data=p;
t->next=NULL;
w->rear->next=t;
w->rear=t;
return(1);
}
}
void Leave(seqstackcar*Enter,seqstackcar*Temp,LinkQueueCar*w)//车辆离开
{
int room;
CarNode *p,*t;
QueueNode*q;
//判断车场内是否有车
if(Enter->top>0)
{
cout<<"要离开的车在车场的位置"<<endl;
cin>>room;
}
else cout<<"车场里没车"<<endl;
while(Enter->top > room)//车辆离开,在这辆车后面的所有车要先进临时栈
{
Temp->top++;
Temp->stack[Temp->top]=Enter->stack[Enter->top];//先把最后近来的进栈
Enter->stack[Enter->top]=NULL;
Enter->top--;
}
p=Enter->stack[Enter->top];//此时top为要出站的车辆
Enter->stack[Enter->top]=NULL;
Enter->top--;
while(Temp->top>=1)//把进入临时栈的车辆出栈
{
Enter->top++;
Enter->stack[Enter->top]=Temp->stack[Temp->top];
Temp->stack[Temp->top]=NULL;
Temp->top--;
}
PRINT(p,room);
//判断通道上是否有车及车站是否已满
if((w->head!=w->rear)&&Enter->top<Max)//便道的车进入车场
{
q=w->head->next;
t=q->data;
Enter->top++;
cout<<"便道的"<<t->num<<"号车进入车场"<<Enter->top<<"位"<<endl;
cout<<" 请输入现在的时间"<<endl;
cin>>t->reach.hour>>t->reach.min;
w->head->next=q->next;
if(q==w->rear) w->rear=w->head;
Enter->stack[Enter->top]=t;
free(q);
}
else cout<<"便道里无车"<<endl;
}
void List1( seqstackcar * s)//列表显示车场信息
{
int i;
if(s->top>0)//判断车场内是否有车
{
cout<<"车场位置 到达时间 车牌号"<<endl;
for (i=1;i<=s->top;i++)
{
cout<<" "<<i<<" "<<s->stack[i]->reach.hour<<":"<<s->stack[i]->reach.min<<" ";
cout<<s->stack[i]->num<<endl;
}
}
else cout<<"车场里没有车"<<endl;
}
void List2(LinkQueueCar*w)//列表显示便道信息
{
QueueNode *p;
p=w->head->next;
if(w->head!=w->rear)//判断有无车
{
cout<<"等待车辆的号码为:";
while (p!=NULL)
{
cout<<p->data->num<<endl;
p=p->next;
}
}
else cout<<"便道里无车"<<endl;
}
void List(seqstackcar s,LinkQueueCar w)
{
int f,l;
f=1;
while (f)
{
cout<<"选择1,2,3"<<endl;
cout<<"1车场 2便道 3返回"<<endl;
while(1)
{
cin>>l;
if(l>=1||l<=3)break;
else cout<<"请选择1|2|3";
}
switch(l)
{
case 1:List1(&s);break;//显示车场信息
case 2:List2(&w);break;//显示便道信息
case 3:f=0;break;
default:break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -