电梯控制.cpp
来自「自己编写的一个简单的程序」· C++ 代码 · 共 62 行
CPP
62 行
#include"iostream.h"
const int maxfloor=15;
class elevator
{
private:
int currentfloor;
public:
elevator(int=1);
void request(int);
};
elevator::elevator(int cfloor)
{
currentfloor=cfloor;
}
void elevator::request(int newfloor)
{
if(newfloor<1||newfloor>maxfloor||newfloor==currentfloor);
else if(newfloor>currentfloor)
{
cout<<"\nstarting at floor"<<currentfloor<<endl;
while(newfloor>currentfloor)
{
currentfloor++;
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--;
cout<<"Going down-now at floor"<<currentfloor<<endl;
}
cout<<"stopping at floor"<<currentfloor<<endl;
}
return;
}
int main()
{
int aimfloor;
char ans='y';
elevator a;
while(ans!='n')
{
cout<<"你想去几楼?请输入楼层(1-15)";
cin>>aimfloor;
a.request(aimfloor);
cout<<"你继续吗?(y or n)"<<endl;
cin>>ans;
}
cout<<"谢谢使用!";
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?