35657

Untitled

May 28th, 2024
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <fstream>
  5.  
  6. template<typename container>
  7. void print(container cont) {
  8.     for (const auto& a : cont) {
  9.         std::cout << a;
  10.     }
  11.     std::cout << std::endl;
  12. }
  13.  
  14. struct student {
  15.     std::string name;
  16.     std::string surname;
  17.     int course; // от 1 до 5
  18.     int rating; // до 100
  19.     double average_score; // до 5
  20. };
  21.  
  22.  
  23. std::ostream& operator<<(std::ostream& output, const student& st) {
  24.     output << st.name << " " << st.surname << " " << st.course << " " << st.rating << " " << st.average_score << std::endl;
  25.     return output;
  26. }
  27.  
  28. std::istream& operator>>(std::istream& input, student& st) {
  29.     input >> st.name >> st.surname >> st.course >> st.rating >> st.average_score;
  30.     return input;
  31. }
  32.  
  33. int main() {
  34.  
  35.     setlocale(LC_ALL, "ru");
  36.  
  37.     //std::vector<student> students{ {"Иван", "Иванов", 1, 33, 3.7}, {"Владимир", "Михайлов", 3, 39, 3.9}, {"Геннадий", "Петров", 2, 56, 4.2}, {"Ольга", "Григорьева", 5, 48, 4.0}, {"Елена", "Гришина", 4, 75, 4.9} };
  38.  
  39.  
  40.     std::ifstream fin("students.txt");
  41.  
  42.     std::ofstream fout("temp.txt");
  43.  
  44.     if (!fin.is_open() || !fout.is_open()) {
  45.         std::cout << "Ошибка открытия файла";
  46.     }
  47.     else {
  48.  
  49.         std::vector<student> students;
  50.  
  51.         student temp;
  52.  
  53.         while (!fin.eof()) {
  54.             fin >> temp;
  55.             students.push_back(temp);
  56.         }
  57.  
  58.         print(students);
  59.  
  60.         std::sort(students.begin(), students.end(), [](const student& left, const student& right) { return left.surname < right.surname; });
  61.  
  62.         print(students);
  63.  
  64.         std::sort(students.begin(), students.end(), [](const student& left, const student& right) { return left.rating > right.rating; });
  65.  
  66.         print(students);
  67.  
  68.         std::sort(students.begin(), students.end(), [](const student& left, const student& right) { return left.average_score > right.average_score; });
  69.  
  70.         print(students);
  71.  
  72.         for (auto a : students) {
  73.             fout << a;
  74.         }
  75.  
  76.         fin.close();
  77.         fout.close();
  78.  
  79.         std::remove("students.txt");
  80.         std::rename("temp.txt", "students.txt");
  81.     }
  82.  
  83.    
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment