| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- // utils/math_utils.dart
- import 'package:rational/rational.dart';
- class MathUtils {
- // 添加常量
- static final Rational zero = Rational.zero;
- static final Rational one = Rational.one;
- static final Rational two = Rational.fromInt(2);
- static final Rational half = Rational.fromInt(1) / Rational.fromInt(2);
- static Rational parseRational(String input) {
- try {
- // 去除空格
- input = input.trim();
-
- if (input.contains('/')) {
- List<String> parts = input.split('/');
- if (parts.length == 2) {
- String numerator = parts[0].trim();
- String denominator = parts[1].trim();
-
- // 修复:直接使用 Rational 构造函数而不是 parse
- BigInt num = BigInt.parse(numerator);
- BigInt den = BigInt.parse(denominator);
-
- if (den == BigInt.zero) {
- throw FormatException('分母不能为零');
- }
-
- return Rational(num, den);
- }
- }
-
- // 处理小数
- if (input.contains('.')) {
- return _decimalToRational(input);
- }
-
- // 处理整数
- return Rational.parse(input);
- } catch (e) {
- throw FormatException('无效的分数格式: $input');
- }
- }
- static Rational _decimalToRational(String decimalStr) {
- // 更精确的小数转分数实现
- if (!decimalStr.contains('.')) {
- return Rational.parse(decimalStr);
- }
-
- List<String> parts = decimalStr.split('.');
- String integerPart = parts[0];
- String decimalPart = parts[1];
-
- // 处理负号
- bool isNegative = integerPart.startsWith('-');
- if (isNegative) {
- integerPart = integerPart.substring(1);
- }
-
- // 如果整数部分是空的,设为0
- if (integerPart.isEmpty) {
- integerPart = '0';
- }
-
- // 计算分母:10的decimalPart长度次方
- BigInt denominator = BigInt.from(10).pow(decimalPart.length);
- // 计算分子:整数部分 * 分母 + 小数部分
- BigInt numerator =
- BigInt.parse(integerPart) * denominator + BigInt.parse(decimalPart);
-
- if (isNegative) {
- numerator = -numerator;
- }
-
- return Rational(numerator, denominator);
- }
- static String rationalToString(Rational rational) {
- // 简化分数显示
- if (rational.denominator == BigInt.one) {
- return rational.numerator.toString();
- }
-
- // 尝试约分
- Rational simplified = rational;
-
- return '${simplified.numerator}/${simplified.denominator}';
- }
- static Rational det2(Rational a, Rational b, Rational c, Rational d) {
- return a * d - b * c;
- }
- static List<Rational> solveLinear2(
- Rational a1, Rational b1, Rational c1,
- Rational a2, Rational b2, Rational c2,
- ) {
- final det = det2(a1, b1, a2, b2);
- if (det == zero) {
- throw Exception('方程组无解或有无穷多解');
- }
- final x = det2(c1, b1, c2, b2) / det;
- final y = det2(a1, c1, a2, c2) / det;
- return [x, y];
- }
- // 创建 Rational 从 BigInt
- static Rational fromBigInt(BigInt value) {
- return Rational(value);
- }
- // 创建 Rational 从 int
- static Rational fromInt(int value) {
- return Rational.fromInt(value);
- }
-
- // 计算最大公约数
- static BigInt _gcd(BigInt a, BigInt b) {
- while (b != BigInt.zero) {
- final temp = b;
- b = a % b;
- a = temp;
- }
- return a;
- }
- }
|