电梯模拟 设计

今天为大家带来一个超级好玩的电梯模拟系统

·题目概要

模拟某校九层教学楼的电梯系统。该楼有一个自动电梯,能在每层停留,其中第一层是大楼的进出层,即是电梯的“本垒层”,电梯“空闲”时,将来到该层候命。

电梯一共有七个状态,即正在开门(Opening)、已开门(Opened)、正在关门(Closing)、已关门(Closed)、等待(Waiting)、移动(Moving)、减速(Decelerate)。

乘客可随机地进出于任何层。对每个人来说,他有一个能容忍的最长等待时间,一旦等候电梯时间过长,他将放弃。

模拟时钟从0开始,时间单位为0.1秒。人和电梯的各种动作均要消耗一定的时间单位(简记为t),比如:

有人进出时,电梯每隔40t测试一次,若无人进出,则关门;

关门和开门各需要20t;

每个人进出电梯均需要25t;

电梯加速需要15t;

上升时,每一层需要51t,减速需要14t;

下降时,每一层需要61t,减速需要23t;

如果电梯在某层静止时间超过300t,则驶回1层候命。

电梯调度规则:

1)就近原则:电梯的主要调度策略是首先响应沿当前行进方向上最近端的请求直到满足最远端请求。若该方向上无请求时,就改变移动方向;

2)在就近原则无法满足的情况下,首先满足更高层的请求;

3)电梯的最大承载人数为13人,电梯人数达到13人后,在有人出电梯之前,不接受进入电梯的请求;

4)乘客上下电梯时先出后进。进电梯时乘客是按发出乘坐请求的顺序依次进入,每次只能进入一人且每个人花费的时间都为25t;

5)电梯在关门期间(电梯离开之前)所在层提出请求的乘客同样允许进入。

要求:

按时序显示系统状态的变化过程,即发生的全部人和电梯的动作序列。

扩展要求:

实现电梯模拟的可视化界面。用动画显示电梯的升降,人进出电梯。设计有下列对象:电梯、人、电梯控制板及其上各种按钮、定时器等。

 

·思路

电梯运行的原则,依据单向最大请求原则。

即:在电梯类中设定两个分别记录上和下的最远请求,初始值分别设为(上)0和(下)10。最大请求即为某一方向上运动的最远楼层,例如:如果是向上,则最大请求最大为9,如       果是向下,则最大请求最远为1

注:此记录量不能合并为一个,即不能只记录当前运行方向的最远层数(最大请求)。因为会出现这种情况:当电梯正在向下,而你只记录了当前的运动方向(下)的最大请求,如       果该值为3,当你运动到楼之后,如果没有记录反向(即向上)的最大请求,那么此时就要遍历3楼以上的所有楼层的请求情况,以确定是否向上,此时就需要遍历!!(而我们       应该尽量避免遍历),如果那样,可想而知,我们要遍历多少次!

将这个大前提说明完之后,我们来规定几个原则

1)最大请求包括外面乘客搭乘需求以及电梯里面的人的出门请求

2)先完成当前方向的运送任务再反过来完成反方向的任务

3)进门的乘客一定是他的需求方向与电梯当前运动方向一致的,最大请求层的乘客情况除外(因为走到最远处了,无论乘客去哪里都应该接,无论其想上楼还是想下楼)

 

电梯能够顺利运行所依照的机制为状态转换。

即:电梯的运行逻辑全都在各个状态中,通过状态的切换而使电梯能够正常运行。

这样的话,就把电梯的逻辑分成几个部分去单独思考,出错也很容易找到位置,并进行局部的整改。

即:每一个状态下应该完成什么样的操作,例如:closing状态,依据题意,电梯正在关门的时候是可以允许有人请求进入的,所以,该状态首先要判断是否要有人进入,也就是是否       要重新开门。如果没有,到适合的时间顺利转换为closed状态,然后接下来的动作就交给closed状态模块进行处理了。

其次就是一个状态转换时间点的问题

总的思路就是记录下一个状态的触发时间,从而合理地控制状态转换时间点

即:在进入某一个状态的时候,通过给出的每个状态的延续时间,计算出下一个状态的触发时间点(开始时间),当总的时间点打到此触发时间点,那么就进行相应的状态转换。

这是一个总的思路,当然在写的时候要根据实际情况去合理地控制触发时间点,比如说closing状态,当你转换到此状态的时候,就应该算出下一个状态(closed)的触发时间点(第一次进入closing的时间点+closing的延续时间长度),但是有一个问题,就是closing状态下是可以允许乘客请求进入的,所以当这种情况发生时,目标状态(下一个状态)和时间点都要做改变,以保证程序逻辑的正确性。

 


整个程序,电梯运行过程只有当有人放弃时,才会整个数据进行遍历,重新刷新请求,除此之外,都不会进行遍历的操作。

 

·程序优化

1)存储结构:

运用类似开散列表的结构,有时候也叫纵向链表。就是一个二维的存储结构,高维是一个数组,低维是一个链表。

即 LinkNode* table[10];            

用此结构来存储所有的等待乘客,即 9层楼,每层楼都对应一个链表存储其乘客信息。

而针对电梯里面的乘客的出门信息,则是用一个一维数组来统计。我们可以想一下,只要乘客进入电梯内,那么只有他的目标层这一个数据是有用信息,其余的所有的信息均为无用信息,所以,针对电梯内部的乘客,并不需要存储他的完整信息,即只需要知道他去哪一层即可,反过来再想一下,电梯到达某一层,要把乘客送出去,即有几个人就出去几个即可。所以,我们只需要存9个数,即每个楼对应的出门人数即可。到此,针对电梯内部信息的优化,我觉得已经差不多了

尽量避免了不必要的遍历,在时间上进行了优化,尽量减少不必要的信息存储,在内存优化上达到了不错的效果

2)语法:

应用了Qt有关C++11语法中的常量表达式进行优化程序,以及内联等的一些语法技巧。

 

·程序代码

关于每个文件的作用,在注释中做了说明

 

objectors.h

#ifndef OBJECTORS_H
#define OBJECTORS_H
/************************************************************************
//! [                   objectors  头文件                              ]
//! [ 定义了一些 最基础的 枚举类型  以及  自定义类型                          ]
//! [ 枚举类型包括:  电梯的状态常量  时间控制常量                            ]
//! [ 最基础的自定义类型   电梯类   乘客类                                  ]
//! [ 基础的类中的成员函数绝大部分为内联函数 为了各方面的方便考虑 统一将声明写在类内 ]
//! [ 将其实现以内联形式写在本文件的类外                                     ]
************************************************************************/
#include<QtCore/qmargins.h>
//! [枚举类型声明]
enum Ele_Statue
{
    Waiting
,   Opening
,   Opened
,   Closing
,   Closed
,   Moving
,   Decelerate                       //! [减速]
,   Short_Wait                       //! [暂时停留300t]
};

enum Time_List
{
    T_v_Udece     =  14              //! [上升时减速]
,   T_v_up        =  15
,   T_close_open  =  20
,   T_v_Ddece     =  23              //! [下降时减速]
,   T_in_out      =  25
,   T_test        =  40              //! [测试有无人进出的时间]
,   T_ele_up      =  51
,   T_ele_down    =  61
,   T_wait        =  300
};

//! [ 电梯类 ]
class _elevator_
{
private:
    Ele_Statue m_statue;              //! [电梯的状态]
    bool m_curDire;                   //! [当前方向,上为真]
    short m_curfloor, in_number;      //! [当前层,电梯内人数]
    Q_DECL_CONSTEXPR short m_call[2]{10, 0};           //! [下、上的最大请求]
    Q_DECL_CONSTEXPR short m_out[10]{0, };             //! [出门人数记录]

public:
    Q_DECL_CONSTEXPR explicit _elevator_(const Ele_Statue& statue = Waiting)
        :m_statue(statue),
          m_curDire(true),
          m_curfloor(1),
          in_number(0)
    {  }

    Q_DECL_CONSTEXPR static constexpr short MAX_people{ 13 }, floor_num{ 9 };

    Q_DECL_CONSTEXPR inline const Ele_Statue& get_statue()const;       //! [获取状态]
    Q_DECL_CONSTEXPR inline short& get_call(const bool);               //! [获取最大请求]
    Q_DECL_CONSTEXPR inline const short get_curfloor()const;           //! [获取当前层]
    Q_DECL_CONSTEXPR inline const short getnum_in()const;              //! [获取人数]
    Q_DECL_CONSTEXPR inline short& get_out(const short);               //! [设置某层出门人数]

    const short get_Dire(const bool = true);                           //! [获取下一步的方向]
    void num_change(bool);                     //! [电梯内人数变化]
    void set_statue(const Ele_Statue&);        //! [设定状态]
    void update_call(const int);               //! [更新最大请求]
    void set_curfloor(const short);
};

//! [ 乘客类 ]
class _people_
{
private:
    int m_wait;              //! [等待时间]
    int m_appear;            //! [出现的时间点]
    short m_from, m_to;      //! [进出楼层]
public:
    Q_DECL_CONSTEXPR explicit _people_(const int t = 0, const int wait = 500)
        :m_wait(wait)
        ,m_appear(t)
        ,m_from(0)
        ,m_to(0)
    {  }

    Q_DECL_CONSTEXPR inline  const short get_wait()const;
    Q_DECL_CONSTEXPR inline const short get_from()const;
    Q_DECL_CONSTEXPR inline const short get_to()const;
    Q_DECL_CONSTEXPR inline const int   get_appear()const;
    Q_DECL_CONSTEXPR inline const bool  IsGiveUp(const int)const;

    void set_wait(const int);
    void set_from(const short);
    void set_to(const short);
    void set_time(const int);
};

//! [ 电梯类的内联函数实现 ]

Q_DECL_CONSTEXPR inline const short _elevator_::getnum_in() const
{ return in_number; }

Q_DECL_CONSTEXPR inline short &_elevator_::get_out(const short floor)
{ return m_out[floor]; }

inline void _elevator_::num_change(bool add)
{ add ? in_number++ : in_number--; }

Q_DECL_CONSTEXPR inline const Ele_Statue& _elevator_::get_statue()const
{ return m_statue; }

Q_DECL_CONSTEXPR inline short &_elevator_::get_call(const bool dir)
{ return m_call[dir]; }

Q_DECL_CONSTEXPR inline const short _elevator_::get_curfloor() const
{ return m_curfloor; }

inline void _elevator_::set_statue(const Ele_Statue& s)
{ m_statue = s; }

inline void _elevator_::update_call(const int call)
{
    if(call > m_call[true] && call > m_curfloor)m_call[true] = call;
    if(call < m_call[false] && call < m_curfloor)m_call[false] = call;
}

inline void _elevator_::set_curfloor(const short f)
{ m_curfloor = f; }

inline const short _elevator_::get_Dire(const bool change)
{
    if(m_call[1] == 0 && m_call[0] == 10)   //! [如果当前在一层,且没有上请求,则返回-1]
        return -1;

    //! [如果正在上升,最大上请求不大于当前层,反向;如果正在下降,存在向上请求,反向]
    if(change)
        if(m_curDire ? m_call[1] <= m_curfloor : (m_call[1] && m_call[0] == 10))
            m_curDire = !m_curDire;

    return m_curDire;
}

//! [ 乘客类的内联函数实现 ]
Q_DECL_CONSTEXPR inline const short _people_::get_wait() const
{ return m_wait; }

Q_DECL_CONSTEXPR inline const short _people_::get_from() const
{ return m_from; }

Q_DECL_CONSTEXPR inline const short _people_::get_to() const
{ return m_to; }

Q_DECL_CONSTEXPR inline const int _people_::get_appear() const
{ return m_appear; }

Q_DECL_CONSTEXPR inline const bool _people_::IsGiveUp(const int t) const
{ return m_appear + m_wait <= t; }

inline void _people_::set_wait(const int w)
{ m_wait = w; }

inline void _people_::set_from(const short f)
{ m_from = f; }

inline void _people_::set_to(const short t)
{ m_to = t; }

inline void _people_::set_time(const int t)
{ m_appear = t; }



#endif // OBJECTORS_H
View Code

element.h

#ifndef ELEMENT_H
#define ELEMENT_H
/****************************************************************
//! [                  element 头文件                           ]
//! [ 定义图元  电梯图元  乘客图元                                  ]
//! [ 相关的坐标运算                                              ]
****************************************************************/

#include <QtCore/qmargins.h>
enum ItemShape
{
    NoShape,
    _Elevator,
    _People,
};

class _item_
{
private:
    //! [data_Member]
    Q_DECL_CONSTEXPR static constexpr short point_number{ 10 };
    static const short coordList[3][point_number][2];
    short coordinate[point_number][2];
    ItemShape m_Shape;

    //! [Member-functions]
    inline void resetX(const short Index, const short x);
    inline void resetY(const short Index, const short y);

public:
    explicit _item_(const ItemShape& shape = NoShape);

    inline void resetShape(const ItemShape&);

    Q_DECL_CONSTEXPR inline const ItemShape& get_shape()const;
    Q_DECL_CONSTEXPR inline const short get_x(const short Index) const;
    Q_DECL_CONSTEXPR inline const short get_y(const short Index) const;

};


//! [inine-functions  defination]
inline void _item_::resetX(const short Index, const short x)
{ coordinate[Index][0] = x; }

inline void _item_::resetY(const short Index, const short y)
{ coordinate[Index][1] = y; }

Q_DECL_CONSTEXPR inline const ItemShape& _item_::get_shape()const
{ return m_Shape;}

Q_DECL_CONSTEXPR inline const short _item_::get_x(const short Index) const
{ return coordinate[Index][0]; }

Q_DECL_CONSTEXPR inline const short _item_::get_y(const short Index) const
{ return coordinate[Index][1]; }

#endif // ELEMENT_H
View Code

element.cpp

#include <QtCore>
#include "element.h"

const short _item_::coordList[3][point_number][2]
{
    { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }
,   { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 0, 1 }, { 2, 1 }, { 0, 2 }, { 2, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 } }
,   { { 0, 0 }, { 0, 2 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, { 2, 0 }, { 2, 2 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }
};

//! [*--------------------------- _item_ 类 实现 ---------------------------*]

_item_::_item_(const ItemShape &shape)
{
    resetShape(shape);
}

void _item_::resetShape(const ItemShape& shape)
{
    for(short i = 0; i < point_number; ++i)
    {
        coordinate[i][0] = coordList[shape][i][0];
        coordinate[i][1] = coordList[shape][i][1];
    }
    m_Shape = shape;
}
View Code

board.h

#ifndef BOARD_H
#define BOARD_H
/*********************************************************************
//! [                           Board 类                           ]
//! [ 面板类   用于绘图  控制图像显示 事件响应  信号-槽定义                ]
*********************************************************************/

#include <QFrame>
#include <QBasicTimer>
#include <QPointer>
#include "objectors.h"
#include "element.h"

QT_BEGIN_NAMESPACE

class QLabel;
class QFont;
class QColor;
class _item_;
enum  ItemShape;
enum  Ele_Statue;
enum  Time_List;

struct LinkNode
{
    _people_ m_data;
    LinkNode* link;
    Q_DECL_CONSTEXPR explicit LinkNode(const int t):m_data(t),link(Q_NULLPTR){}
};

QT_END_NAMESPACE


class _board_ : public QFrame
{
    Q_OBJECT

public slots:
    void SLOT_start();
    void SLOT_appear();
    void SLOT_paused();
    void SLOT_statue();

signals:
    void _time_change(const int);          //! [时间改变信号]
    void _ele_statue();                    //! [电梯状态信号]
    void _from_floor(const int);           //! [层数信息信号]
    void _to_floor(const int);             //! [层数信息信号]
    void _people_appear(const int);        //! [乘客状态信号]
    void _in_number(const int);            //! [人数计数信号]

private:
    //! [Data-Member]
    Q_DECL_CONSTEXPR static constexpr short Board_W{ 30 }, Board_H{ 36 };
    static const QColor colorList[3];
    bool ispaused, isstart;
    QBasicTimer m_timer;
    int timerElapsed, start_t;   //! [两个计时量:总时以及下一个动作开始的时间]
    int ele_X, ele_Y;
    _elevator_ m_ele;
    LinkNode* people_List[_elevator_::floor_num + 1];
    _item_* next_item;
    ItemShape m_board[Board_W * Board_H];

    //! [inline-functions]
    Q_DECL_CONSTEXPR inline ItemShape& Item_type(const int x, const int y);
    Q_DECL_CONSTEXPR inline const int grid_W()const;
    Q_DECL_CONSTEXPR inline const int grid_H()const;

    //! [Member-functions]
    void clearBoard();
    void showPeople(const short);
    void draw(QPainter&,const int,const int,const ItemShape&);   //! [描绘小格子]
    void disappear(const int floor);
    int Check_GiveUp();
    void Move();             //! [移动多少个点]
    void update_call();
    void go_in();
    void go_out();
    const bool Is_open();

protected:
    void paintEvent(QPaintEvent*)Q_DECL_OVERRIDE;
    void timerEvent(QTimerEvent*)Q_DECL_OVERRIDE;

public:
    explicit _board_(QWidget *parent = 0);
    QPointer<QLabel> statue_L;
    ~_board_();

};

Q_DECL_CONSTEXPR inline ItemShape &_board_::Item_type(const int x, const int y)
{ return m_board[y * Board_W + x]; }

Q_DECL_CONSTEXPR inline const int _board_::grid_W() const
{ return contentsRect().width() / Board_W; }

Q_DECL_CONSTEXPR inline const int _board_::grid_H() const
{ return contentsRect().height() / Board_H; }


#endif // BOARD_H
View Code

board.cpp

#include <QtWidgets>
#include <QPainter>
#include <QMessageBox>
#include "board.h"

const QColor _board_::colorList[3] =
{
    QColor(255,255,255),
    QColor(255, 0 , 0 ),
    QColor( 0 , 0 , 0 ),
};

_board_::_board_(QWidget *parent)
    :QFrame(parent),
      ele_X(0),
      ele_Y(0),
      start_t(0),
      timerElapsed(0),
      ispaused(false),
      isstart(false)
{
    for(int i = 0; i < Board_H; ++i)                //! [初始化面板]
        for(int j = 0; j < Board_W; ++j)
            Item_type(j, i) = NoShape;

    for(short i = 0; i < 10; ++i)                   //! [初始化表]
    {
        people_List[i] = new LinkNode(0);
        people_List[i]->link = Q_NULLPTR;
    }

    QFont font;
    font.setFamily("Microsoft YaHei");
    font.setPointSize(12);
    font.setItalic(true);
    statue_L = new QLabel(this);
    statue_L->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    statue_L->setFont(font);
    next_item = new _item_(_Elevator);            //! [将电梯图元初始化在面板上]
    for(short i = 0; i < 10; ++i)
        Item_type(next_item->get_x(i), next_item->get_y(i)) = next_item->get_shape();
}

_board_::~_board_()
{
    delete next_item;
                                   //释放开散列结构
    LinkNode* t;
    for(short i = 0; i < 10; ++i)
    {
        t = people_List[i];
        while(t->link != Q_NULLPTR)
        {
            LinkNode* r = t->link;
            t->link = r->link;
            delete r;
        }
        delete t;
    }
}

void _board_::SLOT_start()
{
    emit _ele_statue();
    if(isstart)return;
    m_timer.start(20,this);
    isstart = true;
    clearBoard();
}

void _board_::SLOT_paused()
{
    if(!isstart)return;

    ispaused = !ispaused;
    if(ispaused)
        m_timer.stop();
    else
        m_timer.start(20,this);
}

void _board_::SLOT_statue()
{
    switch(m_ele.get_statue())
    {
    case 0:
        statue_L->setText("Waiting");
        break;
    case 1:
        statue_L->setText("Opening");
        break;
    case 2:
        statue_L->setText("Opened");
        break;
    case 3:
        statue_L->setText("Closing");
        break;
    case 4:
        statue_L->setText("Closed");
        break;
    case 5:
        statue_L->setText("Moving");
        break;
    case 6:
        statue_L->setText("Decelerate");
        break;
    case 7:
        statue_L->setText("Short_Wait");
        break;
    default:
        break;
    }
}

void _board_::clearBoard()
{
    for(int i = 0; i < Board_H * Board_W; ++i)
        m_board[i] == _People ? m_board[i] = NoShape : 0 ;
}

void _board_::showPeople(const short from)
{
    short i = 0, j = 1;
    next_item->resetShape(_People);
    while(Item_type((j++) * 3, (from - 1) * 4));
    for(--j; i < 7; ++i)
        Item_type(j * 3 + next_item->get_x(i), (from - 1) * 4 + next_item->get_y(i)) = next_item->get_shape();
    update();
}

void _board_::SLOT_appear()
{
    if(!isstart || ispaused) return;

    short from_floor = qrand() % 9 + 1, to_floor;
    while((to_floor = qrand() % 9 + 1) == from_floor);

    LinkNode* newNode = new LinkNode(timerElapsed);
    newNode->link = Q_NULLPTR;

    emit _people_appear(newNode->m_data.get_appear());
    emit _from_floor(from_floor);
    emit _to_floor(to_floor);

    m_ele.update_call(from_floor);
    newNode->m_data.set_from(from_floor);
    newNode->m_data.set_to(to_floor);

    LinkNode* t = people_List[from_floor];
    while(t->link != Q_NULLPTR)
        t = t->link;
    t->link = newNode;
    showPeople(from_floor);
}

void _board_::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event);

    //! [ 绘制背景线条 ]
    QPainter _painter(this);
    QRect rect = contentsRect();
    int LineLeftX = rect.left() + 3 * grid_W();
    _painter.setPen(QPen(QColor(128,  36, 221).light()));
    _painter.drawLine(LineLeftX,rect.bottom(),LineLeftX, rect.top());
    _painter.drawLine(rect.left(),rect.bottom(),rect.left(), rect.top());
    _painter.setPen(QPen(QColor(128,  36, 221).dark()));
    for(int i = 0; i < _elevator_::floor_num; ++i)
        _painter.drawLine(LineLeftX + 2, rect.bottom() - 4 * grid_H() * i, rect.right(), rect.bottom() - 4 * grid_H() * i);

    //! [ 绘制图像小格子 ]
    for(short i = 0; i < Board_H; ++i)
        for(short j = 0; j < Board_W; ++j)
        {
            const ItemShape shape = Item_type(j, i);
            if(shape != NoShape)
                draw(_painter, rect.left() + grid_W() * j, rect.bottom() - grid_H() * (i + 1), shape);
        }
}

void _board_::timerEvent(QTimerEvent* event)
{
    if(event->timerId() == m_timer.timerId())
    {
        timerElapsed++;
        emit _time_change(timerElapsed);
        int f = Check_GiveUp();
        if(f)disappear(f);
        switch(m_ele.get_statue())                 //! [状态转换]
        {
        case Waiting:                              //Waiting must be in one-floor
            if(people_List[1]->link != Q_NULLPTR)  //! [如果有人想进去]
            {
                m_ele.set_statue(Opening);
                emit _ele_statue();
                break;
            }
            if(m_ele.get_Dire() != -1)
                m_ele.set_statue(Moving);

            emit _ele_statue();
            break;

        case Short_Wait:
            if(timerElapsed > start_t)
                start_t = timerElapsed + T_wait;
            if(timerElapsed == start_t || m_ele.get_Dire() != -1)
                m_ele.set_statue(Moving);

            emit _ele_statue();
            break;
        case Closed:
            if(timerElapsed > start_t)
            {
                emit _ele_statue();
                update_call();
                if(m_ele.get_Dire() == -1)            //! [什么请求都没有]
                    if(m_ele.get_curfloor() == 1)     //! [如果在1层]
                        m_ele.set_statue(Waiting);
                    else
                        m_ele.set_statue(Short_Wait);
                else
                    m_ele.set_statue(Moving);
            }
            break;
        case Moving:
            if(timerElapsed > start_t)         //! [说明下一个动作开始的时间没有刷新]
                start_t = timerElapsed + (m_ele.get_Dire(false) ? T_ele_up - T_v_Udece : T_ele_down - T_v_Ddece);

            Move();

            emit _ele_statue();
            break;
        case Opening:
            if(timerElapsed > start_t)
                start_t = timerElapsed + T_close_open;
            if(timerElapsed == start_t && start_t)
                m_ele.set_statue(Opened);

            emit _ele_statue();
            break;
        case Opened:
            if(timerElapsed > start_t)
                go_out();
            if(timerElapsed > start_t)
                go_in();
            if(timerElapsed > start_t)
                m_ele.set_statue(Closing);

            emit _ele_statue();
            break;
        case Closing:
            //! [如果正在关的时候有人进,那么就重新开门]
            if(Is_open())
            {
                m_ele.set_statue(Opening);
                start_t = 0;  //! [把下一个动作的时间定为0,方便Opening处设定下一个动作Opened的开始时间,不然的话start_t代表的是Closed的开始时间]
                emit _ele_statue();
                break;
            }

            if(timerElapsed > start_t)
            {
                start_t = timerElapsed + T_close_open;
                m_ele.set_statue(Closed);
            }
            break;
        case Decelerate:                       //! [防止正在向下减速,上方突然有请求,此时一定要坚持当前运动方向]
            Move();

            emit _ele_statue();
            break;
        default:
            break;
        }

    }
    else
        QFrame::timerEvent(event);
}

void _board_::draw(QPainter& painter, const int X, const int Y, const ItemShape& shape)
{
    QColor color = colorList[shape];
    painter.setPen(QColor(128,  36, 221));
    painter.setBrush(color);
    painter.drawRect(X, Y, grid_W(), grid_H());
}

void _board_::disappear(const int floor)
{
    int index = 3, height = 4*(floor - 1);
    while(index+=3)
    if(Item_type(index, height) == NoShape)break;      //采用尾部删除,下一个位置为空,则删除当前
    for(int i = index - 3; i < index; ++i)
        for(int j = height; j < height + 4; ++j)
            Item_type(i, j) = NoShape;

    update();
}

int _board_::Check_GiveUp()
{
    for(int i = 1; i < 10; ++i)
    {
        LinkNode* t = people_List[i];

        while(t->link != Q_NULLPTR)
        {
            if(t->link->m_data.IsGiveUp(timerElapsed))
            {
                QMessageBox::information(this, "Notice", QString("%1 t 出现的[ %2 ]楼的一个乘客已经放弃等待(==)/").arg(t->link->m_data.get_appear()).arg(t->link->m_data.get_from()));

                int r = t->link->m_data.get_from();
                LinkNode* t0 = t->link;        //! [删除该节点]
                t->link = t0->link;
                delete t0;

                update_call();

                return r;
            }
            t = t->link;
        }
    }
    return 0;
}

void _board_::update_call()
{
    if(m_ele.get_Dire(false) == -1)return;         //! [当前已无任何请求]
    int dir = m_ele.get_Dire(false), max_call = m_ele.get_call(dir);
    if(people_List[max_call]->link == Q_NULLPTR && !m_ele.get_out(max_call))
    {
        m_ele.get_call(0) = 10, m_ele.get_call(1) = 0;
        for(int i = 1; i <= 9; ++i)
            if(people_List[i]->link != Q_NULLPTR || m_ele.get_out(i))
                m_ele.update_call(i);
    }
}

void _board_::Move()
{
    if(m_ele.get_statue() == Moving && timerElapsed == start_t && start_t)
        m_ele.set_statue(Decelerate);
    emit _ele_statue();

    static int grid = 0, floor = 0;
    static bool key = true;
    grid += grid_H() * 4 / T_ele_up;      //! [走过的小点数]

    if(grid % grid_H() == 0)              //! [移动一格]
    {
        floor++;
        next_item->resetShape(_Elevator);

        if(m_ele.get_Dire(false) == 1)
            for(short x = ele_X, y = ++ele_Y, i = 9; i >= 0; --i)
            {
                Item_type(x + next_item->get_x(i), y + next_item->get_y(i)) = _Elevator;
                Item_type(x + next_item->get_x(i), y + next_item->get_y(i) - 1) = NoShape;
            }
        else
            for(short x = ele_X, y = --ele_Y, i = 0; i < 10; ++i)
            {
                Item_type(x + next_item->get_x(i), y + next_item->get_y(i)) = _Elevator;
                Item_type(x + next_item->get_x(i), y + next_item->get_y(i) + 1) = NoShape;
            }
        update();
    }

    if(floor % 4)key = true;

    if(floor && floor % 4 == 0 && key)              //! [防止进入某层一直循环,因为floor+1要经过很多小点的积累]
    {
        key = false;
        if(m_ele.get_Dire(false) == 1)                                  //! [设定当前层]
            m_ele.set_curfloor(m_ele.get_curfloor() + 1);
        else
            m_ele.set_curfloor(m_ele.get_curfloor() - 1);

        if(Is_open())
            m_ele.set_statue(Opening);                            //! [有人进出,开门]
        else if(m_ele.get_curfloor() == 1 && m_ele.get_Dire() == -1) //! [如果到1层且没有请求,停止]
            m_ele.set_statue(Waiting);
        else
            m_ele.set_statue(Moving);                   //! [如果没有人进出那么继续走]

        emit _ele_statue();
    }
}

const bool _board_::Is_open()
{
    if(m_ele.getnum_in() >= _elevator_::MAX_people)return false; //! [超载]

    LinkNode* t = people_List[m_ele.get_curfloor()];
    while(t->link != Q_NULLPTR)      //! [如果到达最大请求处,无论此人去哪里,都要开门 || 反之 必须需求方向与运动方向一致]
    {
        if(m_ele.get_curfloor() == m_ele.get_call(m_ele.get_Dire(false)) || t->link->m_data.get_to()>m_ele.get_curfloor() == m_ele.get_Dire())
            return true;
        t = t->link;
    }
    if(m_ele.get_out(m_ele.get_curfloor()))
        return true;
    return false;
}

void _board_::go_in()
{
    LinkNode* t = people_List[m_ele.get_curfloor()];
    if(t->link != Q_NULLPTR)
    {   //! [如果到达最大请求处,无论此人去哪里,都要开门 || 反之 必须需求方向与运动方向一致]
        if(m_ele.get_curfloor() == m_ele.get_call(m_ele.get_Dire(false)) || t->link->m_data.get_to()>m_ele.get_curfloor() == m_ele.get_Dire())
        {
            start_t = timerElapsed + T_in_out;
            disappear(m_ele.get_curfloor());
            m_ele.update_call(t->link->m_data.get_to());           //! [刷新请求]
            m_ele.get_out(t->link->m_data.get_to())++;
            LinkNode* t0 = t->link;                                //! [删除节点]
            t->link = t0->link;
            delete t0;
            m_ele.num_change(true);

            emit _in_number(m_ele.getnum_in());
        }
    }
}

void _board_::go_out()
{
    int num = m_ele.get_out(m_ele.get_curfloor());
    if(num)             //! [有的人都出去]
    {
        m_ele.num_change(false);
        m_ele.get_out(m_ele.get_curfloor())--;
        emit _in_number(m_ele.getnum_in());
        QMessageBox::information(this, "Notice", "go out one people");
        start_t = timerElapsed + T_in_out;
    }
}
View Code

elevator.h

#ifndef ELEVATOR_H
#define ELEVATOR_H
/********************************************************************
//! [                      ELEVATOR 类                          ]
//! [                         主窗口                             ]
//! [ 协调各个控件单元的布局 显示                                    ]
********************************************************************/
#include <QWidget>
#include <QPointer>
#include "board.h"

QT_BEGIN_NAMESPACE
class QFrame;
class QString;
class QLCDNumber;
class QLabel;
class QPushButton;
QT_END_NAMESPACE

class ELEVATOR : public QWidget
{
    Q_OBJECT

private:
    const QPointer<QLabel> newLabel(const QString&);

    _board_* m_boardPtr;
    QPointer<QLabel> m_L;
    QPointer<QLCDNumber> time_lcd, from_lcd, to_lcd, num_lcd, apear_time_lcd;
    QPointer<QPushButton> appear_btn, start_btn, quit_btn, pause_btn;


public:
    explicit ELEVATOR(QWidget *parent = Q_NULLPTR);

};

#endif // ELEVATOR_H
View Code

elevator.cpp

#include <QtWidgets>

#include "elevator.h"

ELEVATOR::ELEVATOR(QWidget *parent)
    : QWidget(parent)
{
    m_boardPtr = new _board_;

    time_lcd = new QLCDNumber(5);
    apear_time_lcd = new QLCDNumber(5);
    from_lcd = new QLCDNumber(1);
    to_lcd = new QLCDNumber(1);
    num_lcd = new QLCDNumber(5);

    time_lcd->setSegmentStyle(QLCDNumber::Filled);
    from_lcd->setSegmentStyle(QLCDNumber::Filled);
    to_lcd->setSegmentStyle(QLCDNumber::Filled);
    num_lcd->setSegmentStyle(QLCDNumber::Filled);
    apear_time_lcd->setSegmentStyle(QLCDNumber::Filled);

    appear_btn = new QPushButton(tr("appear"));
    start_btn = new QPushButton(tr("Start"));
    pause_btn = new QPushButton(tr("Pause"));
    quit_btn = new QPushButton(tr("Quit"));

    start_btn->setFocusPolicy(Qt::NoFocus);
    appear_btn->setFocusPolicy(Qt::NoFocus);
    pause_btn->setFocusPolicy(Qt::NoFocus);
    quit_btn->setFocusPolicy(Qt::NoFocus);

    connect(start_btn, SIGNAL(clicked()), m_boardPtr, SLOT(SLOT_start()));
    connect(pause_btn, SIGNAL(clicked()),m_boardPtr,SLOT(SLOT_paused()));
    connect(quit_btn,  SIGNAL(clicked()), qApp, SLOT(quit()));
    connect(appear_btn, SIGNAL(clicked()), m_boardPtr, SLOT(SLOT_appear()));
    connect(m_boardPtr, SIGNAL(_time_change(int)), time_lcd, SLOT(display(int)));
    connect(m_boardPtr, SIGNAL(_from_floor(int)), from_lcd, SLOT(display(int)));
    connect(m_boardPtr, SIGNAL(_to_floor(int)), to_lcd, SLOT(display(int)));
    connect(m_boardPtr, SIGNAL(_in_number(int)), num_lcd, SLOT(display(int)));
    connect(m_boardPtr, SIGNAL(_ele_statue()), m_boardPtr, SLOT(SLOT_statue()));
    connect(m_boardPtr, SIGNAL(_people_appear(int)), apear_time_lcd, SLOT(display(int)));

    QGridLayout* layout = new QGridLayout;
    layout->addWidget(newLabel(tr("Time")),0,0,1,2);
    layout->addWidget(time_lcd,1,0,1,2);
    layout->addWidget(newLabel(tr("Ele Statue")),2,0,1,2);
    layout->addWidget(m_boardPtr->statue_L,3,0,1,2);
    layout->addWidget(newLabel(tr("number")),4,0,1,2);
    layout->addWidget(num_lcd,5,0,1,2);
    layout->addWidget(appear_btn,6,0);
    layout->addWidget(pause_btn,6,1);
    layout->addWidget(m_boardPtr,0,2,7,14);
    layout->addWidget(newLabel(tr("From Floor")),0,16,1,2);
    layout->addWidget(from_lcd,1,16,1,2);
    layout->addWidget(newLabel(tr("To Floor")),2,16,1,2);
    layout->addWidget(to_lcd,3,16,1,2);
    layout->addWidget(newLabel(tr("Apear Time")),4,16,1,2);
    layout->addWidget(apear_time_lcd,5,16,1,2);
    layout->addWidget(start_btn,6,16);
    layout->addWidget(quit_btn,6,17);
    setLayout(layout);
    setWindowTitle("windows");
    resize(840,507);
}

const QPointer<QLabel> ELEVATOR::newLabel(const QString &label)
{
    QPointer<QLabel> lab = new QLabel(label);
    lab->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    return lab;
}
View Code

main.cpp

#include "elevator.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ELEVATOR elevator;
    elevator.show();

    return a.exec();
}
View Code

·运行效果

插入视频的话,可能会导致网页卡顿或打不开,所以还是给大家提供静态图片比较好

 

 

上面展示的测试情况不是很多,但是,自己测试的时候测了很多种情况,运行上都没问题,可以放心。

 

·项目过程中遇到的bug

# 常成员函数里面好像不能使用QMessengeBox, 会出现不存在匹配的参数
# 内联函数如果在类外.h中定义没有关键字inline会出现重定义,即使有类作用域符也不好使
# 赋值号两边的类型不是完全等价或不能隐式转化(特别是指针),会出现exit code with 255

 

那么今天的内容就介绍到这里。

 

感谢您的阅读,生活愉快~

 

原文地址:https://www.cnblogs.com/lv-anchoret/p/8623349.html