停车场管理系统(栈和队列)
#include<iostream.h>const int N=3;
const int M=3;
typedef struct
{
int num;
int arrtime;
}elemtype;
struct seqstack
{
elemtype stack[N+1];
int top;
}s1,s2;
struct link
{
elemtype data;
link *next;
};
struct linkqueue
{
link *front,*rear;
}q;
seqstack inistack(seqstack S)
{
S.top=0;
return S;
}
seqstack push(seqstack S,elemtype x)
{
S.top++;
S.stack[S.top]=x;
return S;
}
seqstack pop(seqstack S)
{
S.top--;
return S;
}
elemtype gettop(seqstack S)
{
return S.stack[S.top];
}
int empty(seqstack S)
{
if(S.top==0)
return 1;
else return 0;
}
linkqueue iniqueue(linkqueue s)
{
link *p;
p=new link;
p->next=NULL;
s.front=s.rear=p;
return s;
}
linkqueue enqueue(linkqueue s,elemtype x)
{
link *p;
p=new link;
p->data=x;
p->next=s.rear->next;
s.rear->next=p;
s.rear=p;
return s;
}
linkqueue dlqueue(linkqueue s)
{
link *p=s.front;
s.front=p->next;
delete p;
return s;
}
elemtype gethead(linkqueue s)
{
return s.front->next->data;
}
int emptyqueue(linkqueue s)
{
if(s.front==s.rear)
return 1;
else
return 0;
}
void arrive(elemtype x)
{
if(s1.top==N)
q=enqueue(q,x);
else
s1=push(s1,x);
}
void delive(elemtype x)
{
int f=1;
elemtype y;
link *r;
while((!empty(s1)&&(f==1)))
if(s1.stack[s1.top].num!=x.num)
{
y=gettop(s1);
s1=pop(s1);
s2=push(s2,y);
}
else
{f=0;y=gettop(s1);
s1=pop(s1);
cout<<"停车场中有编号为"<<x.num<<"的车"<<endl;
cout<<"该车将离开,应收停车费:"<<(x.arrtime-y.arrtime)*M<<"元"<<endl;
while(!empty(s2))
{y=gettop(s2);
s2=pop(s2);
s1=push(s1,y);
}
if(!emptyqueue(q))
{
y=gethead(q);
q=dlqueue(q);
s1=push(s1,y);
}
}
if(empty(s1))
{
while(!empty(s1))
{
y=gettop(s2);
s2=pop(s2);
s1=push(s1,y);
}
if(!emptyqueue(q))
{
link *p=q.front;
while((p!=NULL)&&(p->data.num!=x.num))
{
r=p;
p=p->next;
}
if(p!=NULL)
{
cout<<"便道上有编号为"<<x.num<<"的车辆"<<endl;
cout<<"该车将离开,应收停车费0.00 元:"<<endl;
r->next=p->next;
delete p;
}
else
cout<<"便道上没有编号为"<<x.num<<"的车辆,输入的车辆不存在!!"<<endl;
}
}
}
void pr1()
{
int t=s1.top;
while(t!=0)
{cout<<endl;
cout<<"停车场中的车辆编号和到达时间"<<endl;
cout<<s1.stack[t].num<<" "<<s1.stack[t].arrtime<<endl;
t--;
}
cout<<endl;
}
void pr2()
{
link *p=q.front->next;
while(p!=NULL)
{
cout<<"车进入便道!";
cout<<"便道中的车编号和到达时间"<<endl;
cout<<p->data.num<<" "<<p->data.arrtime<<endl;
p=p->next;
}
}
void main()
{
int n;
elemtype x;
s1=inistack(s1);
s2=inistack(s2);
q=iniqueue(q);
while(1)
{ cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<" **欢迎进入车辆管理系统** "<<endl;
cout<<"请输入处理车辆的代号(1. 到达 2. 离开 )"<<endl;
cin>>n;
cout<<"请输入车辆编号及到达或离开的时间"<<endl;
cin>>x.num>>x.arrtime;
if(n==1)
{
arrive(x);
pr1();
pr2();
}
else
if(n==2)
{
delive(x);
pr1();
pr2();
}
else break;
}
cout<<endl;
}
[[italic] 本帖最后由 appleflower 于 2007-12-9 11:12 编辑 [/italic]]