📄 停车场.cpp
字号:
/*停车场管理模拟程序*/
/*计055班陈雨欣*/
#include <iostream.h>
int N;
const int M=5; //M为单元时间的收费值
struct cinfo //定义栈中元素的类型
{
int cnum;
int atime;
};
struct stack //定义栈
{
cinfo cstack[3000]; //这里随便定义一个数字表示数组的长度,因为后
int top; //面会根据用户输入的N值作为停车场能够停车的
int size; //数量.
};
struct node //定义队列结点的类型
{
node* next;
int nnum;
};
struct queue //定义队列
{
node *front,*rear;
};
void initstack(stack* s) //初始化栈
{
s->top=-1;
}
int PushStack(stack* s,cinfo x) //元素进栈
{
if(s->top==N-1)
{
cout<<"Stack is full!"<<endl;
return 0;
}
else
{
s->cstack[++s->top]=x;
return 1;
}
}
cinfo PopStack(stack* s)//元素出栈
{
cinfo y;
if(s->top<0)
{
y.cnum=NULL;
y.atime=NULL;
return y;
}
else
{
s->top--;
return s->cstack[s->top+1];
}
}
void initqueue(queue* q) //初始化队列
{
q->front=new node;
q->rear=q->front;
q->front->next=NULL;
q->front->nnum=0;
}
void EnQueue(queue* q,int num1)//元素进队列
{
node* p;
p=new node;
p->nnum=num1;
p->next=NULL;
q->rear->next=p;
q->rear=p;
q->front->nnum++;
}
int DeQueue(queue* q)//元素出队列
{
node* p;
int n;
if(q->front==q->rear)
return 0;
else
{
p=q->front->next;
q->front->next=p->next;
if(p->next==NULL)
q->rear=q->front;
n=p->nnum;
delete p;
q->front->nnum--;
return n;
}
}
void carrival(stack* s,queue* q,cinfo x) //处理车辆到达的情况
{
int f;
f=PushStack(s,x);
if(f==0)
{
EnQueue(q,x.cnum);
cout<<"The Number"<<x.cnum<<" "<<"car park into the pos"<<q->front->nnum<<" "<<"of the road."<<endl;
}
else
{
cout<<"The Number"<<x.cnum<<" "<<"car park into the pos"<<s->top+1<<" "<<"of the room."<<endl;
}
}
void carleave(stack* s1,stack* s2,queue* q,cinfo x) //处理车辆离开的情况
{
node* p;
cinfo y;
int a,n=0;
while((s1->top>-1)&&(n==0))
{
y=PopStack(s1);
if(y.cnum!=x.cnum)
{
a=PushStack(s2,y);
}
else
n=1;
}
if(y.cnum==x.cnum)
{
cout<<"The number"<<x.cnum<<" "<<"car want to leave,the charge is"<<" "<<(x.atime-y.atime)*M<<" "<<"yuan!"<<endl;
while(s2->top>-1)
{
y=PopStack(s2);
n=PushStack(s1,y);
}
a=DeQueue(q);
if(a!=0)
{
y.cnum=a;
y.atime=x.atime;
n=PushStack(s1,y);
cout<<"The Number"<<y.cnum<<" "<<"car park into the pos"<<s1->top+1<<" "<<"of the room."<<endl;
}
}
else
{
while(s2->top>-1)
{
y=PopStack(s2);
n=PushStack(s1,y);
}
p=q->front;
n=0;
while(p->next!=NULL&&n==0)
{
if(p->next->nnum!=x.cnum)
p=p->next;
else
{
p->next=p->next->next;
q->front->nnum--;
if(p->next=NULL)
q->rear=q->front;
cout<<"The number"<<x.cnum<<" "<<"want to leave from the road."<<endl;
n=1;
}
}
if(n==0)
{
cout<<"You have entered a wrong number!"<<endl;
}
}
}
void main()//主程序
{
cout<<" ********停车场管理模拟程序******** " <<endl<<endl;
cout<<" *计055班陈雨欣*" <<endl<<endl;
cout<<"请输入一个表示停车场能容纳的最大车辆数的数(N):"<<endl;
cin>>N;//这里N将作为栈能存放元素的数量
while(N<=0)
{
cout<<"Oh!You have enter a wrong number,'N' should above 0.Please enter another number again:"<<endl;
cin>>N;
}
char ch1;
stack* s1,*s2;
queue* q;
cinfo x;
int flag;
s1=new stack;
s2=new stack;
q=new queue;
initstack(s1);
initstack(s2);
initqueue(q);
flag=1;
for(;;)
{
cout<<"Please enter the infomation:'A'/'D'; car number; car arrival time."<<endl;
cin>>ch1>>x.cnum>>x.atime;
switch(ch1)
{
case'A':
case'a':carrival(s1,q,x);
break;
case'D':
case'd':carleave(s1,s2,q,x);
break;
case'E':
case'e':flag=0;cout<<"Ok,the system will be shut down!"<<endl;
break;
default:cout<<"You entered wrong!"<<endl;
}
if(flag==0)
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -