park.cpp
来自「(五)测试数据:n=0 n=-1 n=2 a 1 2 a 2 3」· C++ 代码 · 共 229 行
CPP
229 行
#include "iostream.h"
int N;
const int M=5;
struct cinfo
{
int cnum;
int atime;
};
struct stack
{
cinfo cstack[3000];
int top;
int size;
};
struct node
{
node* next;
int nnum;
};
struct queue
{
node *front,*rear;
};
void initstack(stack* s)
{
s->top=-1;
}
int instack(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 outstack(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 inqueue(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 outqueue(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=instack(s,x);
if(f==0)
{
inqueue(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=outstack(s1);
if(y.cnum!=x.cnum)
{
a=instack(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=outstack(s2);
n=instack(s1,y);
}
a=outqueue(q);
if(a!=0)
{
y.cnum=a;
y.atime=x.atime;
n=instack(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=outstack(s2);
n=instack(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<<"Please enter a number as the maxsize of the roomStack:"<<endl;
cin>>N;
while(N<=0)
{
cout<<" a wrong number "<<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;
break;
default:cout<<"error"<<endl;
}
if(flag==0)
break;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?