diff --git a/Common.BasicHelper/LiteLogger/LoggerManager.cs b/Common.BasicHelper/LiteLogger/LoggerManager.cs deleted file mode 100644 index a1e0745..0000000 --- a/Common.BasicHelper/LiteLogger/LoggerManager.cs +++ /dev/null @@ -1,138 +0,0 @@ -using Common.BasicHelper.Util.Extension; -using System; -using System.Collections.Generic; -using System.IO; -using System.Threading.Tasks; - -namespace Common.BasicHelper.LiteLogger; - -public class LoggerManager -{ - /// - /// 日志级别 - /// - public enum LogLevel - { - Off = 0, - Fatal = 1, - Error = 2, - Warn = 3, - Info = 4, - Debug = 5, - Trace = 6 - } - - /// - /// 日志记录器结构 - /// - public struct LoggerInfo - { - public LoggerInfo(string name_in, string folder, string descr_in = "Nice Logger!", - LogLevel lv = LogLevel.Error, int lfs = 1024) - { - name = name_in; - descr = descr_in; - Level = lv; - limitedFileSize = lfs; - - file = new FileInfo($"{Path.GetFullPath(folder)}/" + - $"Log_{DateTime.Now:yyyy.MM.dd}_{lv}.log"); - if (!file.Exists) file.Create().Dispose(); - - logged_count = 0; - logged_char_count = 0; - } - - public LogLevel Level; - public string name; - public string descr; - public ulong logged_count; - public ulong logged_char_count; - - // File Size in KB - public int limitedFileSize; - - private readonly FileInfo file; - - /// - /// 写入日志 - /// - /// 内容 - /// 内容等级 - public void Log(string content, LogLevel lv = LogLevel.Error) - { - if (lv <= Level) - { - StreamWriter sw; - - if (file.Length >= limitedFileSize * 1024) - sw = file.CreateText(); - else sw = file.AppendText(); - - sw.WriteLine($"{DateTime.Now:yyyy.MM.dd-HH:mm:ss}\t{content}"); - - sw.CloseAndDispose(); - } - } - - /// - /// 异步写入日志 - /// - /// 内容 - /// 内容等级 - public async Task LogAsync(string content, LogLevel lv = LogLevel.Error) - { - if (lv <= Level) - { - StreamWriter sw; - - if (file.Length >= limitedFileSize * 1024) - sw = file.CreateText(); - else sw = file.AppendText(); - - await sw.WriteLineAsync($"{DateTime.Now:yyyy.MM.dd-HH:mm:ss}\t{content}"); - - sw.CloseAndDispose(); - } - } - - } - - private readonly Dictionary loggerPool = new(); - - /// - /// 日志记录器池 - /// - public Dictionary LoggerPool => loggerPool; - - /// - /// 追加日志记录器 - /// - /// 日志记录器名称 - /// 日志记录器结构 - public void AppendLogger(string name, LoggerInfo logger) => loggerPool.Add(name, logger); - - /// - /// 记录日志 - /// - /// 日志记录器名称 - /// 日志内容 - /// 日志记录等级 - public void Log(string logger_name, string content, LogLevel lv = LogLevel.Error) - { - if (LoggerPool.ContainsKey(logger_name)) - LoggerPool[logger_name].Log(content, lv); - } - - /// - /// 异步记录日志 - /// - /// 日志记录器名称 - /// 日志内容 - /// 日志记录等级 - public async Task LogAsync(string logger_name, string content, LogLevel lv = LogLevel.Error) - { - if (LoggerPool.ContainsKey(logger_name)) - await LoggerPool[logger_name].LogAsync(content, lv); - } -} diff --git a/Common.BasicHelper/Math/Equation.cs b/Common.BasicHelper/Math/Equation.cs new file mode 100644 index 0000000..dc538bc --- /dev/null +++ b/Common.BasicHelper/Math/Equation.cs @@ -0,0 +1,122 @@ +namespace Common.BasicHelper.Math; + +public class Equation +{ + /// + /// 解方程 - 二元一次 + /// + /// 方程一的系数 a + /// 方程一的系数 b + /// 方程一的结果 c + /// 方程二的系数 a + /// 方程二的系数 b + /// 方程二的系数 c + /// 结果 - x,y - 组成的数组 + public static double[] SolveEquation( + double a1, double b1, double c1, + double a2, double b2, double c2) + { + var rst = new double[2]; + rst[1] = (c2 - (a2 * c1 / a1)) / (b2 - (a2 * b1 / a1)); + rst[0] = (c1 - (b1 * rst[1])) / a1; + return rst; + } + + /// + /// 解方程 - 三元一次 + /// + /// 方程一的系数 a + /// 方程一的系数 b + /// 方程一的系数 c + /// 方程一的结果 d + /// 方程二的系数 a + /// 方程二的系数 b + /// 方程二的系数 c + /// 方程二的结果 d + /// 结果 - x,y - 组成的数组 + public static double[] SolveEquation( + double a1, double b1, double c1, double d1, + double a2, double b2, double c2, double d2, + double a3, double b3, double c3, double d3 + ) + { + var rst = new double[3]; + double na1, nb1, nc1, na2, nb2, nc2, x, y, z; + + na1 = a1 * b2 - a2 * b1; nb1 = a1 * c2 - a2 * c1; nc1 = a1 * d2 - a2 * d1; + + na2 = a1 * b3 - a3 * b1; nb2 = a1 * c3 - a3 * c1; nc2 = a1 * d3 - a3 * d1; + + var y_z = SolveEquation(na1, nb1, nc1, na2, nb2, nc2); + y = y_z[0]; z = y_z[1]; + x = (d1 - b1 * y - c1 * z) / a1; + rst[0] = x; rst[1] = y; rst[2] = z; + return rst; + } + + /* a1x+b1y+c1z+d1m=e1 + * a2x+b2y+c2z+d2m=e2 + * a3x+b3y+c3z+d3m=e3 + * a4x+b4y+c4z+d4m=e4 + * + * a1x=e1-b1y-c1z-d1m + * x=(e1-b1y-c1z-d1m)/a1 + * + * a2(e1-b1y-c1z-d1m)/a1+b2y+c2z+d2m=e2 + * a3(e1-b1y-c1z-d1m)/a1+b3y+c3z+d3m=e3 + * a4(e1-b1y-c1z-d1m)/a1+b4y+c4z+d4m=e4 + * + * (a1*b2-a2*b1)y+(a1*c2-a2*c1)z+(a1*d2-a2*d1)m=a1*e2-a2*e1 + * (a1*b3-a3*b1)y+(a1*c3-a3*c1)z+(a1*d3-a3*d1)m=a1*e3-a3*e1 + * (a1*b4-a4*b1)y+(a1*c4-a4*c1)z+(a1*d4-a4*d1)m=a1*e4-a4*e1 + */ + + /// + /// 解方程组 - 四元一次 + /// + /// 方程一的系数 a + /// 方程一的系数 b + /// 方程一的系数 c + /// 方程一的系数 d + /// 方程一的结果 e + /// 方程二的系数 a + /// 方程二的系数 b + /// 方程二的系数 c + /// 方程二的系数 d + /// 方程二的结果 e + /// 方程三的系数 a + /// 方程三的系数 b + /// 方程三的系数 c + /// 方程三的系数 d + /// 方程三的结果 e + /// 方程四的系数 a + /// 方程四的系数 b + /// 方程四的系数 c + /// 方程四的系数 d + /// 方程四的结果 e + /// x_y_z_t 的数组,对应角标分别为 0,1,2,3 + public static double[] SolveEquation( + double a1, double b1, double c1, double d1, double e1, + double a2, double b2, double c2, double d2, double e2, + double a3, double b3, double c3, double d3, double e3, + double a4, double b4, double c4, double d4, double e4 + ) + { + var rst = new double[4]; + double na1, nb1, nc1, nd1, na2, nb2, nc2, nd2, na3, nb3, nc3, nd3, x, y, z, m; + na1 = a1 * b2 - a2 * b1; nb1 = a1 * c2 - a2 * c1; + nc1 = a1 * d2 - a2 * d1; nd1 = a1 * e2 - a2 * e1; + na2 = a1 * b3 - a3 * b1; nb2 = a1 * c3 - a3 * c1; + nc2 = a1 * d3 - a3 * d1; nd2 = a1 * e3 - a3 * e1; + na3 = a1 * b4 - a4 * b1; nb3 = a1 * c4 - a4 * c1; + nc3 = a1 * d4 - a4 * d1; nd3 = a1 * e4 - a4 * e1; + var y_z_m = SolveEquation( + na1, nb1, nc1, nd1, + na2, nb2, nc2, nd2, + na3, nb3, nc3, nd3); + y = y_z_m[0]; z = y_z_m[1]; m = y_z_m[2]; + x = (e1 - b1 * y - c1 * z - d1 * m) / a1; + rst[0] = x; rst[1] = y; rst[2] = z; rst[3] = m; + return rst; + } +} diff --git a/Common.BasicHelper/Math/Standard.cs b/Common.BasicHelper/Math/Standard.cs index 30468af..e997701 100644 --- a/Common.BasicHelper/Math/Standard.cs +++ b/Common.BasicHelper/Math/Standard.cs @@ -53,4 +53,19 @@ public static T Max(params T[] input) where T : IComparable return max; } + + /// + /// 获取指定位数的数字 + /// 例如:456 取 2 位数返回 5,6892 取 4 位数返回 6 + /// + /// 指示要被获取指定位数字的数字 + /// 指示要获取的位数 + /// 返回指定位上数字 + public static int GetBit(int number, int bit) + { + var pow = 10; + for (var i = 0; i < bit - 1; ++i) + pow *= pow; + return (number % pow) / (pow / 10); + } } diff --git a/Common.BasicHelper/Math/Tricks.cs b/Common.BasicHelper/Math/Tricks.cs new file mode 100644 index 0000000..97e7f03 --- /dev/null +++ b/Common.BasicHelper/Math/Tricks.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Common.BasicHelper.Math; + +public class Tricks +{ + /// + /// 获取一个乘法表 + /// + /// 乘法表从哪开始 + /// 乘法表从哪结束 + /// 方向, true 为正三角, false 为反三角, 默认为正 + /// 一个分行的乘法表 + public static string GetMultiplicationTable(int from, int to, bool direction = true) + { + var sb = new StringBuilder(); + + if (direction) + { + for (var x = from; x <= to; ++x) + { + for (var y = from; y <= x; ++y) + sb.Append($"{y}*{x}={x * y}\t"); + sb.AppendLine(); + } + } + else + { + for (var x = from; x >= from; --x) + { + for (var y = from; y <= x; ++y) + sb.Append($"{y}*{x}={x * y}\t"); + sb.AppendLine(); + } + } + + return sb.ToString(); + } +} + +public static class TricksExtensions +{ + /// + /// 获取一个乘法表 + /// + /// 乘法表从哪开始 + /// 乘法表从哪结束 + /// 方向, true 为正三角, false 为反三角, 默认为正 + /// 一个分行的乘法表 + public static string GetMultiplicationTableTo(this int from, int to, bool direction = true) + => Tricks.GetMultiplicationTable(from, to, direction); +} diff --git a/Common.BasicHelper/Util/Extension/CleanHelper.cs b/Common.BasicHelper/Util/Extension/CleanHelper.cs index 41e24b1..c6ab8ce 100644 --- a/Common.BasicHelper/Util/Extension/CleanHelper.cs +++ b/Common.BasicHelper/Util/Extension/CleanHelper.cs @@ -6,7 +6,6 @@ namespace Common.BasicHelper.Util.Extension; public static class CleanHelper { - /// /// 关闭并释放对象 /// diff --git a/Common.BasicHelper/Util/Extension/QueueHelper.cs b/Common.BasicHelper/Util/Extension/QueueHelper.cs index 03c4f46..d463ad0 100644 --- a/Common.BasicHelper/Util/Extension/QueueHelper.cs +++ b/Common.BasicHelper/Util/Extension/QueueHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace Common.BasicHelper.Util.Extension; @@ -40,11 +41,11 @@ public static Queue Pop(this Queue queue) public static bool IsEmpty(this Queue queue) => queue.Count == 0; /// - /// 判断队列是否为空 + /// 判断队列是否非空 /// /// 队列类型 /// 队列 - /// 是否为空 + /// 是否非空 public static bool IsNotEmpty(this Queue queue) => queue.Count > 0; /// @@ -81,4 +82,32 @@ Queue func() } else return func(); } + + /// + /// 异步遍历队列对每一个元素执行操作, 执行完毕后返回队列本身 + /// + /// 队列类型 + /// 队列 + /// 对元素的操作 + /// 是否将出队元素重新入队 + /// 操作锁 + /// 返回队列本身的任务 + public static async Task> ForEachAsync(this Queue queue, Action action, + bool reappend = false) + { + Queue func() + { + var count = queue.Count; + while (count > 0) + { + var item = queue.Dequeue(); + action.Invoke(item); + --count; + if (reappend) queue.Enqueue(item); + } + return queue; + } + + return await Task.Run(func); + } } diff --git a/Common.BasicHelper/Util/Extension/StringHelper.cs b/Common.BasicHelper/Util/Extension/StringHelper.cs index ac4e7a7..0048732 100644 --- a/Common.BasicHelper/Util/Extension/StringHelper.cs +++ b/Common.BasicHelper/Util/Extension/StringHelper.cs @@ -1,4 +1,6 @@ -using System.Text; +using System.IO; +using System.Text; +using System.Threading.Tasks; namespace Common.BasicHelper.Util.Extension; @@ -25,4 +27,32 @@ public static string Num2UpperChar(this string source) return sb.ToString(); } + /// + /// 从磁盘读取全部文本 + /// + /// 文件路径 + /// 文本内容, 若文件不存在则返回空 + public static string ReadAllTextFromDisk(this string path) + { + if (File.Exists(path)) + return File.ReadAllText(path); + else return null; + } + +#if NETSTANDARD2_1_OR_GREATER + + /// + /// 从磁盘异步读取全部文本 + /// + /// 文件路径 + /// 文本内容读取任务, 若文件不存在则返回空 + public static Task ReadAllTextFromDiskAsync(this string path) + { + if (File.Exists(path)) + return File.ReadAllTextAsync(path); + else return null; + } + +#endif + } diff --git a/Common.BasicHelper/Util/RegexStrings.cs b/Common.BasicHelper/Util/RegexStrings.cs index 90ce838..6223820 100644 --- a/Common.BasicHelper/Util/RegexStrings.cs +++ b/Common.BasicHelper/Util/RegexStrings.cs @@ -4,7 +4,5 @@ public struct RegexStrings { public const string GUID_Part_Parttern = @"([A-Z]|[a-z]|[0-9])"; - public const string LiteDB_Name_Limit = @"([A-Z]|[a-z]|[0-9])"; - public const string Version_Parse_STR = @"([0-9]*\.[0-9]*\.[0-9]*(\.[0-9]*)?)"; }