💄 Solver better delta calc

This commit is contained in:
2025-09-13 13:43:51 +08:00
parent 0b2e7242b7
commit 65c595461d
3 changed files with 50 additions and 4 deletions

View File

@@ -287,7 +287,7 @@ class _CalculatorHomePageState extends State<CalculatorHomePage> {
message: '平方', message: '平方',
child: FilledButton.tonal( child: FilledButton.tonal(
onPressed: () => _insertSymbol('^2'), onPressed: () => _insertSymbol('^2'),
child: Text('^2', style: GoogleFonts.robotoMono()), child: Text('²', style: GoogleFonts.robotoMono()),
), ),
), ),
), ),

View File

@@ -406,7 +406,10 @@ ${b1}y &= ${c1 - a1 * x}
String _expandExpressions(String input) { String _expandExpressions(String input) {
String result = input; String result = input;
while (true) { int maxIterations = 10; // Prevent infinite loops
int iterationCount = 0;
while (iterationCount < maxIterations) {
String oldResult = result; String oldResult = result;
final powerMatch = RegExp( final powerMatch = RegExp(
@@ -431,6 +434,7 @@ ${b1}y &= ${c1 - a1 * x}
final expanded = final expanded =
'${newA}x^2${newB >= 0 ? '+' : ''}${newB}x${newC >= 0 ? '+' : ''}$newC'; '${newA}x^2${newB >= 0 ? '+' : ''}${newB}x${newC >= 0 ? '+' : ''}$newC';
result = result.replaceFirst(powerMatch.group(0)!, '($expanded)'); result = result.replaceFirst(powerMatch.group(0)!, '($expanded)');
iterationCount++;
continue; continue;
} }
@@ -455,11 +459,53 @@ ${b1}y &= ${c1 - a1 * x}
final expanded = final expanded =
'${newA}x^2${newB >= 0 ? '+' : ''}${newB}x${newC >= 0 ? '+' : ''}$newC'; '${newA}x^2${newB >= 0 ? '+' : ''}${newB}x${newC >= 0 ? '+' : ''}$newC';
result = result.replaceFirst(factorMulMatch.group(0)!, '($expanded)'); result = result.replaceFirst(factorMulMatch.group(0)!, '($expanded)');
iterationCount++;
continue;
}
// Handle expressions like x(expr) or (expr)x or coeff(expr)
final termFactorMatch = RegExp(
r'([+-]?(?:\d*\.?\d*)?x?)\(([^)]+)\)',
).firstMatch(result);
if (termFactorMatch != null) {
final termStr = termFactorMatch.group(1)!;
final factorStr = termFactorMatch.group(2)!;
// Skip if the term is just a sign or empty
if (termStr == '+' || termStr == '-' || termStr.isEmpty) {
break;
}
// Parse the term (coefficient and x power)
final termCoeffs = _parsePolynomial(termStr);
final factorCoeffs = _parsePolynomial(factorStr);
final termA = termCoeffs[1] ?? 0; // x coefficient
final termB = termCoeffs[0] ?? 0; // constant term
final factorA = factorCoeffs[1] ?? 0; // x coefficient
final factorB = factorCoeffs[0] ?? 0; // constant term
// Multiply: (termA*x + termB) * (factorA*x + factorB)
final newA = termA * factorA;
final newB = termA * factorB + termB * factorA;
final newC = termB * factorB;
final expanded =
'${newA}x^2${newB >= 0 ? '+' : ''}${newB}x${newC >= 0 ? '+' : ''}$newC';
result = result.replaceFirst(termFactorMatch.group(0)!, '($expanded)');
iterationCount++;
continue; continue;
} }
if (result == oldResult) break; if (result == oldResult) break;
iterationCount++;
} }
if (iterationCount >= maxIterations) {
throw Exception('表达式展开过于复杂,请简化输入。');
}
return result; return result;
} }

View File

@@ -21,13 +21,13 @@
<!-- iOS meta tags & icons --> <!-- iOS meta tags & icons -->
<meta name="mobile-web-app-capable" content="yes"> <meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="simple_math_calc"> <meta name="apple-mobile-web-app-title" content="SimpleMathCalc">
<link rel="apple-touch-icon" href="icons/Icon-192.png"> <link rel="apple-touch-icon" href="icons/Icon-192.png">
<!-- Favicon --> <!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"> <link rel="icon" type="image/png" href="favicon.png">
<title>simple_math_calc</title> <title>SimpleMathCalc</title>
<link rel="manifest" href="manifest.json"> <link rel="manifest" href="manifest.json">
<style id="splash-screen-style"> <style id="splash-screen-style">
html { html {