static void Main(string[] args) { string path = @"..\..\..\..\Task1\bin\Debug\netcoreapp3.1\out.txt"; try { using (StreamReader sr = new StreamReader(new FileStream(path, FileMode.Open))) { while (true) { string s = sr.ReadLine(); if (s == null) { break; } Street.Street street = StreetParse(s.Split(' ')); if ((!street == true) && (~street % 2 == 1)) { Console.WriteLine(street.ToString()); } } } } catch (Exception e) { Console.WriteLine($"Error happend ({e.Message})"); } }
static void Main(string[] args) { Street.Street[] streets; List <Street.Street> s = new List <Street.Street>(); int n = 0; do { Console.WriteLine("Введите n"); int.TryParse(Console.ReadLine(), out n); } while (n < 1); bool flag = true; using (StreamReader sr = new StreamReader(new FileStream("data.txt", FileMode.Open))) { while (true) { string input = sr.ReadLine(); if (input == null) { break; } if (!Check(input.Split(' '), s)) { flag = false; break; } } } if (!flag) { Console.WriteLine("Некоррректные данные в файле(создаем новый массив)"); streets = CreateArray(n); } else { if (s.Count >= n) { streets = new Street.Street[n]; for (int i = 0; i < n; i++) { streets[i] = s[i]; } } else { Console.WriteLine("Недостаточно строк в файле(создаем новый массив)"); streets = CreateArray(s.Count); } } using (StreamWriter sw = new StreamWriter(new FileStream("out.txt", FileMode.Create))) { foreach (Street.Street i in streets) { sw.WriteLine(i.ToString()); } } }
static Street.Street[] CreateArray(int n) { Random r = new Random(); Street.Street[] streets = new Street.Street[n]; for (int i = 0; i < n; i++) { int[] nums = new int[r.Next(1, 10)]; for (int j = 0; j < nums.Length; j++) { nums[j] = r.Next(1, 100); } string name = ""; for (int j = 0; j < r.Next(5, 10); j++) { name += (char)r.Next('a', 'z' + 1); } streets[i] = new Street.Street(name, nums); } return(streets); }