//实现一个电梯类,可用于多部电梯的集中控制,测试你的类。
#include <iostream>
#include <cstdlib>
using namespace std;
class Elevator
{
private:
int currentFloor; //电梯所处位置
public:
Elevator(int cfloor=1); //构造函数
int request(int newfloor);
};
Elevator::Elevator(int cfloor)
{
currentFloor=cfloor;
}
int Elevator::request(int newfloor)
{
if(newfloor==currentFloor)
{
cout<<"You have on the floor!!!"<<endl;
return 0;
}
else if(newfloor>currentFloor) //电梯向上移动
{
cout<<endl<<"Starting at floor......"<<currentFloor<<endl;
while(newfloor>currentFloor)
{
currentFloor++;
cout<<"Going up-now at floor.."<<currentFloor<<endl;
}
cout<<"Stopping at floor......"<<currentFloor<<endl;
}
else //电梯向下移动
{
cout<<endl<<"Starting at floor........"<<currentFloor<<endl;
while(newfloor<currentFloor)
{
currentFloor--;
cout<<"Going down-now at floor.."<<currentFloor<<endl;
}
cout<<"Stopping at floor........"<<currentFloor<<endl;
}
return currentFloor;
}
int main()
{
char answer='y';
int aimfloor=0; //目标层数
Elevator a[4];
int el_num;
int num[4] = {0,0,0,0};
while(answer!='M')
{
cout << "Please enter the number of elevator you want to use(1--3)" << endl;
cin >> el_num;
cout<<"please input your floor(1--15):";
cin>>aimfloor;
if(aimfloor<1||aimfloor>15)
{
cout<<"*****Floor is wrong!!!*****"<<endl;
}
else
{
num[el_num] = a[el_num].request(aimfloor);
}
cout<<endl<<"You go on?(y or n):,if you want quit,please enter M" << endl;
cin>>answer;
cout << "elevator 1 is on " << num[1] << endl;
cout << "elevator 2 is on " << num[2] << endl;
cout << "elevator 3 is on " << num[3] << endl;
}
cout<<"Thank you for using!!!"<<endl;
system("pause");
return 0;
}
实现一个电梯类,可用于多部电梯的集中控制,测试你的类。
最新推荐文章于 2023-05-16 14:49:31 发布