#pragma once #include #include #include QT_BEGIN_NAMESPACE namespace Ui { class CalculatorForm; } QT_END_NAMESPACE class CalculatorDesigner : public QWidget { Q_OBJECT public: explicit CalculatorDesigner(QWidget* parent = nullptr); ~CalculatorDesigner() override; // 供自测直接调用,效果等价于点击对应按钮;逻辑与 ../calculator/ 版一字不差。 void onDigit(int digit); void onDot(); void onOperator(QChar op); void onLeftParen(); void onRightParen(); void onEquals(); void onClear(); void onBackspace(); QString displayText() const; QString expressionText() const; private: // 单个运算数(数字串)允许的最大长度,要求不得小于 10 位。 static constexpr int kMaxOperandLength = 15; static_assert(kMaxOperandLength >= 10, "单个运算数的最大长度不得小于 10 位"); // 表达式总长度上限,要求不得低于 100 个字符。 static constexpr int kMaxExpressionLength = 200; static_assert(kMaxExpressionLength >= 100, "表达式总长度上限不得低于 100 个字符"); QString currentOperandToken() const; int currentOperandDigitCount() const; QString lastNumberToken() const; void refreshDisplays(); void showError(); Ui::CalculatorForm* ui; QString m_expression; // 当前正在编辑的表达式,例如 "12+3*(4-2" QString m_lastResultText; // 上一次成功计算得到的结果文本 int m_openParenCount = 0; // 尚未闭合的左括号数量 bool m_resultShown = false; // 显示区当前展示的是计算结果而非正在输入的数字 bool m_error = false; };