没事写了个关于电梯位置的程序
#include<iostream.h>
const int MAXFLOOD=15;
file://类声明
class Elevator
{
private:
int currentFloor;
public:
Elevator(int=1);//构造函数
void request(int);
};
file://类的实现部分
Elevator::Elevator(int cfloor)
{
currentFloor=cfloor;
}
void Elevator::request(int newfloor)
{
if(newfloor<1||newfloor>MAXFLOOD||newfloor==currentFloor);
file://什么也不做
else if(newfloor>currentFloor)//向上移动电梯
{
cout<<"\nStarting at floor"<<currentFloor<<endl;
while(newfloor>currentFloor)
{
currentFloor++;//当前层号加1
cout<<"Going Up-Now at floor"<<currentFloor<<endl;
}
cout<<"Stopping at floor"<<currentFloor<<endl;
}
else//向下移动电梯
{
cout<<"\nStarting at floor"<<currentFloor<<endl;
while(newfloor<currentFloor)
{
currentFloor--;//当前层数减1
cout<<"Going Down-Now at floor"<<currentFloor<<endl;
}
cout<<"Stopping at floor"<<currentFloor<<endl;
}
return;
}
int main()
{
Elevator a;//声明Elevator类的一个对象
a.request(6);
a.request(3);
return 0;
}