05a685c952
- calculator_designer_ext(Day2 增强版:表达式求值 + 括号,递归下降解析器)接入 cmake - calculator_mainwindow(Day3 上午):增强版计算器装进 QMainWindow, 菜单/状态栏/历史停靠窗,纯代码 + 显式 connect,与 mainwindow_showcase 形成坑卡对照 - calculator_mainwindow_qss(Day3 下午):上午版 + QSS 双主题美化(按键分色/伪状态/深浅切换) - 验证 mainwindow_showcase(QMainWindow 六大组件综合演示,.ui + .qrc) - OUTLINE_4DAY.md / ASSIGNMENTS.md Day3 衔接增强版起点 + 参考实现指引 - 补提交 Day2 calculator/ 与 calculator_designer/ 参考实现(CMakeLists 已引用) - 全量构建 28/28,4 目标离屏自测退出码 0 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
94 lines
3.7 KiB
C++
94 lines
3.7 KiB
C++
#include <QApplication>
|
||
#include <QDebug>
|
||
#include <QTimer>
|
||
|
||
#include "calculatordesigner.h"
|
||
|
||
// ------------------------------------------------------------ 自测入口 ----
|
||
// 与 ../calculator/qt_calculator.cpp 完全相同的 5 组用例:验证「界面搭建方式
|
||
// 换了,行为不变」。
|
||
|
||
static bool expectEqual(const char* label, const QString& actual, const QString& expected) {
|
||
if (actual != expected) {
|
||
qCritical() << "自测失败:" << label << "实际=" << actual << "预期=" << expected;
|
||
return false;
|
||
}
|
||
qInfo() << "自测通过:" << label << "=>" << actual;
|
||
return true;
|
||
}
|
||
|
||
static bool runSelfTest(CalculatorDesigner* calc) {
|
||
calc->onClear();
|
||
calc->onDigit(7); calc->onOperator(QLatin1Char('+')); calc->onDigit(3); calc->onEquals();
|
||
if (!expectEqual("7 + 3 =", calc->displayText(), QStringLiteral("10"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onDigit(6); calc->onOperator(QLatin1Char('/')); calc->onDigit(0); calc->onEquals();
|
||
if (!expectEqual("6 ÷ 0 =", calc->displayText(), QStringLiteral("Error"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onDigit(4); calc->onDot(); calc->onDigit(5);
|
||
calc->onOperator(QLatin1Char('+'));
|
||
calc->onDigit(0); calc->onDot(); calc->onDigit(5);
|
||
calc->onEquals();
|
||
if (!expectEqual("4.5 + 0.5 =", calc->displayText(), QStringLiteral("5"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onDigit(1); calc->onDigit(2); calc->onBackspace();
|
||
if (!expectEqual("12 退格", calc->displayText(), QStringLiteral("1"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onDigit(2); calc->onOperator(QLatin1Char('+'));
|
||
calc->onDigit(3); calc->onOperator(QLatin1Char('+'));
|
||
calc->onDigit(4); calc->onEquals();
|
||
if (!expectEqual("2 + 3 + 4 =(链式运算)", calc->displayText(), QStringLiteral("9"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onDigit(2); calc->onOperator(QLatin1Char('+'));
|
||
calc->onDigit(3); calc->onOperator(QLatin1Char('*'));
|
||
calc->onDigit(4); calc->onEquals();
|
||
if (!expectEqual("2 + 3 × 4 =(乘法优先于加法)", calc->displayText(), QStringLiteral("14"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onLeftParen();
|
||
calc->onDigit(2); calc->onOperator(QLatin1Char('+')); calc->onDigit(3);
|
||
calc->onRightParen();
|
||
calc->onOperator(QLatin1Char('*'));
|
||
calc->onDigit(4); calc->onEquals();
|
||
if (!expectEqual("(2 + 3) × 4 =(括号提升优先级)", calc->displayText(), QStringLiteral("20"))) return false;
|
||
if (!expectEqual("(2 + 3) × 4 = 表达式区文本", calc->expressionText(), QStringLiteral("(2+3)*4 = 20"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onDigit(1); calc->onOperator(QLatin1Char('+'));
|
||
calc->onLeftParen();
|
||
calc->onDigit(2); calc->onOperator(QLatin1Char('*'));
|
||
calc->onLeftParen();
|
||
calc->onDigit(3); calc->onOperator(QLatin1Char('+')); calc->onDigit(4);
|
||
calc->onRightParen();
|
||
calc->onRightParen();
|
||
calc->onEquals();
|
||
if (!expectEqual("1 + (2 × (3 + 4)) =(嵌套括号)", calc->displayText(), QStringLiteral("15"))) return false;
|
||
|
||
calc->onClear();
|
||
calc->onLeftParen();
|
||
calc->onDigit(1); calc->onOperator(QLatin1Char('+')); calc->onDigit(2);
|
||
calc->onEquals(); // 未手动补齐右括号,应自动闭合后求值
|
||
if (!expectEqual("(1 + 2 =(自动补全括号)", calc->displayText(), QStringLiteral("3"))) return false;
|
||
|
||
return true;
|
||
}
|
||
|
||
int main(int argc, char* argv[]) {
|
||
QApplication app(argc, argv);
|
||
CalculatorDesigner calc;
|
||
calc.show();
|
||
|
||
if (qgetenv("QT_QPA_PLATFORM") == "offscreen") {
|
||
QTimer::singleShot(0, &calc, [&calc] {
|
||
const bool ok = runSelfTest(&calc);
|
||
QCoreApplication::exit(ok ? 0 : 1);
|
||
});
|
||
}
|
||
return app.exec();
|
||
}
|