From feff7f0936f28e0029af7de2fbb6813240f5d46a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 17 Sep 2025 00:09:11 +0800 Subject: [PATCH] :bug: Bug fixes --- lib/solver.dart | 6 +++--- test/solver_test.dart | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/solver.dart b/lib/solver.dart index ec850e2..84081b9 100644 --- a/lib/solver.dart +++ b/lib/solver.dart @@ -91,7 +91,7 @@ class SolverService { /// 检测方程中的变量 Set _detectVariables(String input) { - final variablePattern = RegExp(r'\b([a-zA-Z])\b'); + final variablePattern = RegExp(r'([a-zA-Z])'); final matches = variablePattern.allMatches(input); return matches.map((match) => match.group(1)!).toSet(); } @@ -103,8 +103,8 @@ class SolverService { // 检测方程中的变量 final variables = _detectVariables(cleanInput); - if (variables.isEmpty) { - // 如果没有变量,当作简单表达式处理 + if (!cleanInput.contains('=') || variables.isEmpty) { + // 如果没有等号或没有变量,当作简单表达式处理 try { return _solveSimpleExpression(input); } catch (e) { diff --git a/test/solver_test.dart b/test/solver_test.dart index 19e0a1d..49eba8a 100644 --- a/test/solver_test.dart +++ b/test/solver_test.dart @@ -60,10 +60,9 @@ void main() { // 这个方程的根应该是 x = (4 ± √36)/2 = (4 ± 6)/2 // 所以 x1 = 5, x2 = -1 expect( - result.finalAnswer.contains('2 + 3') && - result.finalAnswer.contains('2 - 3'), + result.finalAnswer.contains('5') && result.finalAnswer.contains('-1'), true, - reason: '方程 x^2 - 4x - 5 = 0 的根应该被表示为 2 ± 3', + reason: '方程 x^2 - 4x - 5 = 0 的根为 2 ± 3', ); });