Помогите пожалуйста. Нужно добавить ifstream и изменить вектор
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Athlete {
string surname;
int min;
int sec;
int tent;
int hund;
};
double totalTime(const Athlete& athlete) {
return athlete.min * 6000 + athlete.sec * 100 + athlete.tent * 10 + athlete.hund;
}
int main() {
system("chcp 1251 > nul");
const int numAthletes = 10;
Athlete* athletes = new Athlete[numAthletes];
ofstream outfile("athletes.txt");
if (!outfile) {
cout << "Ошибка открытия файла для записи!" << endl;
delete[] athletes;
return 1;
}
for (int i = 0; i < numAthletes; ++i) {
cout << "Введите фамилию спортсмена #" << (i + 1) << ": ";
cin >> athletes[i].surname;
cout << "Введите время (минуты, секунды, десятые, сотые) для " << athletes[i].surname << ": ";
cin >> athletes[i].min >> athletes[i].sec >> athletes[i].tent >> athletes[i].hund;
outfile << athletes[i].surname << " " << athletes[i].min << " " << athletes[i].sec << " " << athletes[i].tent << " " << athletes[i].hund << std::endl;
}
outfile.close();
vector<Athlete> bestAthletes(athletes, athletes + numAthletes);
sort(bestAthletes.begin(), bestAthletes.end(), [](const Athlete& a, const Athlete& b) {
return totalTime(a) < totalTime(b);
});
ofstream bestOutfile("best_athletes.txt");
if (!bestOutfile) {
std::cerr << "Ошибка открытия файла для записи лучших спортсменов!" << std::endl;
delete[] athletes;
return 1;
}
for (int i = 0; i < 6 && i < bestAthletes.size(); ++i) {
bestOutfile << bestAthletes[i].surname << " "
<< bestAthletes[i].min << " "
<< bestAthletes[i].sec << " "
<< bestAthletes[i].tent << " "
<< bestAthletes[i].hund << std::endl;
}
bestOutfile.close();
delete[] athletes;
return 0;
Изменённый код: https://pastebin.com/6xGXhzi3
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Athlete {
string surname;
int min;
int sec;
int tent;
int hund;
};
double totalTime(const Athlete& athlete) {
return athlete.min * 6000 + athlete.sec * 100 + athlete.tent * 10 + athlete.hund;
}
int main() {
system("chcp 1251 > nul");
ifstream infile("athletes.txt");
if (!infile) {
cerr << "Ошибка открытия файла для чтения!" << endl;
return 1;
}
vector<Athlete> athletes;
Athlete temp;
while (infile >> temp.surname >> temp.min >> temp.sec >> temp.tent >> temp.hund) {
athletes.push_back(temp);
}
infile.close();
if (athletes.empty()) {
cerr << "Файл пуст или некорректен!" << endl;
return 1;
}
// Сортируем спортсменов по времени
sort(athletes.begin(), athletes.end(), [](const Athlete& a, const Athlete& b) {
return totalTime(a) < totalTime(b);
});
// Записываем лучших в файл
ofstream bestOutfile("best_athletes.txt");
if (!bestOutfile) {
cerr << "Ошибка открытия файла для записи лучших спортсменов!" << endl;
return 1;
}
for (size_t i = 0; i < 6 && i < athletes.size(); ++i) {
bestOutfile << athletes[i].surname << " "
<< athletes[i].min << " "
<< athletes[i].sec << " "
<< athletes[i].tent << " "
<< athletes[i].hund << endl;
}
bestOutfile.close();
cout << "Данные успешно обработаны!" << endl;
return 0;
}