Advertisement
Toliak

lab6_1

Nov 1st, 2018
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <list>
  5.  
  6. struct Student
  7. {
  8.     std::string name;
  9.     std::string group;
  10.     unsigned short book;
  11.     unsigned short marks[4];
  12. };
  13.  
  14. struct ListNode
  15. {
  16.     Student s;                          // Значение
  17.     ListNode *prev = nullptr;           // Линковка
  18.     ListNode *next = nullptr;
  19. };
  20.  
  21. // Меняет местами значения вершин
  22. void swap(ListNode &l, ListNode &r)
  23. {
  24.     // Избегаем излешнее копирование
  25.     Student s = std::move(l.s);
  26.     l.s = std::move(r.s);
  27.     r.s = std::move(s);
  28. }
  29.  
  30. // Сортировка (прямой выбор)
  31. void sort(ListNode *start)
  32. {
  33.     // Своеобразный цикл для обхода списка
  34.     for (ListNode *current = start; current != nullptr; current = current->next) {
  35.         ListNode *minimum = current;                    // Минимум - текущий элемент
  36.         for (ListNode *inside = current; inside != nullptr; inside = inside->next) {
  37.             if (inside->s.name < minimum->s.name) {
  38.                 minimum = inside;                       // Меньше минимума? Меняем
  39.             }
  40.         }
  41.  
  42.         if (minimum != current) {                       // Менять элемент на себя бессмысленно
  43.             swap(*minimum, *current);
  44.         }
  45.     }
  46. }
  47.  
  48. int main()
  49. {
  50.     ListNode *currentNode = nullptr;            // Текущая вершина
  51.  
  52.     size_t n;                                   // Размер списка
  53.     std::cout << "Enter amount of students: ";
  54.     std::cin >> n;
  55.     for (size_t i = 0; i < n; i++) {            // Обход
  56.         if (currentNode == nullptr) {           // Если нет вершины
  57.             currentNode = new ListNode;         // Создаем
  58.         } else {
  59.             auto newNode = new ListNode;        // Создаем новую вершину
  60.             currentNode->next = newNode;        // Линковка
  61.             newNode->prev = currentNode;
  62.             currentNode = newNode;
  63.         }
  64.         Student &s = currentNode->s;            // Ссылка на текущ студента
  65.  
  66.         std::cin.ignore();                      // Костыль
  67.         std::cout << "[S " << std::setw(3) << i << "] Name  : ";
  68.         std::getline(std::cin, s.name);         // Ввод
  69.         std::cout << "[S " << std::setw(3) << i << "] Group : ";
  70.         std::getline(std::cin, s.group);
  71.         std::cout << "[S " << std::setw(3) << i << "] Book  : ";
  72.         std::cin >> s.book;
  73.         for (size_t j = 0; j < 4; j++) {
  74.             std::cout << "[S " << std::setw(3) << i
  75.                       << "] Mark(" << j << "): ";
  76.             std::cin >> s.marks[j];
  77.         }
  78.     }
  79.  
  80.     // Отходим назад
  81.     while (currentNode && currentNode->prev) {
  82.         currentNode = currentNode->prev;
  83.     }
  84.  
  85.     sort(currentNode);      // Сортируем
  86.     // Своеобразный цикл для обхода списка
  87.     for (ListNode *current = currentNode; current != nullptr; current = current->next) {
  88.         static size_t counter = 0;    // Счетчик
  89.         std::cout << "[S " << std::setw(3) << counter
  90.                   << "] Name  : " << current->s.name
  91.                   << std::endl;
  92.         std::cout << "[S " << std::setw(3) << counter
  93.                   << "] Group : " << current->s.group
  94.                   << std::endl;
  95.         std::cout << "[S " << std::setw(3) << counter
  96.                   << "] Book  : " << current->s.book
  97.                   << std::endl;
  98.         for (size_t j = 0; j < 4; j++)
  99.             std::cout << "[S " << std::setw(3) << counter
  100.                       << "] Mark(" << j << "): "
  101.                       << current->s.marks[j] << std::endl;
  102.         counter++;
  103.     }
  104.  
  105.     // Своеобразный цикл для обхода списка
  106.     for (ListNode *current = currentNode; current != nullptr; current = current->next) {
  107.         if (current->prev != nullptr) {
  108.             delete current->prev; // Очистка памяти
  109.         }
  110.         currentNode = current; // Двигаем основную вершину в конец
  111.     }
  112.     delete currentNode;  // Удаляем
  113.  
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement