246 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace laba3.Core
{
/// <summary>
/// Different utils;
/// </summary>
internal static class Utils
{
/// <summary>
/// CLI read double from keyboard;
/// </summary>
/// <param name="name">Name will be printed before scan</param>
/// <returns></returns>
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;
}
/// <summary>
/// CLI read int from keyboard;
/// </summary>
/// <param name="name">Name will be printed before scan</param>
/// <returns></returns>
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;
}
/// <summary>
/// Util for init arrays with random values, copy it, print it, etc.;
/// </summary>
public class Arrays
{
private int size;
private int[] array;
/// <summary>
/// Array size of 10;
/// Filled random values;
/// </summary>
public Arrays()
{
size = 10;
array = new int[size];
initRandom();
}
/// <summary>
/// Array size of X;
/// Filled random values;
/// </summary>
/// <param name="size">Size of array</param>
public Arrays(int size)
{
this.size = size;
array = new int[size];
initRandom();
}
/// <summary>
///Array size of X;
/// Filled random values;
/// </summary>
/// <param name="size">Size of array</param>
/// <param name="max">Max value of random</param>
public Arrays(int size, int max)
{
this.size = size;
array = new int[size];
initRandom(max);
}
/// <summary>
/// Copy array;
/// </summary>
/// <param name="src">Source from copy</param>
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];
}
}
/// <summary>
/// Get array;
/// </summary>
/// <returns>Array pointer</returns>
public int[] getArray()
{
return this.array;
}
/// <summary>
/// Get size;
/// </summary>
/// <returns>Array size</returns>
public int getSize()
{
return this.size;
}
/// <summary>
/// Print array in console;
/// </summary>
/// <param name="name">Name will be printed</param>
public void print(string name)
{
Console.Write($"Массив {name}: [");
foreach (int x in this.array) { Console.Write($"{x}, "); }
Console.WriteLine("\b\b]");
}
public override string ToString()
{
string str = "[";
foreach (int x in this.array) { str+=$"{x}, "; }
str = str.TrimEnd(' ').TrimEnd(',');
str += "]";
return str;
}
/// <summary>
/// Sort this array with Gnome Sort;
/// Avg time O(n^2);
/// </summary>
/// <returns>Ellapsed ms</returns>
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;
}
/// <summary>
/// Sort this array with Shell Sort;
/// Avg time depends on gap sequence;
/// </summary>
/// <returns>Ellapsed ms</returns>
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);
}
}
}
public static void OpenInWeb(string url)
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = url, // Замените на свой сайт
UseShellExecute = true // Это указывает, что нужно использовать ассоциации с оболочкой (браузер)
});
}
catch (Exception ex)
{
MessageBox.Show("Ошибка при открытии браузера: " + ex.Message);
}
}
}
}