🐛 Fix notes

This commit is contained in:
2025-09-17 00:10:38 +08:00
parent feff7f0936
commit dac46e680e

View File

@@ -68,19 +68,21 @@ class SolverService {
String _formatFactorTerm(int coeff, int constTerm, [String variable = 'x']) { String _formatFactorTerm(int coeff, int constTerm, [String variable = 'x']) {
String result = ''; String result = '';
if (coeff != 0) { if (coeff != 0) {
if (coeff == 1) if (coeff == 1) {
result += variable; result += variable;
else if (coeff == -1) } else if (coeff == -1) {
result += '-$variable'; result += '-$variable';
else } else {
result += '${coeff}$variable'; result += '$coeff$variable';
}
} }
if (constTerm != 0) { if (constTerm != 0) {
if (result.isNotEmpty) { if (result.isNotEmpty) {
if (constTerm > 0) if (constTerm > 0) {
result += ' + $constTerm'; result += ' + $constTerm';
else } else {
result += ' - ${-constTerm}'; result += ' - ${-constTerm}';
}
} else { } else {
result += constTerm.toString(); result += constTerm.toString();
} }
@@ -202,12 +204,12 @@ class SolverService {
title: '开方', title: '开方',
explanation: '对方程两边同时开平方。', explanation: '对方程两边同时开平方。',
formula: formula:
'\$\$${mainVariable} ${h >= 0 ? '+' : ''}$h = \\pm \\sqrt{\\frac{${innerRat.numerator}}{${innerRat.denominator}}}\$\$', '\$\$$mainVariable ${h >= 0 ? '+' : ''}$h = \\pm \\sqrt{\\frac{${innerRat.numerator}}{${innerRat.denominator}}}\$\$',
), ),
CalculationStep( CalculationStep(
stepNumber: 4, stepNumber: 4,
title: '解出 ${mainVariable}', title: '解出 $mainVariable',
explanation: '分别取正负号,解出 ${mainVariable} 的值。', explanation: '分别取正负号,解出 $mainVariable 的值。',
formula: formula:
'\$\$${mainVariable}_1 = $x1Str, \\quad ${mainVariable}_2 = $x2Str\$\$', '\$\$${mainVariable}_1 = $x1Str, \\quad ${mainVariable}_2 = $x2Str\$\$',
), ),
@@ -225,16 +227,16 @@ class SolverService {
} }
// 2. 检查是否为一元二次方程 (包含 variable^2 或 variable²) // 2. 检查是否为一元二次方程 (包含 variable^2 或 variable²)
if (processedInput.contains('${mainVariable}^2') || if (processedInput.contains('$mainVariable^2') ||
processedInput.contains('${mainVariable}²')) { processedInput.contains('$mainVariable²')) {
return _solveQuadraticEquation( return _solveQuadraticEquation(
processedInput.replaceAll('${mainVariable}²', '${mainVariable}^2'), processedInput.replaceAll('$mainVariable²', '$mainVariable^2'),
mainVariable, mainVariable,
); );
} }
// 3. 检查是否为幂次方程 (variable^n = a 的形式) // 3. 检查是否为幂次方程 (variable^n = a 的形式)
if (processedInput.contains('${mainVariable}^') && if (processedInput.contains('$mainVariable^') &&
processedInput.contains('=')) { processedInput.contains('=')) {
return _solvePowerEquation(processedInput, mainVariable); return _solvePowerEquation(processedInput, mainVariable);
} }
@@ -350,9 +352,9 @@ class SolverService {
CalculationStep( CalculationStep(
stepNumber: 1, stepNumber: 1,
title: '移项', title: '移项',
explanation: '将所有含 ${variable} 的项移到等式左边,常数项移到右边。', explanation: '将所有含 $variable 的项移到等式左边,常数项移到右边。',
formula: formula:
'\$\$${a}${variable} ${c >= 0 ? '-' : '+'} ${c.abs()}${variable} = $d ${b >= 0 ? '-' : '+'} ${b.abs()}\$\$', '\$\$$a$variable ${c >= 0 ? '-' : '+'} ${c.abs()}$variable = $d ${b >= 0 ? '-' : '+'} ${b.abs()}\$\$',
), ),
); );
@@ -362,7 +364,7 @@ class SolverService {
title: '合并同类项', title: '合并同类项',
explanation: '合并等式两边的项。', explanation: '合并等式两边的项。',
formula: formula:
'\$\$${_formatNumber(newA.toDouble())}${variable} = ${_formatNumber(newD.toDouble())}\$\$', '\$\$${_formatNumber(newA.toDouble())}$variable = ${_formatNumber(newD.toDouble())}\$\$',
), ),
); );
@@ -377,15 +379,15 @@ class SolverService {
steps.add( steps.add(
CalculationStep( CalculationStep(
stepNumber: 3, stepNumber: 3,
title: '求解 ${variable}', title: '求解 $variable',
explanation: '两边同时除以 ${variable} 的系数 ($newA)。', explanation: '两边同时除以 $variable 的系数 ($newA)。',
formula: '\$\$${variable} = \\frac{$newD}{$newA}\$\$', formula: '\$\$$variable = \\frac{$newD}{$newA}\$\$',
), ),
); );
return CalculationResult( return CalculationResult(
steps: steps, steps: steps,
finalAnswer: '\$\$${variable} = $x\$\$', finalAnswer: '\$\$$variable = $x\$\$',
); );
} }
@@ -726,7 +728,7 @@ class SolverService {
r'^${RegExp.escape(variable)}\^(\d+)$', r'^${RegExp.escape(variable)}\^(\d+)$',
).firstMatch(leftSide); ).firstMatch(leftSide);
if (powerMatch == null) { if (powerMatch == null) {
throw Exception("不支持的幂次方程格式。当前支持 ${variable}^n = a 的形式。"); throw Exception("不支持的幂次方程格式。当前支持 $variable^n = a 的形式。");
} }
final n = int.parse(powerMatch.group(1)!); final n = int.parse(powerMatch.group(1)!);
@@ -757,8 +759,8 @@ class SolverService {
CalculationStep( CalculationStep(
stepNumber: 2, stepNumber: 2,
title: '对方程两边同时开 $n 次方', title: '对方程两边同时开 $n 次方',
explanation: '对方程两边同时开 $n 次方以解出 ${variable}', explanation: '对方程两边同时开 $n 次方以解出 $variable',
formula: '\$\$${variable} = \\sqrt[$n]{$a}\$\$', formula: '\$\$$variable = \\sqrt[$n]{$a}\$\$',
), ),
); );
@@ -786,13 +788,13 @@ class SolverService {
stepNumber: 3, stepNumber: 3,
title: '计算结果', title: '计算结果',
explanation: '计算开 $n 次方的结果。', explanation: '计算开 $n 次方的结果。',
formula: '\$\$${variable} = $resultStr\$\$', formula: '\$\$$variable = $resultStr\$\$',
), ),
); );
return CalculationResult( return CalculationResult(
steps: steps, steps: steps,
finalAnswer: '\$\$${variable} = $resultStr\$\$', finalAnswer: '\$\$$variable = $resultStr\$\$',
); );
} }