📄 停车场管理.doc
字号:
停车场管理
发表日期:2004年6月23日 已经有3302位读者读过此文
#include<iostream.h>
#include<stdlib.h>
class carstack{
private:
int car[5];
int pos;
public:
carstack(void);
void input(int n);
void output(void);
int showpos(void);
int empty(void);
};
int carstack::empty(void)
{
return car[0];
}
carstack::carstack(void)
{
pos=0;
for(int i=0;i<5;i++)
car[i]=0;
}
void carstack::input(int n)
{
car[pos]=n;
pos=pos+1;
}
void carstack::output(void)
{
pos--;
car[pos]=0;
}
int carstack::showpos(void)
{
return pos;
}
class carqueue{
private:
int *waitcar;
int begin;
int end;
public:
carqueue(void);
int reset(int b);
int showpos(void);
void putin(int b);
void putout(void);
~carqueue(void);
};
carqueue::carqueue(void)
{
waitcar=new int[5];
begin=end=0;
if(waitcar==NULL)
exit(1);
}
carqueue::~carqueue(void)
{
delete[] waitcar;
}
void carqueue::putin(int b) //b=1;
{
if(end==0)
waitcar[begin]=b;
waitcar[end]=b;
end++;
}
void carqueue::putout(void) //b是位置。
{
int t;
int *l,g;
int *str,*ptr;
g=end-1;
t=waitcar[begin];
l=new int[g];
str=l;
ptr=waitcar+1;
while(g>0)
{
*str=*ptr;
str++;
ptr++;
g--;
}
delete[] waitcar;
waitcar=l;
end--;
}
int carqueue::showpos(void)
{
return end;
}
int carqueue::reset(int b)
{
int *str,*ptr;
int *t;
int g;
t=new int[b+5];
g=b;
str=t;
ptr=waitcar;
while(g--)
{
*str=*ptr;
str++;
ptr++;
}
delete[] waitcar;
waitcar=t;
b=b+5;
return b;
}
void main()
{
carstack stopcar;
carqueue wait;
int ch,max=5;
cout<<"按1代表有车进入停车场,按2代表有车离开停车场,按3代表中止此程序:";
cin>>ch;
while(1)
{
if(ch==1)
{
if(stopcar.showpos()>4)
{
cout<<endl<<"停车场的位置已满,车辆将停在便道上。"<<endl;
if(wait.showpos()>=max)
{
max=wait.reset(max);
wait.putin(1);
}
else
wait.putin(1);
cout<<"车辆停在便道上"<<wait.showpos()<<"的位置."<<endl;
}
else
{
stopcar.input(1);
cout<<"车辆停在停车场"<<stopcar.showpos()<<"的位置上。"<<endl;
}
}
if(ch==2)
{
while(1)
{
if(stopcar.empty()==0)
{
cout<<"停车场已经没有车了!"<<endl;
break;
}
stopcar.output();
if(wait.showpos()>0)
{
wait.putout();
stopcar.input(1);
cout<<"停车场上"<<stopcar.showpos()<<"位置的车离开."<<endl;
cout<<"停车场上的"<<stopcar.showpos()<<"位置上有空位,请便道上的第一辆车进入停车场。"<<endl;
}
else
{
cout<<"停车场上"<<stopcar.showpos()+1<<"位置的车离开."<<endl;
cout<<"停车场上的"<<stopcar.showpos()+1<<"位置上有空位,"<<"停车场的便道上已经没有空车。"<<endl;
}
break;
}
}
if(ch==3)
{
cout<<"程序中止。"<<endl;
break;
}
while(1)
{
cout<<"请再选择:";
cin>>ch;
if(ch==2)
{
if(stopcar.empty()==0)
{
cout<<"对不起,停车场和便道已经没有车了,你不能选择车辆离开。"<<endl;
continue;
}
}
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -