Advertisement
DasShelmer

11_1_19

Feb 29th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7. struct Student {
  8.     string Name, Surname, Patronymic;
  9.     int YearOfBirth;
  10.     int* Marks;
  11.     const int MarksLen = 5;
  12.  
  13.  
  14.     Student();
  15.  
  16.     Student(ifstream &f) {
  17.         string temp;
  18.         f >> Surname >> Name >> Patronymic >> temp;
  19.         YearOfBirth = stoi(temp);
  20.         Marks = new int[MarksLen];
  21.         for (int i = 0; i < MarksLen; i++) {
  22.             f >> temp;
  23.             Marks[i] = stoi(temp);
  24.         }
  25.     }
  26.  
  27.     string toString() {
  28.         string temp = Surname + " " + Name + " " + Patronymic + " " + to_string(YearOfBirth) + " ";
  29.         for (int i = 0; i < MarksLen; i++) {
  30.             temp += to_string(Marks[i]);
  31.             if (i < MarksLen - 1)
  32.                 temp += " ";
  33.         }
  34.         temp += "\n";
  35.         return temp;
  36.     }
  37.  
  38.     bool cmpByFullName(Student& b) {
  39.  
  40.         int temp = this->Surname.compare(b.Surname);
  41.         if (!temp) {
  42.             temp = this->Name.compare(b.Name);
  43.  
  44.             if (!temp) {
  45.                 temp = this->Patronymic.compare(b.Patronymic);
  46.             }
  47.         }
  48.         return temp;
  49.     }
  50.  
  51.     bool operator<(Student& s) {
  52.         return cmpByFullName(s);
  53.     }
  54. };
  55.  
  56.  
  57. template <class T>
  58. void shellSort(T** arr, int n)
  59. {
  60.     // Начинаем с наибольшего шага
  61.     for (int gap = n / 2; gap > 0; gap /= 2)
  62.     {
  63.         // Перечисление элементов, которые сортируются на определённом шаге
  64.         for (int i = gap; i < n; i += 1)
  65.         {
  66.             T *temp = arr[i];
  67.             int j;
  68.             // Сортировка вставками
  69.             for (j = i; j >= gap && *arr[j - gap] < *temp; j -= gap)
  70.                 arr[j] = arr[j - gap];
  71.  
  72.             arr[j] = temp;
  73.         }
  74.     }
  75. }
  76.  
  77. int main() {
  78.    
  79.     ifstream in("input.txt");
  80.     if (!in) return 0; // Файл пуст
  81.  
  82.     int studentsLen = 1 + count(std::istreambuf_iterator<char>(in),
  83.         std::istreambuf_iterator<char>(), '\n');
  84.     in.seekg(0);
  85.  
  86.     // Основной массив
  87.     Student** students = new Student*[studentsLen];
  88.  
  89.     // Заполнение массива из файла
  90.     for (int i = 0; i < studentsLen; i++)
  91.         students[i] = new Student(in);
  92.    
  93.     in.close();
  94.     shellSort(students, studentsLen);// Сортировка
  95.  
  96.     ofstream out("output.txt");
  97.     for (int i = 0; i < studentsLen; i++) {
  98.         out << students[i]->toString();
  99.         cout << students[i]->toString();
  100.     }
  101.     out.close();
  102.     return 0;
  103. }
  104.  
  105.  
  106. /* input.txt
  107. Ivanov Vasiliy Ivanovich 2000 8 7 5 4 0
  108. Gridin Kyzma Vladimirovich 3000 4 6 2 4 5
  109. Arusenyan Michail Alekseevich 9999 6 7 2 4 1
  110. Prusikin Dobrinya Iliich 105 6 7 8 3 2
  111. Safonov Mark Stepanovich 2020 9 9 9 9 9
  112. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement