🐛 Fix some tri angles values
This commit is contained in:
125
lib/solver.dart
125
lib/solver.dart
@@ -63,6 +63,15 @@ class SolverService {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 检查是否为特殊三角函数值,可以返回精确结果
|
||||||
|
final exactTrigResult = _getExactTrigResult(input);
|
||||||
|
if (exactTrigResult != null) {
|
||||||
|
return CalculationResult(
|
||||||
|
steps: steps,
|
||||||
|
finalAnswer: '\$\$$exactTrigResult\$\$',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// 预处理输入,将三角函数的参数从度转换为弧度
|
// 预处理输入,将三角函数的参数从度转换为弧度
|
||||||
String processedInput = _convertTrigToRadians(input);
|
String processedInput = _convertTrigToRadians(input);
|
||||||
|
|
||||||
@@ -431,6 +440,122 @@ ${b1}y &= ${c1 - a1 * x}
|
|||||||
|
|
||||||
/// ---- 辅助函数 ----
|
/// ---- 辅助函数 ----
|
||||||
|
|
||||||
|
/// 获取精确三角函数结果
|
||||||
|
String? _getExactTrigResult(String input) {
|
||||||
|
final cleanInput = input.replaceAll(' ', '').toLowerCase();
|
||||||
|
|
||||||
|
// 匹配 sin(角度) 模式
|
||||||
|
final sinMatch = RegExp(r'^sin\((\d+(?:\+\d+)*)\)$').firstMatch(cleanInput);
|
||||||
|
if (sinMatch != null) {
|
||||||
|
final angleExpr = sinMatch.group(1)!;
|
||||||
|
final angle = _evaluateAngleExpression(angleExpr);
|
||||||
|
if (angle != null) {
|
||||||
|
return _getSinExactValue(angle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 匹配 cos(角度) 模式
|
||||||
|
final cosMatch = RegExp(r'^cos\((\d+(?:\+\d+)*)\)$').firstMatch(cleanInput);
|
||||||
|
if (cosMatch != null) {
|
||||||
|
final angleExpr = cosMatch.group(1)!;
|
||||||
|
final angle = _evaluateAngleExpression(angleExpr);
|
||||||
|
if (angle != null) {
|
||||||
|
return _getCosExactValue(angle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 匹配 tan(角度) 模式
|
||||||
|
final tanMatch = RegExp(r'^tan\((\d+(?:\+\d+)*)\)$').firstMatch(cleanInput);
|
||||||
|
if (tanMatch != null) {
|
||||||
|
final angleExpr = tanMatch.group(1)!;
|
||||||
|
final angle = _evaluateAngleExpression(angleExpr);
|
||||||
|
if (angle != null) {
|
||||||
|
return _getTanExactValue(angle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 计算角度表达式(如 30+45 = 75)
|
||||||
|
int? _evaluateAngleExpression(String expr) {
|
||||||
|
final parts = expr.split('+');
|
||||||
|
int sum = 0;
|
||||||
|
for (final part in parts) {
|
||||||
|
final num = int.tryParse(part.trim());
|
||||||
|
if (num == null) return null;
|
||||||
|
sum += num;
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 获取 sin 的精确值
|
||||||
|
String? _getSinExactValue(int angle) {
|
||||||
|
// 标准化角度到 0-360 度
|
||||||
|
final normalizedAngle = angle % 360;
|
||||||
|
|
||||||
|
switch (normalizedAngle) {
|
||||||
|
case 0:
|
||||||
|
case 360:
|
||||||
|
return '0';
|
||||||
|
case 30:
|
||||||
|
return '\\frac{1}{2}';
|
||||||
|
case 45:
|
||||||
|
return '\\frac{\\sqrt{2}}{2}';
|
||||||
|
case 60:
|
||||||
|
return '\\frac{\\sqrt{3}}{2}';
|
||||||
|
case 75:
|
||||||
|
return '1 + \\frac{\\sqrt{2}}{2}';
|
||||||
|
case 90:
|
||||||
|
return '1';
|
||||||
|
case 120:
|
||||||
|
return '\\frac{\\sqrt{3}}{2}';
|
||||||
|
case 135:
|
||||||
|
return '\\frac{\\sqrt{2}}{2}';
|
||||||
|
case 150:
|
||||||
|
return '\\frac{1}{2}';
|
||||||
|
case 180:
|
||||||
|
return '0';
|
||||||
|
case 210:
|
||||||
|
return '-\\frac{1}{2}';
|
||||||
|
case 225:
|
||||||
|
return '-\\frac{\\sqrt{2}}{2}';
|
||||||
|
case 240:
|
||||||
|
return '-\\frac{\\sqrt{3}}{2}';
|
||||||
|
case 270:
|
||||||
|
return '-1';
|
||||||
|
case 300:
|
||||||
|
return '-\\frac{\\sqrt{3}}{2}';
|
||||||
|
case 315:
|
||||||
|
return '-\\frac{\\sqrt{2}}{2}';
|
||||||
|
case 330:
|
||||||
|
return '-\\frac{1}{2}';
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 获取 cos 的精确值
|
||||||
|
String? _getCosExactValue(int angle) {
|
||||||
|
// cos(angle) = sin(90 - angle)
|
||||||
|
final complementaryAngle = 90 - angle;
|
||||||
|
return _getSinExactValue(complementaryAngle.abs());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 获取 tan 的精确值
|
||||||
|
String? _getTanExactValue(int angle) {
|
||||||
|
// tan(angle) = sin(angle) / cos(angle)
|
||||||
|
final sinValue = _getSinExactValue(angle);
|
||||||
|
final cosValue = _getCosExactValue(angle);
|
||||||
|
|
||||||
|
if (sinValue != null && cosValue != null) {
|
||||||
|
if (cosValue == '0') return null; // 未定义
|
||||||
|
return '\\frac{$sinValue}{$cosValue}';
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// 将三角函数的参数从度转换为弧度
|
/// 将三角函数的参数从度转换为弧度
|
||||||
String _convertTrigToRadians(String input) {
|
String _convertTrigToRadians(String input) {
|
||||||
String result = input;
|
String result = input;
|
||||||
|
Reference in New Issue
Block a user