当前位置: 代码迷 >> QT开发 >> if、else if是否包括switch的所有应用场景了
  详细解决方案

if、else if是否包括switch的所有应用场景了

热度:91   发布时间:2016-04-25 04:35:50.0
if、else if是不是包括switch的所有应用场景了?
switch的子项必须为整数,if没有这个限制。

如此说来,if、else if岂非包括switch的所有应用场景了?可以完全取代switch。

之所以很多人用switch,是不是因为它的写法,看起来代码很工整好看?
C/C++ code
void calculatePaintRect(){    if ((!m_bMousePressed) || (NO_ACTION==enAction))    {        return;    }    //上一次鼠标位置与这次鼠标位置的差额    QPoint ptEvent = event->pos();    int xDiff = ptEvent.x() - m_ptLast.x();    int yDiff = ptEvent.y() - m_ptLast.y();    QRect rectAdjust = m_rectOfRedLine;    //或者移动,或者缩放    if (DRAG_TOP_LEFT == enAction)    {        //缩放(右下不变)        xDiff = ptEvent.x() - m_rectOfRedLine.x();        yDiff = ptEvent.y() - m_rectOfRedLine.y();        rectAdjust.adjust(xDiff, yDiff, 0, 0);        if (SCALE_FIX == m_eumScaleType)        {            int newWidth = rectAdjust.width();            int newHeight = newWidth * CAPTURE_HEIGHT / CAPTURE_WIDTH;            rectAdjust.setLeft(rectAdjust.right() - newWidth);            rectAdjust.setTop(rectAdjust.bottom() - newHeight);        }    }    else if (DRAG_TOP_RIGHT == enAction)    {        //缩放(左下不变)        xDiff = ptEvent.x() - m_rectOfRedLine.right();        yDiff = ptEvent.y() - m_rectOfRedLine.y();        rectAdjust.adjust(0, yDiff, xDiff, 0);        if (SCALE_FIX == m_eumScaleType)        {            int newWidth = rectAdjust.width();            int newHeight = newWidth * CAPTURE_HEIGHT / CAPTURE_WIDTH;            rectAdjust.setRight(rectAdjust.left() + newWidth);            rectAdjust.setTop(rectAdjust.bottom() - newHeight);        }    }    else if (DRAG_BOTTOM_LEFT == enAction)    {        //缩放(右上不变)        xDiff = ptEvent.x() - m_rectOfRedLine.x();        yDiff = ptEvent.y() - m_rectOfRedLine.bottom();        rectAdjust.adjust(xDiff, 0, 0, yDiff);        if (SCALE_FIX == m_eumScaleType)        {            int newWidth = rectAdjust.width();            int newHeight = newWidth * CAPTURE_HEIGHT / CAPTURE_WIDTH;            rectAdjust.setLeft(rectAdjust.right() - newWidth);            rectAdjust.setBottom(rectAdjust.top() + newHeight);        }    }    else if (DRAG_BOTTOM_RIGHT == enAction)    {        //缩放(左上不变)        xDiff = ptEvent.x() - m_rectOfRedLine.right();        yDiff = ptEvent.y() - m_rectOfRedLine.bottom();        rectAdjust.adjust(0, 0, xDiff, yDiff);        if (SCALE_FIX == m_eumScaleType)        {            int newWidth = rectAdjust.width();            int newHeight = newWidth * CAPTURE_HEIGHT / CAPTURE_WIDTH;            rectAdjust.setRight(rectAdjust.left() + newWidth);            rectAdjust.setBottom(rectAdjust.top() + newHeight);        }    }


------解决方案--------------------
switch有跳转表 直接可以跳转 效率高 if else得一个一个判断
  相关解决方案