1
0
Files
2026-02-12 01:22:45 +03:00

46 lines
1.5 KiB
C#
Raw Permalink 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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace Lab
{
internal class Parsers
{
public static bool TryParseDouble(string? s, out double val) => double.TryParse((s ?? "").Trim().Replace(",", "."), NumberStyles.Float, CultureInfo.InvariantCulture, out val) && !double.IsNaN(val) && !double.IsInfinity(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', ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
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;
}
}
}