227 lines
6.1 KiB
C#
227 lines
6.1 KiB
C#
using Sortering;
|
|
using System.Diagnostics;
|
|
using System.Text.RegularExpressions;
|
|
|
|
internal class Program
|
|
{
|
|
private static int[] _ints;
|
|
private static bool _running = true;
|
|
private static Random _random = new Random();
|
|
|
|
private static Dictionary<string, Action<string[]>> Commands =
|
|
new Dictionary<string, Action<string[]>>(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
{ "insertionsort", SortInsertion },
|
|
{ "bubblesort", SortBubble },
|
|
{ "mkarray", MkArray },
|
|
{ "print", Print },
|
|
{ "sequentialfind", SequentialFind },
|
|
{ "binaryfind", BinaryFind },
|
|
{ "exit", _ => _running = false }
|
|
};
|
|
|
|
private static void Main(string[] args)
|
|
{
|
|
while (_running)
|
|
{
|
|
Console.Write("> ");
|
|
var input = ReadLineWithAutocomplete();
|
|
if (string.IsNullOrWhiteSpace(input)) continue;
|
|
|
|
var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
var cmd = parts[0];
|
|
var cmdArgs = parts.Length > 1 ? parts[1..] : Array.Empty<string>();
|
|
|
|
if (Commands.TryGetValue(cmd, out var action))
|
|
action(cmdArgs);
|
|
else
|
|
{
|
|
Console.WriteLine("Unknown command. Available:");
|
|
foreach (var c in Commands.Keys)
|
|
Console.WriteLine(" - " + c);
|
|
}
|
|
}
|
|
}
|
|
|
|
// autocompletion
|
|
|
|
private static string ReadLineWithAutocomplete()
|
|
{
|
|
var buffer = new List<char>();
|
|
int cursor = 0;
|
|
|
|
while (true)
|
|
{
|
|
var key = Console.ReadKey(intercept: true);
|
|
|
|
if (key.Key == ConsoleKey.Enter)
|
|
{
|
|
Console.WriteLine();
|
|
return new string(buffer.ToArray());
|
|
}
|
|
|
|
if (key.Key == ConsoleKey.Backspace && cursor > 0)
|
|
{
|
|
buffer.RemoveAt(cursor - 1);
|
|
cursor--;
|
|
RedrawLine(buffer, cursor);
|
|
continue;
|
|
}
|
|
|
|
if (key.Key == ConsoleKey.Tab)
|
|
{
|
|
Autocomplete(buffer, ref cursor);
|
|
continue;
|
|
}
|
|
|
|
if (!char.IsControl(key.KeyChar))
|
|
{
|
|
buffer.Insert(cursor, key.KeyChar);
|
|
cursor++;
|
|
RedrawLine(buffer, cursor);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void Autocomplete(List<char> buffer, ref int cursor)
|
|
{
|
|
var text = new string(buffer.ToArray());
|
|
var parts = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
// Only autocomplete the command (first word)
|
|
if (parts.Length > 1)
|
|
return;
|
|
|
|
string prefix = parts.Length == 0 ? "" : parts[0];
|
|
|
|
var matches = Commands.Keys
|
|
.Where(c => c.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
|
.ToList();
|
|
|
|
if (matches.Count == 1)
|
|
{
|
|
buffer.Clear();
|
|
buffer.AddRange(matches[0]);
|
|
cursor = buffer.Count;
|
|
RedrawLine(buffer, cursor);
|
|
}
|
|
}
|
|
|
|
private static void RedrawLine(List<char> buffer, int cursor)
|
|
{
|
|
Console.Write("\r> ");
|
|
Console.Write(new string(buffer.ToArray()));
|
|
Console.Write(" ");
|
|
Console.SetCursorPosition(2 + cursor, Console.CursorTop);
|
|
}
|
|
|
|
// commands
|
|
|
|
private static void SortInsertion(string[] args)
|
|
{
|
|
if (_ints == null)
|
|
{
|
|
Console.WriteLine("No array. Use mkarray first.");
|
|
return;
|
|
}
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
Sort.InsertionSort(_ints);
|
|
sw.Stop();
|
|
|
|
Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms");
|
|
}
|
|
|
|
private static void SortBubble(string[] args)
|
|
{
|
|
if (_ints == null)
|
|
{
|
|
Console.WriteLine("No array. Use mkarray first.");
|
|
return;
|
|
}
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
Sort.BubbleSort(_ints);
|
|
sw.Stop();
|
|
|
|
Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms");
|
|
}
|
|
|
|
private static void SequentialFind(string[] args)
|
|
{
|
|
if (_ints == null)
|
|
{
|
|
Console.WriteLine("No array. Use mkarray first.");
|
|
return;
|
|
}
|
|
|
|
if (args.Length < 1 || !int.TryParse(args[0], out int value))
|
|
{
|
|
Console.WriteLine("Usage: find <value>");
|
|
return;
|
|
}
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
int match = Sort.SequentialFind(_ints, value);
|
|
|
|
sw.Stop();
|
|
|
|
if (match == -1)
|
|
Console.WriteLine($"Found no match in {sw.ElapsedMilliseconds} ms");
|
|
else
|
|
Console.WriteLine($"Found match at {match} in {sw.ElapsedMilliseconds} ms");
|
|
}
|
|
|
|
private static void BinaryFind(string[] args)
|
|
{
|
|
if (_ints == null)
|
|
{
|
|
Console.WriteLine("No array. Use mkarray first.");
|
|
return;
|
|
}
|
|
|
|
if (args.Length < 1 || !int.TryParse(args[0], out int value))
|
|
{
|
|
Console.WriteLine("Usage: find <value>");
|
|
return;
|
|
}
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
int match = Sort.BinaryFind(_ints, value);
|
|
|
|
sw.Stop();
|
|
|
|
if (match == -1)
|
|
Console.WriteLine($"Found no match in {sw.ElapsedMilliseconds} ms");
|
|
else
|
|
Console.WriteLine($"Found match at {match} in {sw.ElapsedMilliseconds} ms");
|
|
}
|
|
|
|
private static void Print(string[] args)
|
|
{
|
|
if (_ints == null)
|
|
{
|
|
Console.WriteLine("No array.");
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < _ints.Length; i++)
|
|
Console.WriteLine(_ints[i]);
|
|
}
|
|
|
|
private static void MkArray(string[] args)
|
|
{
|
|
if (args.Length < 1 || !int.TryParse(args[0], out int count) || count <= 0)
|
|
{
|
|
Console.WriteLine("Usage: mkarray <size>");
|
|
return;
|
|
}
|
|
|
|
_ints = new int[count];
|
|
for (int i = 0; i < count; i++)
|
|
_ints[i] = _random.Next(1000 + (int)(count * 0.1));
|
|
|
|
Console.WriteLine($"Created array with {count} elements");
|
|
}
|
|
}
|