Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- struct Student {
- string Name, Surname, Patronymic;
- int YearOfBirth;
- int* Marks;
- const int MarksLen = 5;
- Student();
- Student(ifstream &f) {
- string temp;
- f >> Surname >> Name >> Patronymic >> temp;
- YearOfBirth = stoi(temp);
- Marks = new int[MarksLen];
- for (int i = 0; i < MarksLen; i++) {
- f >> temp;
- Marks[i] = stoi(temp);
- }
- }
- string toString() {
- string temp = Surname + " " + Name + " " + Patronymic + " " + to_string(YearOfBirth) + " ";
- for (int i = 0; i < MarksLen; i++) {
- temp += to_string(Marks[i]);
- if (i < MarksLen - 1)
- temp += " ";
- }
- temp += "\n";
- return temp;
- }
- bool cmpByFullName(Student& b) {
- int temp = this->Surname.compare(b.Surname);
- if (!temp) {
- temp = this->Name.compare(b.Name);
- if (!temp) {
- temp = this->Patronymic.compare(b.Patronymic);
- }
- }
- return temp;
- }
- bool operator<(Student& s) {
- return cmpByFullName(s);
- }
- };
- template <class T>
- void shellSort(T** arr, int n)
- {
- // Начинаем с наибольшего шага
- for (int gap = n / 2; gap > 0; gap /= 2)
- {
- // Перечисление элементов, которые сортируются на определённом шаге
- for (int i = gap; i < n; i += 1)
- {
- T *temp = arr[i];
- int j;
- // Сортировка вставками
- for (j = i; j >= gap && *arr[j - gap] < *temp; j -= gap)
- arr[j] = arr[j - gap];
- arr[j] = temp;
- }
- }
- }
- int main() {
- ifstream in("input.txt");
- if (!in) return 0; // Файл пуст
- int studentsLen = 1 + count(std::istreambuf_iterator<char>(in),
- std::istreambuf_iterator<char>(), '\n');
- in.seekg(0);
- // Основной массив
- Student** students = new Student*[studentsLen];
- // Заполнение массива из файла
- for (int i = 0; i < studentsLen; i++)
- students[i] = new Student(in);
- in.close();
- shellSort(students, studentsLen);// Сортировка
- ofstream out("output.txt");
- for (int i = 0; i < studentsLen; i++) {
- out << students[i]->toString();
- cout << students[i]->toString();
- }
- out.close();
- return 0;
- }
- /* input.txt
- Ivanov Vasiliy Ivanovich 2000 8 7 5 4 0
- Gridin Kyzma Vladimirovich 3000 4 6 2 4 5
- Arusenyan Michail Alekseevich 9999 6 7 2 4 1
- Prusikin Dobrinya Iliich 105 6 7 8 3 2
- Safonov Mark Stepanovich 2020 9 9 9 9 9
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement