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