distributed-computing/tasks/mikhailov-ys/lab_2/worker-2/Program.cs
2024-01-06 23:35:43 +04:00

34 lines
1.0 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.IO;
using System.Text;
string[] numbers;
using (FileStream fstream = File.OpenRead("/var/result/data.txt"))
{
byte[] buffer = new byte[fstream.Length];
await fstream.ReadAsync(buffer, 0, buffer.Length);
string text = Encoding.Default.GetString(buffer);
text = text.Replace("\r\n", ";");
numbers = text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
}
int maxNumber = int.MinValue;
foreach (var number in numbers)
{
int currentNumber = Convert.ToInt32(number);
if (currentNumber > maxNumber)
{
maxNumber = currentNumber;
}
}
int maxNumberSquared = maxNumber * maxNumber;
using (FileStream fstream = new FileStream("/var/result/result.txt", FileMode.OpenOrCreate))
{
byte[] buffer = Encoding.Default.GetBytes(maxNumberSquared.ToString());
await fstream.WriteAsync(buffer, 0, buffer.Length);
}
Console.WriteLine("Мax число из файла: " + maxNumber);
Console.WriteLine("Вторая степень Max числа: " + maxNumberSquared);