From 26ca6d5e8f4cb81aac078d5390f136a6c7c81331 Mon Sep 17 00:00:00 2001 From: MakesYT <42534870+MakesYT@users.noreply.github.com> Date: Sat, 21 Oct 2023 18:01:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=AE=A1=E7=AE=97=E5=B0=8F=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/SDKs/Tools/Math.cs | 57 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/Core/SDKs/Tools/Math.cs b/Core/SDKs/Tools/Math.cs index 55d8a9d..8be1b79 100644 --- a/Core/SDKs/Tools/Math.cs +++ b/Core/SDKs/Tools/Math.cs @@ -1,5 +1,4 @@ -using System.Numerics; -using System.Text.RegularExpressions; +using System.Text.RegularExpressions; using log4net; namespace Core.SDKs.Tools; @@ -8,49 +7,49 @@ public class Math { private static readonly ILog log = LogManager.GetLogger(nameof(Math)); - // 评估一个复杂数学表达式,返回一个BigInteger类型的值 - public static BigInteger Evaluate(string expression) + // 评估一个复杂数学表达式,返回一个decimal类型的值 + public static decimal Evaluate(string expression) { //log.Debug($"计算{expression}"); // 去掉空格 expression = expression.Replace(" ", ""); // 处理括号 - while (expression.Contains("(")) + while (expression.Contains('(')) { // 找到最内层的括号 - int start = expression.LastIndexOf("("); - int end = expression.IndexOf(")", start); + var start = expression.LastIndexOf("("); + var end = expression.IndexOf(")", start); // 计算括号内的子表达式 - BigInteger subResult = Evaluate(expression.Substring(start + 1, end - start - 1)); + var subResult = Evaluate(expression.Substring(start + 1, end - start - 1)); // 用子表达式的结果替换括号 expression = expression.Remove(start, end - start + 1).Insert(start, subResult.ToString()); } // 处理幂运算 - while (expression.Contains("^")) + while (expression.Contains('^')) { // 使用正则表达式匹配第一个幂运算符及其两边的操作数 - Match match = Regex.Match(expression, @"(-?\d+)\^(-?\d+)"); + var match = Regex.Match(expression, @"(-?\d+(\.\d+)?)\^(-?\d+(\.\d+)?)"); // 获取操作数和运算符 - BigInteger left = BigInteger.Parse(match.Groups[1].Value); - BigInteger right = BigInteger.Parse(match.Groups[2].Value); + var left = decimal.Parse(match.Groups[1].Value); + var right = decimal.Parse(match.Groups[4].Value); // 计算幂运算结果 - BigInteger subResult = BigInteger.Pow(left, (int)right); + var subResult = (decimal)System.Math.Pow((double)left, (double)right); // 用幂运算结果替换原来的子表达式 expression = expression.Remove(match.Index, match.Length).Insert(match.Index, subResult.ToString()); } // 处理乘法和除法 - while (expression.Contains("*") || expression.Contains("/")) + while (expression.Contains('*') || expression.Contains('/')) { // 使用正则表达式匹配第一个乘法或除法运算符及其两边的操作数 - Match match = Regex.Match(expression, @"(-?\d+)(\*|/)(-?\d+)"); + var match = Regex.Match(expression, @"(-?\d+(\.\d+)?)(\*|/)(-?\d+(\.\d+)?)"); // 获取操作数和运算符 - BigInteger left = BigInteger.Parse(match.Groups[1].Value); - BigInteger right = BigInteger.Parse(match.Groups[3].Value); - string op = match.Groups[2].Value; + var left = decimal.Parse(match.Groups[1].Value); + var right = decimal.Parse(match.Groups[4].Value); + var op = match.Groups[3].Value; // 计算乘法或除法结果 - BigInteger subResult = op == "*" ? left * right : left / right; + var subResult = op == "*" ? left * right : left / right; if (match.Groups[1].Value.StartsWith("-")) { expression = expression.Remove(match.Index, match.Length) @@ -63,29 +62,29 @@ public static BigInteger Evaluate(string expression) } // 处理加法和减法 - while (expression.Contains("+") || expression.Contains("-")) + while (expression.Contains('+') || expression.Contains('-')) { // 使用正则表达式匹配第一个加法或减法运算符及其两边的操作数 - Match match = Regex.Match(expression, @"(-?\d+)(\+|-)(-?\d+)"); + var match = Regex.Match(expression, @"(-?\d+(\.?\d+?)?)(\+|-)(-?\d+(\.\d+)?)"); if (string.IsNullOrWhiteSpace(match.Value)) { break; } // 获取操作数和运算符 - BigInteger left = BigInteger.Parse(match.Groups[1].Value); - BigInteger right = BigInteger.Parse(match.Groups[3].Value); - string op = match.Groups[2].Value; + var left = decimal.Parse(match.Groups[1].Value); + var right = decimal.Parse(match.Groups[4].Value); + var op = match.Groups[3].Value; // 计算加法或减法结果 - BigInteger subResult = op == "+" ? left + right : left - right; + var subResult = op == "+" ? left + right : left - right; // 用加法或减法结果替换原来的子表达式 expression = expression.Remove(match.Index, match.Length).Insert(match.Index, subResult.ToString()); } - // 返回最终结果,转换为BigInteger类型 + // 返回最终结果,转换为decimal类型 - var bigInteger = BigInteger.Parse(expression); - //log.Debug($"{bigInteger}"); - return bigInteger; + var deciMal = decimal.Parse(expression); + //log.Debug($"{deciMal}"); + return deciMal; } } \ No newline at end of file