?? 停車場.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 + -