📄 停车场管理程序.txt
字号:
#include<iostream>
using namespace std;
typedef struct Vehicle{
int no;
int entime;
Vehicle *next;
}Vehicle;//建立车辆信息数据结构
typedef struct Spstack{
Vehicle *base;
Vehicle *top;
int vehiclesum;//停车场已停放车辆的数量
}Spstack;//建立停车场
typedef struct Queue{
Vehicle *front;
Vehicle *rear;
}Queue;//建立便道队列
//-----------------------------------------------------
int Creatstack(Spstack &S){//创建停车场
S.base=(Vehicle *)malloc(2*sizeof(Vehicle));//停车场有两个车位
if(!S.base) {
cout<<"overflow!"<<endl;
return 0;
}
S.top=S.base;
S.vehiclesum=0;
return 1;
}
int push(Spstack &S, Vehicle c){
if(2==S.vehiclesum){
cout<<"the stack is full!"<<endl;
return 0;
}
*S.top=c;
S.top--;
S.vehiclesum++;
cout<<"no:"<<c.no<<"time:"<<c.entime<<endl;
return 1;
}
int pop(Spstack &S,Vehicle &c){
if(S.top==S.base){
cout<<"the stack is empty!"<<endl;
return 0;
}
S.top++;
c=*S.top;
S.vehiclesum--;
return 1;
}
//------------------------------------------------
int creatqueue(Queue &Q){//创建便道队列的头节点
Q.front=Q.rear=(Vehicle *)malloc(sizeof(Vehicle));
if(!Q.front){
cout<<"overflow!"<<endl;
return 0;
}
Q.front=NULL;
return 1;
}
int Enqueue(Queue &Q,Vehicle c){
Vehicle *p;
p=(Vehicle *)malloc(sizeof(Vehicle));
if(!p){
cout<<"queue overflow!"<<endl;
return 0;
}
p->no=c.no;
p->entime=c.entime;
Q.rear->next=p;
Q.rear=p;
cout<<"no:"<<c.no<<"time:"<<c.entime<<endl;
return 1;
}
int Dequeue(Queue &Q,Vehicle &c){
Vehicle *p1;
if(Q.front==Q.rear){
cout<<"the queue is empty!"<<endl;
return 0;
}
p1=Q.front->next;
c.no=p1->no;
c.entime=p1->entime;
Q.front->next=p1->next;
if(Q.rear==p1)Q.rear=Q.front;
free(p1);
return 1;
}
//--------------------------------------------------
int Indexstack(Spstack &S,int no){//关于车辆在停车场中位置的函数
Vehicle *p;
p=S.base;
int i=1;
while(p->no!=no){
p--;
i++;
}
return i;//返回车辆在停车场中的位置
}
int Indexqueue(Queue &Q,int no){//关于车辆在便道中位置的函数
Vehicle *p;
p=Q.front->next;
int i;
for(i=1;p->no!=no;p=p->next,i++)
if(!p){
cout<<"cannot found out the vehicle!"<<endl;
return 0;
}
return i;//返回车辆在便道中的位置
}
int Time(int entime,int detime){//车辆在停车场中停留时间的函数
return (detime-entime);//返回车辆在停车场中的停留时间
}
//-------------------------------------------------
void Arrive(Spstack &S,Queue &Q,Vehicle c){//车辆进停车场的函数
if(S.vehiclesum>=2){//判断停车场是否满了
Enqueue(Q,c);//若停车场满,车辆进入便道排队
cout<<"the vehicle 's location of queue is no'"<<Indexqueue(Q,c.no)<<endl;//输出车辆在便道上的位置
}
else {
push(S,c);//若停车场有位置,车辆如停车场
cout<<"the vehicle 's location of park is no'"<<Indexstack(S,c.no)<<endl;//输出车辆在停车场中的位置
}
}
void Depart(Spstack &S1,Spstack &S2,Queue &Q,int no,int detime){//车辆离开停车场的函数
Vehicle *P,b;
int a;
P=++S1.top;
while(P->no!=no){//通过车牌号码来找要出停车场的车
pop(S1,b);
push(S2,b);//移动车辆到临时停放场
P=++S1.top;
}
pop(S1,b);
a=Time(b.entime,detime);
cout<<"the vehicle spent time:"<<a<<"hour and should pay"
<<10*a<<"yuan"<<endl;//输出车辆停留时间和应缴费用
while(S2.top!=S2.base){//如果临时停放场还有车辆停留
pop(S2,b);
push(S1,b);//临时停放的车辆转到停车场停放
}
if(S1.vehiclesum<2){//若停车场还有空位
Dequeue(Q,b);//让便道上排队的车进入停车场
b.entime=detime;//把离开车辆的离开时间作为从便道上进停车场的车辆的进入时间
push(S1,b);
cout<<"the vehicle 's location is no'"<<Indexstack(S1,b.no)<<endl;//输出车辆在停车场的位置
}
}
//---------------------------------------------------------------
void main()
{
char a='a';
int no,time;
Vehicle p;
Spstack S1,S2;
Creatstack(S1);
Creatstack(S2);
Queue Q;
creatqueue(Q);
while(a!='E'){
cout<<"please input the massage(eg:A 23 12):"<<endl;
cin>>a>>no>>time;
p.no=no;
if(a=='A'){
p.entime=time;
Arrive(S1,Q,p);
}
else if(a=='D')
Depart(S1,S2,Q,no,time);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -