实现一个电梯类,可用于多部电梯的集中控制,测试你的类。

//实现一个电梯类,可用于多部电梯的集中控制,测试你的类。
#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;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Αиcíеиτеǎг

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值