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())
Картинка
Объяснение:
На схеме цикл:
1) у = 2 * х - вычисляет у
2) выводит на экран значения х и у
3) х = х+1 - увеличивает значение х на 1
4) х <= 0 - проверяет условие (если да, то начинается сначала)
x = -5
y = 2 * -5 = -10
x = -5 y = -10
-4 <= 0 (да)
y = 2 * -4 = -8
x = -4 y = -8
-3 <= 0 (да)
y = 2 * -3 = -6
x = -3 y = -6
-2 <= 0 (да)
y = 2 * -2 = -4
x = -2 y = -4
-1 <= 0 (да)
y = 2 * -1 = -2
x = -1 y = -2
0 <= 0 (да)
y = 2 * 0 = 0
x = 0 y = 0
1 <= 0 (нет)
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}; ");
}
}
}
}
Картинка
Объяснение:
На схеме цикл:
1) у = 2 * х - вычисляет у
2) выводит на экран значения х и у
3) х = х+1 - увеличивает значение х на 1
4) х <= 0 - проверяет условие (если да, то начинается сначала)
x = -5
y = 2 * -5 = -10
x = -5 y = -10
-4 <= 0 (да)
y = 2 * -4 = -8
x = -4 y = -8
-3 <= 0 (да)
y = 2 * -3 = -6
x = -3 y = -6
-2 <= 0 (да)
y = 2 * -2 = -4
x = -2 y = -4
-1 <= 0 (да)
y = 2 * -1 = -2
x = -1 y = -2
0 <= 0 (да)
y = 2 * 0 = 0
x = 0 y = 0
1 <= 0 (нет)