Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
- cout << "Ввод данных спортсменов:" << endl;
- 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 << "\n";
- }
- outfile.close();
- // Читаем данные из файла с помощью ifstream и заполняем вектор
- ifstream infile("athletes.txt");
- if (!infile) {
- cout << "Ошибка открытия файла для чтения!" << endl;
- delete[] athletes;
- return 1;
- }
- vector<Athlete> athletesVector;
- Athlete temp;
- while (infile >> temp.surname >> temp.min >> temp.sec >> temp.tent >> temp.hund) {
- athletesVector.push_back(temp);
- }
- infile.close();
- // Сортируем вектор по возрастанию общего времени
- sort(athletesVector.begin(), athletesVector.end(), [](const Athlete& a, const Athlete& b) {
- return totalTime(a) < totalTime(b);
- });
- // Записываем 6 лучших спортсменов в отдельный файл
- ofstream bestOutfile("best_athletes.txt");
- if (!bestOutfile) {
- cerr << "Ошибка открытия файла для записи лучших спортсменов!" << endl;
- delete[] athletes;
- return 1;
- }
- for (size_t i = 0; i < 6 && i < athletesVector.size(); ++i) {
- bestOutfile << athletesVector[i].surname << " "
- << athletesVector[i].min << " "
- << athletesVector[i].sec << " "
- << athletesVector[i].tent << " "
- << athletesVector[i].hund << "\n";
- }
- bestOutfile.close();
- delete[] athletes;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement