45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Lab
|
||
{
|
||
internal class Parsers
|
||
{
|
||
public static bool TryParseDouble(string? s, out double val) => double.TryParse((s ?? "").Trim().Replace(",", "."), out val);
|
||
public static bool TryParseInt(string? s, out int val) => int.TryParse((s ?? "").Trim(), out val);
|
||
|
||
public static bool TryParseDoubleArray(string? s, out double[] vals, out string err)
|
||
{
|
||
vals = Array.Empty<double>();
|
||
err = "";
|
||
|
||
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(s)) {
|
||
err = "Пустой ввод. Введите числа, разделяя их пробелами/запятыми/точкой с запятой.";
|
||
return false;
|
||
}
|
||
|
||
string[] parts = s.Split(new[] { ' ', '\t', '\n', '\r', ',', ';' });
|
||
|
||
List<double> list = new List<double>();
|
||
|
||
for (int i = 0; i < parts.Length; i++)
|
||
{
|
||
if (!TryParseDouble(parts[i], out double val))
|
||
{
|
||
err = $"Вронгаешь элемент! [ #{i + 1}: «{parts[i]}» ]";
|
||
return false;
|
||
}
|
||
|
||
list.Add(val);
|
||
}
|
||
|
||
vals = list.ToArray();
|
||
|
||
return true;
|
||
}
|
||
}
|
||
}
|