• Предмет: Информатика
  • Автор: AliyaLissYouTube
  • Вопрос задан 7 лет назад

Написать программу, которая в файле находит самого высокого человека и выводит его данные.

Ответы

Ответ дал: ЯковПервый
0

//Поскольку вы не указали структуру файла и язык программирования, то подберу их сам.

//ЯП: C#

//Структура: рост/имя/вес/страна проживания

Код:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace Prog

{

class Program

{

static void Main(string[] args)

{

string location = "C://Test//Persons.txt";

try

{

int size = File.ReadLines(location).Count();

if (size > 0)

{

List<string> person = new List<string>();

string[] tallestPerson = new string[4];

int maxHeight = -1;

int height = 0;

int index = 0;

StreamReader PersonsReader = new StreamReader(location, Encoding.Default);

for (int i = 0; i < size; i++)

{

person.Add(PersonsReader.ReadLine());

height = Convert.ToInt32(person[i].Remove(person[i].IndexOf("/")));

if (height > maxHeight)

{

maxHeight = height;

index = i;

}

}

string tmp = person[index];

int paramIndex = 0;

for (int j = 0; j < tmp.Length; j++)

{

if (tmp[j] != '/')

tallestPerson[paramIndex] += tmp[j];

else

paramIndex++;

}

Console.WriteLine("Самый высокий человек: " + tallestPerson[1]);

Console.WriteLine("Рост: " + tallestPerson[0] + " см");

Console.WriteLine("Вес: " + tallestPerson[2] + " кг");

Console.WriteLine("Страна проживания: " + tallestPerson[3]);

}

else

{

Console.WriteLine("Файл пустой!");

}

}

catch (Exception)

{

Console.WriteLine("Ошибка! Файл не нейден, либо нарушена его структура!");

}

finally

{

Console.ReadKey();

}

}

}

}

Приложения:
Вас заинтересует