You are on page 1of 4

Задания

Цель работы: изучить возможности .NET Framework для


многопоточного программирования.
Постановка задачи.
Создать среду для моделирования полетов самолетов. Отдельный объект
– самолет – обладает такими характеристиками как скорость, высота,
направление полета. Программа полета для каждого самолета храниться в
отдельном текстовом файле. Формат файла следующий – каждая строка
содержит относительное время полета, высоту и точку курса для указанного
времени. Среда моделирования позволяет «запустить» самолет, указанный
пользователем, с соответствующей программой полета. Среда моделирования
также отображает текущие данные запущенных самолетов.
Решение:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace IPR2
{
enum Direction { Left, Up, Down, Right }

class AirPlaneInfo
{
public double Speed { get; set; }
public int Height { get; set; }
public Direction Direction { get; set; }
public int Time { get; set; }
}

class AirPlane
{
public List<AirPlaneInfo> infos;
public int Index { get; set; }
}

class Program
{
static double passedSeconds;

static void Main(string[] args)


{
var startTime = DateTime.Now;

AirPlane plane1 = new AirPlane


{
Index = 1,
infos = new List<AirPlaneInfo>()
{
new AirPlaneInfo {
Height = 1,
Speed = 99,
Time = 5,
Direction = Direction.Right},
new AirPlaneInfo {
Height = 5,
Speed = 20,
Time = 10,
Direction = Direction.Left},
new AirPlaneInfo {
Height = 1000,
Speed = 20,
Time = 20,
Direction = Direction.Left}
}
};

AirPlane plane2 = new AirPlane


{
Index = 5,
infos = new List<AirPlaneInfo>()
{
new AirPlaneInfo {
Height = 888,
Speed = 77,
Time = 3,
Direction = Direction.Left},
new AirPlaneInfo {
Height = 1111,
Speed = 567,
Time = 7,
Direction = Direction.Up},
new AirPlaneInfo {
Height = 1313,
Speed = 1,
Time = 8,
Direction = Direction.Left}
}
};

SaveToFile(plane1);
SaveToFile(plane2);

var savedPlane1 = ReadFromFile(plane1.Index);


var savedPlane5 = ReadFromFile(plane2.Index);

Task.Run(() => ProccessPlane(savedPlane1));


Task.Run(() => ProccessPlane(savedPlane5));

while (true)
{
passedSeconds = (DateTime.Now - startTime).TotalSeconds;

Thread.Sleep(100);
}
}

static void ProccessPlane(AirPlane plane)


{
int curIndex = 0;

while (curIndex < plane.infos.Count)


{
AirPlaneInfo info = plane.infos[curIndex];
Console.WriteLine($"Index:{plane.Index} Height = {info.Height} Speed =
{info.Speed} Direction = {info.Direction}");

Thread.Sleep(100);
if (info.Time < passedSeconds)
{
curIndex++;
}
}
}

public static AirPlane ReadFromFile(int planeIndex)


{
AirPlane plane = new AirPlane
{
Index = planeIndex,
infos = new List<AirPlaneInfo>()
};

try
{
using (var sr = new StreamReader($"plane{planeIndex}.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
var lines = line.Split();

plane.infos.Add(new AirPlaneInfo
{
Height = Convert.ToInt32(lines[0]),
Speed = Convert.ToDouble(lines[1]),
Time = Convert.ToInt32(lines[2]),
Direction = (Direction) Convert.ToInt32(lines[3])
});
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}

return plane;
}

public static void SaveToFile(AirPlane plane)


{
try
{
using (var sw = new StreamWriter($"plane{plane.Index}.txt"))
{

foreach (var info in plane.infos)


{
sw.WriteLine($"{info.Height} {info.Speed} {info.Time}
{(int)info.Direction}");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
}
}

You might also like