using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Proj
{
class Program
static void Main(string[] args)
WorkWithFiles();
Console.ReadKey();
}
private static void WorkWithFiles()
string firstPath = Console.ReadLine(),
secondPath = Console.ReadLine(),
resultPath = Console.ReadLine();
List<int> resultArray = new List<int>();
resultArray.AddRange(FindNumbersInFile(firstPath, x => x % 2 == 0));
resultArray.AddRange(FindNumbersInFile(secondPath, x => x % 2 != 0));
PrintResult(resultArray, resultPath);
private static List<int> FindNumbersInFile(string PATH, Predicate<int> predicate)
if (File.Exists(PATH))
var firstFileNumbers = File.ReadAllText(PATH).Split(' ').Select(int.Parse).ToList();
resultArray.AddRange(firstFileNumbers.Where(x => predicate(x)));
else
throw new Exception($"Incorrect file path: {PATH}");
return resultArray;
private static void PrintResult(List<int> resultList, string PATH)
using (StreamWriter sw = File.CreateText(PATH))
sw.Write("Even: ");
foreach (var n in resultList.Where(x => x % 2 == 0).ToList())
sw.Write($"{n}; ");
sw.WriteLine();
sw.Write("\nOdd: ");
foreach (var n in resultList.Where(x => x % 2 != 0).ToList())
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Proj
{
class Program
{
static void Main(string[] args)
{
WorkWithFiles();
Console.ReadKey();
}
private static void WorkWithFiles()
{
string firstPath = Console.ReadLine(),
secondPath = Console.ReadLine(),
resultPath = Console.ReadLine();
List<int> resultArray = new List<int>();
resultArray.AddRange(FindNumbersInFile(firstPath, x => x % 2 == 0));
resultArray.AddRange(FindNumbersInFile(secondPath, x => x % 2 != 0));
PrintResult(resultArray, resultPath);
}
private static List<int> FindNumbersInFile(string PATH, Predicate<int> predicate)
{
List<int> resultArray = new List<int>();
if (File.Exists(PATH))
{
var firstFileNumbers = File.ReadAllText(PATH).Split(' ').Select(int.Parse).ToList();
resultArray.AddRange(firstFileNumbers.Where(x => predicate(x)));
}
else
{
throw new Exception($"Incorrect file path: {PATH}");
}
return resultArray;
}
private static void PrintResult(List<int> resultList, string PATH)
{
using (StreamWriter sw = File.CreateText(PATH))
{
sw.Write("Even: ");
foreach (var n in resultList.Where(x => x % 2 == 0).ToList())
{
sw.Write($"{n}; ");
}
sw.WriteLine();
sw.Write("\nOdd: ");
foreach (var n in resultList.Where(x => x % 2 != 0).ToList())
{
sw.Write($"{n}; ");
}
}
}
}