35657

Untitled

Sep 28th, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <Windows.h>
  6. #include <fstream>
  7.  
  8.  
  9. template <typename container>
  10. void print(const container& cont) {
  11.     for (const auto& a : cont) {
  12.         std::cout << a;
  13.     }
  14.     std::cout << std::endl;
  15. }
  16.  
  17. struct student {
  18.     std::string name;
  19.     std::string surname;
  20.     int course;
  21.     int rating;
  22.     double average_score;
  23. };
  24.  
  25. std::ostream& operator<<(std::ostream& output, const student& student) {
  26.     return output << student.name << " " << student.surname << " " << student.course << " " << student.rating << " " << student.average_score << std::endl;
  27. }
  28.  
  29. std::istream& operator>>(std::istream& input, student& student) {
  30.     return input >> student.name >> student.surname >> student.course >> student.rating >> student.average_score;
  31. }
  32.  
  33. int main() {
  34.  
  35.     SetConsoleCP(1251);
  36.     SetConsoleOutputCP(1251);
  37.  
  38.     std::vector<student> students;
  39.  
  40.     std::ifstream fin("students.txt");
  41.     std::ofstream fout("students2.txt");
  42.  
  43.     if (!fin.is_open() || !fout.is_open()) {
  44.         std::cout << "Ошибка открытия файла" << std::endl;
  45.     }
  46.     else {
  47.  
  48.         student temp;
  49.         fin >> temp;
  50.         while (!fin.eof()) {
  51.             students.push_back(temp);
  52.             fin >> temp;
  53.         }
  54.  
  55.         print(students);
  56.  
  57.         std::sort(students.begin(), students.end(), [](const student & a, const student & b) {return a.surname < b.surname; });
  58.  
  59.         print(students);
  60.  
  61.         std::sort(students.begin(), students.end(), [](const student & a, const student & b) {return a.rating > b.rating; });
  62.  
  63.         print(students);
  64.  
  65.         std::sort(students.begin(), students.end(), [](const student & a, const student & b) {return a.average_score > b.average_score; });
  66.  
  67.         print(students);
  68.  
  69.         auto it = std::remove_if(students.begin(), students.end(), [](const student & a) {return a.course == 5; });
  70.  
  71.         students.erase(it, students.end());
  72.  
  73.         //students.erase(std::remove_if(students.begin(), students.end(), [](const student & a) {return a.course == 5; }), students.end());
  74.  
  75.         print(students);
  76.  
  77.         std::for_each(students.begin(), students.end(), [](student & a) {a.course++; });
  78.  
  79.         print(students);
  80.  
  81.         it = std::max_element(students.begin(), students.end(), [](const student & a, const student & b) {return a.rating < b.rating; });
  82.  
  83.         std::cout << *it << std::endl;
  84.  
  85.         for (const auto& a : students) {
  86.             fout << a;
  87.         }
  88.         fin.close();
  89.         fout.close();
  90.         std::remove("students.txt");
  91.         std::rename("students2.txt", "students.txt");
  92.     }
  93.  
  94.    
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment