20221224-1
This commit is contained in:
parent
2ea36fe8a0
commit
af7a5dd299
20
anbs_cp/Classes/CountConverter.cs
Normal file
20
anbs_cp/Classes/CountConverter.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Конвертер количества элементов
|
||||
/// </summary>
|
||||
public sealed class CountConverter : ValueConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Имена размеров файлов по умолчанию
|
||||
/// </summary>
|
||||
public static readonly string[] DefaultNames = {"", "тыс.", "млн."};
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор класса
|
||||
/// </summary>
|
||||
/// <param name="valueNames">Массив имён размерностей</param>
|
||||
public CountConverter (string[] valueNames) : base(valueNames, 1000)
|
||||
{
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
namespace anbs_cp.Classes;
|
||||
using anbs_cp.Interfaces;
|
||||
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Форматирует число элементов в понятную строку
|
||||
|
20
anbs_cp/Classes/FileSizeConverter.cs
Normal file
20
anbs_cp/Classes/FileSizeConverter.cs
Normal file
@ -0,0 +1,20 @@
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Конвертер размеров файлов
|
||||
/// </summary>
|
||||
public sealed class FileSizeConverter : ValueConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Имена размеров файлов по умолчанию
|
||||
/// </summary>
|
||||
public static readonly string[] DefaultNames = {"байт", "Кб", "Мб", "Гб", "Тб"};
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор класса
|
||||
/// </summary>
|
||||
/// <param name="valueNames">Массив имён размерностей</param>
|
||||
public FileSizeConverter (string[] valueNames) : base(valueNames, 1024)
|
||||
{
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
namespace anbs_cp.Classes;
|
||||
using anbs_cp.Interfaces;
|
||||
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Форматирует размер файла/папки в понятную строку
|
||||
|
72
anbs_cp/Classes/ValueConverter.cs
Normal file
72
anbs_cp/Classes/ValueConverter.cs
Normal file
@ -0,0 +1,72 @@
|
||||
using anbs_cp.Interfaces;
|
||||
|
||||
namespace anbs_cp.Classes;
|
||||
|
||||
/// <summary>
|
||||
/// Абстрактный класс конвертера величин для отображения (улучшенный аналог ValueFormatter)
|
||||
/// </summary>
|
||||
public abstract class ValueConverter: IValueConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
/// <param name="valueNames">Массив имён размерностей</param>
|
||||
/// <param name="divider">Делитель</param>
|
||||
protected ValueConverter (string[] valueNames, long divider)
|
||||
{
|
||||
ValueNames = valueNames;
|
||||
Divider = divider;
|
||||
}
|
||||
|
||||
#region Реализация интерфейса
|
||||
/// <summary>
|
||||
/// Массив имён размерностей
|
||||
/// </summary>
|
||||
public string[] ValueNames { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Делитель
|
||||
/// </summary>
|
||||
public long Divider { get; init; }
|
||||
#endregion
|
||||
|
||||
#region Методы
|
||||
/// <summary>
|
||||
/// Функция конвертирования в строку
|
||||
/// </summary>
|
||||
/// <param name="value">Значение</param>
|
||||
/// <returns>Конвертирование значение в строку</returns>
|
||||
public string Convert (long value)
|
||||
{
|
||||
(decimal, int) result = DivideIt(value, 0);
|
||||
|
||||
return $"{result.Item1:F2} {ValueNames[result.Item2]}";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Рекурсивная функция деления
|
||||
/// </summary>
|
||||
/// <param name="value">Число</param>
|
||||
/// <param name="count">Счётчик вызова рекурсии</param>
|
||||
/// <returns>Число в остатке и количество вызовов рекурсии</returns>
|
||||
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
|
||||
}
|
17
anbs_cp/Interfaces/IValueConverter.cs
Normal file
17
anbs_cp/Interfaces/IValueConverter.cs
Normal file
@ -0,0 +1,17 @@
|
||||
namespace anbs_cp.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Интерфейс конвертера величин для отображения (улучшенный аналог IValueFormatter)
|
||||
/// </summary>
|
||||
public interface IValueConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Массив имён размерностей
|
||||
/// </summary>
|
||||
public string[] ValueNames { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Делитель
|
||||
/// </summary>
|
||||
public long Divider { get; init; }
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
namespace anbs_cp.Classes;
|
||||
namespace anbs_cp.Interfaces;
|
||||
|
||||
/// <summary>
|
||||
/// Форматирует размерности в понятную строку
|
@ -2,7 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Version>2022.1224.0</Version>
|
||||
<Version>2022.1224.1</Version>
|
||||
<Authors>Alexander Babaev</Authors>
|
||||
<Product>ANB Software Components Pack</Product>
|
||||
<Description>Library of some useful functions in C# language.</Description>
|
||||
@ -15,8 +15,8 @@
|
||||
<SignAssembly>True</SignAssembly>
|
||||
<PackageProjectUrl>https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack</PackageProjectUrl>
|
||||
<RepositoryUrl>https://git.babaev-an.ru/babaev-an/anbsoftware_componentspack</RepositoryUrl>
|
||||
<AssemblyVersion>2022.1224.0</AssemblyVersion>
|
||||
<FileVersion>2022.1224.0</FileVersion>
|
||||
<AssemblyVersion>2022.1224.1</AssemblyVersion>
|
||||
<FileVersion>2022.1224.1</FileVersion>
|
||||
<PackageId>ANBSoftware.ComponentsPack</PackageId>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<AnalysisLevel>6.0</AnalysisLevel>
|
||||
|
@ -4,5 +4,7 @@
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=_041F_0430_0440_0441_0435_0440/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0421_043E_0437_0434_0430_0451_043C/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0432_0432_0435_0434_0451_043D/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0438_043C_0451_043D/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0441_0447_0451_0442_0447_0438_043A/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0445_044D_0448_0430/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0445_044D_0448_0435_043C/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
27
demo/CountValueTest.Designer.cs
generated
27
demo/CountValueTest.Designer.cs
generated
@ -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;
|
||||
}
|
@ -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)
|
||||
};
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user