upload
This commit is contained in:
18
Services/ArrayIoService.cs
Normal file
18
Services/ArrayIoService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Lab9.Services;
|
||||
|
||||
public static class ArrayIoService
|
||||
{
|
||||
private static readonly char[] Separators = [' ', ',', ';', '\n', '\r', '\t'];
|
||||
|
||||
public static int[] ReadArrayFromFile(string filePath)
|
||||
{
|
||||
var content = File.ReadAllText(filePath);
|
||||
|
||||
return content
|
||||
.Split(Separators, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(int.Parse)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
25
Services/SortingContext.cs
Normal file
25
Services/SortingContext.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Lab9.Models;
|
||||
using Lab9.Strategies;
|
||||
|
||||
namespace Lab9.Services;
|
||||
|
||||
public class SortingContext
|
||||
{
|
||||
public SortingContext()
|
||||
{
|
||||
}
|
||||
|
||||
public SortingContext(ISortStrategy strategy)
|
||||
{
|
||||
ContextStrategy = strategy;
|
||||
}
|
||||
|
||||
public ISortStrategy? ContextStrategy { get; set; }
|
||||
|
||||
public SortResult Execute(int[] array, bool ascending, bool enableLogging)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(ContextStrategy);
|
||||
|
||||
return ContextStrategy.Sort(array, ascending, enableLogging);
|
||||
}
|
||||
}
|
||||
40
Services/StepLogger.cs
Normal file
40
Services/StepLogger.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Lab9.Services;
|
||||
|
||||
public class StepLogger
|
||||
{
|
||||
private readonly StringBuilder _builder = new();
|
||||
|
||||
public void Header(string text)
|
||||
{
|
||||
_builder.AppendLine(text);
|
||||
}
|
||||
|
||||
public void BlankLine()
|
||||
{
|
||||
_builder.AppendLine();
|
||||
}
|
||||
|
||||
public void Line(string text)
|
||||
{
|
||||
_builder.AppendLine(text);
|
||||
}
|
||||
|
||||
public void ArrayState(int[] array, params int[] highlightedIndices)
|
||||
{
|
||||
var highlighted = highlightedIndices.Length == 0
|
||||
? new HashSet<int>()
|
||||
: highlightedIndices.ToHashSet();
|
||||
|
||||
var parts = new string[array.Length];
|
||||
for (var i = 0; i < array.Length; i++)
|
||||
{
|
||||
parts[i] = highlighted.Contains(i) ? $"[{array[i]}]" : array[i].ToString();
|
||||
}
|
||||
|
||||
_builder.AppendLine(string.Join(" ", parts));
|
||||
}
|
||||
|
||||
public override string ToString() => _builder.ToString();
|
||||
}
|
||||
Reference in New Issue
Block a user