【Qt】1_简易计算器程序设计

界面展示

【Qt】1_简易计算器程序设计_第1张图片

核心概念

二阶构造

思否

中缀表达式数字和运算符的分离算法

QQueue QCalculatorDec::split(const QString& exp)
{
    QQueue ret;
    QString num = "";
    QString pre = "";

    for(int i=0; i
  • 分析,所要计算的中缀表达式中包含:

    • 数字和小数点 【 0 - 9 活 . 】
    • 符号位【 + 或 - 】
    • 运算符【 +, -, *, / 】
    • 括号 【 ( 或 ) 】
9.3 + ( 3 - -0.11 ) * 5
  • 思想,以符号作为标记对表达式中的字符逐个访问

    • 定义累计变量 num
    • 当前字符 exp[i] 为数组或小数点时:

      • 累计: num += exp[i]
    • 当前字符 exp[i] 为符号时:

      • num 为运算数,分离并保存
      • 若 exp[i] 为正负号:

        • 累计符号位 + 和 - : num += exp[i]
      • 若 exp[i] w为运算符

        • 分离并保存

中缀表达式转后缀表达式

bool QCalculatorDec::match(const QQueue& exp)
{
    bool ret = true;
    QStack stack;

    for(int i=0; ret && (i
  • 确保表达式中的括号能够左右匹配(括号成对出现;左括号先于右括号出现)
bool QCalculatorDec::transform(QQueue& exp, QQueue& output)
{
    bool ret = match(exp);
    QStack stack;

    output.clear();

    while ( ret && !exp.isEmpty() )
    {
        QString e = exp.dequeue();

        if( isNumber(e) )
        {
            output.enqueue(e);
        }
        else if( isOperator(e) )
        {
            while( (!stack.isEmpty()) && (priority(e) <= priority(stack.top())) )
            {
                output.enqueue(stack.pop());
            }

            stack.push(e);
        }
        else if( isLeft(e) )
        {
            stack.push(e);
        }
        else if( isRight(e) )
        {
            while( !stack.isEmpty() && !isLeft(stack.top()) )
            {
                output.enqueue(stack.pop());
            }

            if( !stack.isEmpty() )
            {
                stack.pop();
            }
        }
        else
        {
            ret = false;
        }
    }

    while( !stack.isEmpty() )
    {
        output.enqueue(stack.pop());
    }

    if( !ret )
    {
        output.clear();
    }

    return ret;
}
  • 分析,中缀表达式转后缀表达式的过程类似编译过程:

    • 四则运算表达式中的括号必须匹配
    • 根据运算符优先级进行转换
    • 转换后的表达时中没有括号
    • 转换后可以顺序的计算出最终结果
  • 思想

    • 当前元素 e 为数字:输出
    • 当前元素 e 为运算符:

      • 1.与栈顶元素进行优先级比较
      • 2.小于等于,讲栈顶元素输出,转 1
      • 3.大于,讲当前元素 e 入栈
    • 当前元素 e 为左括号: 入栈
    • 当前元素 e 为右括号:

      • 1.弹出栈顶元素并输出,直至栈顶元素为左括号
      • 2.将栈顶的左括号从栈中弹出
 9.3 + ( 3 - -0.11 ) * 5  ==>  9.3 3 -0.11 - 5 * +
 

后缀表达式计算结果

  • 思想

    • 遍历后缀表达时中的数字和运算符

      • 当前元素为数组,进栈
      • 当前元素为运算符:

        • 从栈中弹出右操作符
        • 从栈中弹出左操作数
        • 根据符号进行运算
        • 将运算结果压入栈中
    • 遍历结束

      • 栈中的唯一数字为运算结果
QString QCalculatorDec::calculator(QString l, QString op, QString r)
{
    QString ret = "Error";

    if( isNumber(l) && isNumber(r) )
    {
        double lp = l.toDouble();
        double rp = r.toDouble();

        if( op == "+" )
        {
            ret.sprintf("%f", lp + rp);
        }
        else if( op == "-" )
        {
            ret.sprintf("%f", lp - rp);
        }
        else if( op == "*" )
        {
            ret.sprintf("%f", lp * rp);
        }
        else if( op == "/" )
        {
            const double p = 0.00000000001;

            if( (-p < rp) && (rp < p) )
            {
                ret.sprintf("%f", lp / rp);
            }
        }
    }

    return ret;
}

QString QCalculatorDec::calculator(QQueue& exp)
{
    QString ret = "Error";
    QStack stack;

    while( !exp.isEmpty() )
    {
        QString e = exp.dequeue();

        if( isNumber(e) )
        {
            stack.push(e);
        }
        else if( isOperator(e) )
        {
            QString r = !stack.isEmpty() ? stack.pop() : "";
            QString l = !stack.isEmpty() ? stack.pop() : "";
            QString result = calculator(l, e, r);

            if( result != "Error" )
            {
                stack.push(result);
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
    }

    if( exp.isEmpty() && (stack.size() == 1) && isNumber(stack.top()) )
    {
        ret = stack.pop();
    }

    return ret;
}
  • 注意:

    • 与数学相关的算法需要考虑除 0 的情况
    • 若是浮点运算,避免代码中直接与 0 做相等比较

用户界面与交互逻辑分离

纯虚类 - 接口

定义接口类:

class ICalculator
{
public:
    virtual bool expression(const QString& exp) = 0;
    virtual QString result() = 0;
};

交互逻辑实现接口(QCalculatorDec):

class QCalculatorDec : public ICalculator
{
protected:
    // ...
    
public:
    QCalculatorDec();
    bool expression(const QString& exp);
    QString result();
    ~QCalculatorDec();
};

用户界面使用接口(QCalculatorUI):

class QCalculatorUI : public QWidget
{
    Q_OBJECT

private:
    // ...

public:
    static QCalculatorUI* NewInstance();
    void setCalculator(ICalculator* cal);
    ICalculator* getCalculator();
    ~QCalculatorUI();
};
  • 模块之间需要进行解耦(强内聚,弱耦合)

    • 每个模块只实现单一的功能
    • 模块内部的子模块只为整体的单一功能而存在
    • 模块之间通过约定好的接口进行交互
    • 模块间不能出现循环依赖

用户界面与业务逻辑的交互:
【Qt】1_简易计算器程序设计_第2张图片

计算机应用程序的整体框架:
【Qt】1_简易计算器程序设计_第3张图片

总结

  • GUI 应用程序中,需要进行用户界面与业务逻辑的分离,在C++中使用纯虚类实现接口。
  • 为保证得到合法的对象,二阶构造设计模式的使用是非常有必要的。

仓库

以上内容参考狄泰软件学院系列课程,请大家保护原创!

你可能感兴趣的:(c++)