diff --git a/anbs_cp/Classes/CountConverter.cs b/anbs_cp/Classes/CountConverter.cs new file mode 100644 index 0000000..8545849 --- /dev/null +++ b/anbs_cp/Classes/CountConverter.cs @@ -0,0 +1,20 @@ +namespace anbs_cp.Classes; + +/// +/// Конвертер количества элементов +/// +public sealed class CountConverter : ValueConverter +{ + /// + /// Имена размеров файлов по умолчанию + /// + public static readonly string[] DefaultNames = {"", "тыс.", "млн."}; + + /// + /// Конструктор класса + /// + /// Массив имён размерностей + public CountConverter (string[] valueNames) : base(valueNames, 1000) + { + } +} diff --git a/anbs_cp/Classes/CountFormatter.cs b/anbs_cp/Classes/CountFormatter.cs index c908651..29774e0 100644 --- a/anbs_cp/Classes/CountFormatter.cs +++ b/anbs_cp/Classes/CountFormatter.cs @@ -1,4 +1,6 @@ -namespace anbs_cp.Classes; +using anbs_cp.Interfaces; + +namespace anbs_cp.Classes; /// /// Форматирует число элементов в понятную строку diff --git a/anbs_cp/Classes/FileSizeConverter.cs b/anbs_cp/Classes/FileSizeConverter.cs new file mode 100644 index 0000000..dfd3155 --- /dev/null +++ b/anbs_cp/Classes/FileSizeConverter.cs @@ -0,0 +1,20 @@ +namespace anbs_cp.Classes; + +/// +/// Конвертер размеров файлов +/// +public sealed class FileSizeConverter : ValueConverter +{ + /// + /// Имена размеров файлов по умолчанию + /// + public static readonly string[] DefaultNames = {"байт", "Кб", "Мб", "Гб", "Тб"}; + + /// + /// Конструктор класса + /// + /// Массив имён размерностей + public FileSizeConverter (string[] valueNames) : base(valueNames, 1024) + { + } +} diff --git a/anbs_cp/Classes/FileSizeFormatter.cs b/anbs_cp/Classes/FileSizeFormatter.cs index 1cae56d..b3e2891 100644 --- a/anbs_cp/Classes/FileSizeFormatter.cs +++ b/anbs_cp/Classes/FileSizeFormatter.cs @@ -1,4 +1,6 @@ -namespace anbs_cp.Classes; +using anbs_cp.Interfaces; + +namespace anbs_cp.Classes; /// /// Форматирует размер файла/папки в понятную строку diff --git a/anbs_cp/Classes/ValueConverter.cs b/anbs_cp/Classes/ValueConverter.cs new file mode 100644 index 0000000..27202b6 --- /dev/null +++ b/anbs_cp/Classes/ValueConverter.cs @@ -0,0 +1,72 @@ +using anbs_cp.Interfaces; + +namespace anbs_cp.Classes; + +/// +/// Абстрактный класс конвертера величин для отображения (улучшенный аналог ValueFormatter) +/// +public abstract class ValueConverter: IValueConverter +{ + /// + /// Конструктор + /// + /// Массив имён размерностей + /// Делитель + protected ValueConverter (string[] valueNames, long divider) + { + ValueNames = valueNames; + Divider = divider; + } + + #region Реализация интерфейса + /// + /// Массив имён размерностей + /// + public string[] ValueNames { get; init; } + + /// + /// Делитель + /// + public long Divider { get; init; } + #endregion + + #region Методы + /// + /// Функция конвертирования в строку + /// + /// Значение + /// Конвертирование значение в строку + public string Convert (long value) + { + (decimal, int) result = DivideIt(value, 0); + + return $"{result.Item1:F2} {ValueNames[result.Item2]}"; + } + + + /// + /// Рекурсивная функция деления + /// + /// Число + /// Счётчик вызова рекурсии + /// Число в остатке и количество вызовов рекурсии + private (decimal, int) DivideIt (decimal value, int count) + { + //Если счёт уже больше количества названий + if (count > ValueNames.Length) + return (value, count); + + //Если частное уже меньше делителя, то прерываем цикл + if (value < Divider) + return (value, count); + + //Увеличиваем счётчик... + count++; + + //... и продолжаем цикл + return DivideIt(value / Divider, count); + + + } + #endregion +} \ No newline at end of file diff --git a/anbs_cp/Interfaces/IValueConverter.cs b/anbs_cp/Interfaces/IValueConverter.cs new file mode 100644 index 0000000..56e8a1c --- /dev/null +++ b/anbs_cp/Interfaces/IValueConverter.cs @@ -0,0 +1,17 @@ +namespace anbs_cp.Interfaces; + +/// +/// Интерфейс конвертера величин для отображения (улучшенный аналог IValueFormatter) +/// +public interface IValueConverter +{ + /// + /// Массив имён размерностей + /// + public string[] ValueNames { get; init; } + + /// + /// Делитель + /// + public long Divider { get; init; } +} \ No newline at end of file diff --git a/anbs_cp/Classes/IValueFormatter.cs b/anbs_cp/Interfaces/IValueFormatter.cs similarity index 98% rename from anbs_cp/Classes/IValueFormatter.cs rename to anbs_cp/Interfaces/IValueFormatter.cs index eb1f2bd..2ae523d 100644 --- a/anbs_cp/Classes/IValueFormatter.cs +++ b/anbs_cp/Interfaces/IValueFormatter.cs @@ -1,4 +1,4 @@ -namespace anbs_cp.Classes; +namespace anbs_cp.Interfaces; /// /// Форматирует размерности в понятную строку diff --git a/anbs_cp/anbs_cp.csproj b/anbs_cp/anbs_cp.csproj index e1ecddc..bf3f4ba 100644 --- a/anbs_cp/anbs_cp.csproj +++ b/anbs_cp/anbs_cp.csproj @@ -2,7 +2,7 @@ net7.0 - 2022.1224.0 + 2022.1224.1 Alexander Babaev ANB Software Components Pack Library of some useful functions in C# language. @@ -15,8 +15,8 @@ True https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack - 2022.1224.0 - 2022.1224.0 + 2022.1224.1 + 2022.1224.1 ANBSoftware.ComponentsPack MIT 6.0 diff --git a/anbsoftware.componentspack.sln.DotSettings b/anbsoftware.componentspack.sln.DotSettings index 68f48c1..731bddc 100644 --- a/anbsoftware.componentspack.sln.DotSettings +++ b/anbsoftware.componentspack.sln.DotSettings @@ -4,5 +4,7 @@ True True True + True + True True True \ No newline at end of file diff --git a/demo/CountValueTest.Designer.cs b/demo/CountValueTest.Designer.cs index aef3ab5..2088b44 100644 --- a/demo/CountValueTest.Designer.cs +++ b/demo/CountValueTest.Designer.cs @@ -34,6 +34,7 @@ sealed partial class CountValueTest this.CalculateButton = new System.Windows.Forms.Button(); this.SelectFormatterLabel = new System.Windows.Forms.Label(); this.SelectFormatterBox = new System.Windows.Forms.ComboBox(); + this.calculateConvButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // InsertDataLabel @@ -49,7 +50,7 @@ sealed partial class CountValueTest // this.InsertDataBox.Location = new System.Drawing.Point(12, 86); this.InsertDataBox.Name = "InsertDataBox"; - this.InsertDataBox.Size = new System.Drawing.Size(341, 24); + this.InsertDataBox.Size = new System.Drawing.Size(457, 24); this.InsertDataBox.TabIndex = 1; this.InsertDataBox.TextChanged += new System.EventHandler(this.InsertDataBox_TextChanged); // @@ -60,7 +61,7 @@ sealed partial class CountValueTest this.ResultLabel.Margin = new System.Windows.Forms.Padding(5); this.ResultLabel.Name = "ResultLabel"; this.ResultLabel.Padding = new System.Windows.Forms.Padding(5); - this.ResultLabel.Size = new System.Drawing.Size(365, 79); + this.ResultLabel.Size = new System.Drawing.Size(478, 79); this.ResultLabel.TabIndex = 2; this.ResultLabel.Text = "&Вставьте любое целочисленное значение в поле выше и нажмите кнопку \"Вычислить\", " + "чтобы увидеть результат..."; @@ -68,11 +69,11 @@ sealed partial class CountValueTest // // CalculateButton // - this.CalculateButton.Location = new System.Drawing.Point(140, 116); + this.CalculateButton.Location = new System.Drawing.Point(12, 116); this.CalculateButton.Name = "CalculateButton"; - this.CalculateButton.Size = new System.Drawing.Size(103, 23); + this.CalculateButton.Size = new System.Drawing.Size(234, 23); this.CalculateButton.TabIndex = 3; - this.CalculateButton.Text = "В&ычислить"; + this.CalculateButton.Text = "В&ычислить обработчиком"; this.CalculateButton.UseVisualStyleBackColor = true; this.CalculateButton.Click += new System.EventHandler(this.CalculateButton_Click); // @@ -95,14 +96,25 @@ sealed partial class CountValueTest "Обработчик размера файла"}); this.SelectFormatterBox.Location = new System.Drawing.Point(12, 29); this.SelectFormatterBox.Name = "SelectFormatterBox"; - this.SelectFormatterBox.Size = new System.Drawing.Size(341, 25); + this.SelectFormatterBox.Size = new System.Drawing.Size(457, 25); this.SelectFormatterBox.TabIndex = 5; // + // calculateConvButton + // + this.calculateConvButton.Location = new System.Drawing.Point(252, 116); + this.calculateConvButton.Name = "calculateConvButton"; + this.calculateConvButton.Size = new System.Drawing.Size(217, 23); + this.calculateConvButton.TabIndex = 6; + this.calculateConvButton.Text = "&Вычислить конвертером"; + this.calculateConvButton.UseVisualStyleBackColor = true; + this.calculateConvButton.Click += new System.EventHandler(this.calculateConvButton_Click); + // // CountValueTest // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 17F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(365, 222); + this.ClientSize = new System.Drawing.Size(478, 222); + this.Controls.Add(this.calculateConvButton); this.Controls.Add(this.SelectFormatterBox); this.Controls.Add(this.SelectFormatterLabel); this.Controls.Add(this.CalculateButton); @@ -131,4 +143,5 @@ sealed partial class CountValueTest private Button CalculateButton; private Label SelectFormatterLabel; private ComboBox SelectFormatterBox; + private Button calculateConvButton; } \ No newline at end of file diff --git a/demo/CountValueTest.cs b/demo/CountValueTest.cs index e8c7202..4afcb46 100644 --- a/demo/CountValueTest.cs +++ b/demo/CountValueTest.cs @@ -1,4 +1,5 @@ using anbs_cp.Classes; +using anbs_cp.Interfaces; namespace demo; @@ -37,4 +38,19 @@ public sealed partial class CountValueTest: Form if (value > long.MaxValue) InsertDataBox.Text = long.MaxValue.ToString(); } + + private void calculateConvButton_Click (object sender, EventArgs e) + { + if (string.IsNullOrEmpty(InsertDataBox.Text)) + return; + + long value = TypeConverter.StrToInt64(InsertDataBox.Text); + + ResultLabel.Text = SelectFormatterBox.SelectedIndex switch + { + 0 => new CountConverter(CountConverter.DefaultNames).Convert(value), + 1 => new FileSizeConverter(FileSizeConverter.DefaultNames).Convert(value), + _ => new CountConverter(CountConverter.DefaultNames).Convert(value) + }; + } } \ No newline at end of file