using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace laba3.Core
{
///
/// Different utils;
///
internal static class Utils
{
///
/// CLI read double from keyboard;
///
/// Name will be printed before scan
///
public static double ReadDouble(string name)
{
double result;
string inputBuffer;
bool isValid;
do
{
Console.Write($"Введите {name}: ");
inputBuffer = Console.ReadLine();
isValid = double.TryParse(inputBuffer, out result);
if (!isValid)
Console.WriteLine("Неверный тип аргумента!");
} while (!isValid);
return result;
}
///
/// CLI read int from keyboard;
///
/// Name will be printed before scan
///
public static int ReadInt(string name)
{
int result;
string inputBuffer;
bool isValid;
do
{
Console.Write($"Введите {name}: ");
inputBuffer = Console.ReadLine();
isValid = int.TryParse(inputBuffer, out result);
if (!isValid)
Console.WriteLine("Неверный тип аргумента!");
} while (!isValid);
return result;
}
///
/// Util for init arrays with random values, copy it, print it, etc.;
///
public class Arrays
{
private int size;
private int[] array;
///
/// Array size of 10;
/// Filled random values;
///
public Arrays()
{
size = 10;
array = new int[size];
initRandom();
}
///
/// Array size of X;
/// Filled random values;
///
/// Size of array
public Arrays(int size)
{
this.size = size;
array = new int[size];
initRandom();
}
///
///Array size of X;
/// Filled random values;
///
/// Size of array
/// Max value of random
public Arrays(int size, int max)
{
this.size = size;
array = new int[size];
initRandom(max);
}
///
/// Copy array;
///
/// Source from copy
public Arrays (Arrays src)
{
this.size = src.size;
this.array = new int[src.size];
for (int i = 0; i < src.size; i++)
{
this.array[i] = src.array[i];
}
}
///
/// Get array;
///
/// Array pointer
public int[] getArray()
{
return this.array;
}
///
/// Get size;
///
/// Array size
public int getSize()
{
return this.size;
}
///
/// Print array in console;
///
/// Name will be printed
public void print(string name)
{
Console.Write($"Массив {name}: [");
foreach (int x in this.array) { Console.Write($"{x}, "); }
Console.WriteLine("\b\b]");
}
///
/// Sort this array with Gnome Sort;
/// Avg time O(n^2);
///
/// Ellapsed ms
public long gnomeSort()
{
Stopwatch sw = new Stopwatch();
sw.Start();
int i = 1; int j = 2;
while (i < size)
{
if (array[i - 1] < array[i])
{
i = j;
j = j + 1;
}
else
{
(array[i - 1], array[i]) = (array[i], array[i - 1]);
i = i - 1;
if (i == 0)
{
i = j;
j = j + 1;
}
}
}
sw.Stop();
return sw.ElapsedMilliseconds;
}
///
/// Sort this array with Shell Sort;
/// Avg time depends on gap sequence;
///
/// Ellapsed ms
public long shellSort()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int s = size / 2; s > 0; s /= 2)
{
for (int i = s; i < size; ++i)
{
for (int j = i - s; j >= 0 && array[j] > array[j + s]; j -= s)
{
(array[j], array[j + s]) = (array[j + s], array[j]);
}
}
}
sw.Stop();
return sw.ElapsedMilliseconds;
}
private void initRandom()
{
Random rnd = new Random();
for (int i = 0; i < size; i++)
{
array[i] = rnd.Next();
}
}
private void initRandom(int max)
{
Random rnd = new Random();
for (int i = 0; i < size; i++)
{
array[i] = rnd.Next(max);
}
}
}
}
}