Advertisement
StoneHaos

sort_simon

Apr 30th, 2020
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct {
  7.     string SNP; //s-фамилия n-имя p-отчество
  8.     int bd, bm, by;//bd-число bm-месяц by-год рождения
  9. }student;
  10.  
  11. /*int comp(const void *a, const void *b) {
  12.     if (((student*)a)->by < ((student*)b)->by)
  13.         return -1;
  14.     else if (((student*)a)->by > ((student*)b)->by)
  15.         return 1;
  16.     else {
  17.         if (((student*)a)->bm < ((student*)b)->bm)
  18.             return -1;
  19.         else if (((student*)a)->bm > ((student*)b)->bm)
  20.             return 1;
  21.         else {
  22.             if (((student*)a)->bd < ((student*)b)->bd)
  23.                 return -1;
  24.             else if (((student*)a)->bd > ((student*)b)->bd)
  25.                 return 1;
  26.             else
  27.                 return 0;
  28.         }
  29.     }
  30. }*/
  31.  
  32. int comp(student a, student b) {
  33.     if (a.by < b.by)
  34.         return -1;
  35.     else if (a.by > b.by)
  36.         return 1;
  37.     else {
  38.         if (a.bm < b.bm)
  39.             return -1;
  40.         else if (a.bm > b.bm)
  41.             return 1;
  42.         else {
  43.             if (a.bd < b.bd)
  44.                 return -1;
  45.             else if (a.bd > b.bd)
  46.                 return 1;
  47.             else
  48.                 return 0;
  49.         }
  50.     }
  51. }
  52.  
  53. void BSort(student *arr, int D, int (*comp)(student a, student b))
  54. {
  55.     for (int i = 0; i < D; ++i)
  56.         for (int j = i; j > 0 && (comp(arr[j], arr[j - 1]) == -1); --j) //arr[j] < arr[j - 1]
  57.         {
  58.             student temp = arr[j];
  59.             arr[j] = arr[j - 1];
  60.             arr[j - 1] = temp;
  61.         }
  62. }
  63.     /*
  64.     Составить список студентов группы, включив следующие данные: Ф.И.О.,
  65.     число, месяц и год рождения. Переставить записи студентов по возрасту
  66.     */
  67.  
  68.    
  69.     int main()
  70.     {
  71.         student ISP[5] =
  72.         {
  73.         {"Golovin Semen D.",13,2,2002},
  74.         {"Starikov Kirill M.",12,8,2002},
  75.         {"Rubanov Michael D.",4,5,2002},
  76.         {"Nikitin Maksim K.",18,11,2002},
  77.         {"Molodchikov Sergey S.",8,7,2002}
  78.         };
  79.         cout << "old" << endl;
  80.         for (int i = 0; i < 5; i++)
  81.         {
  82.             cout << ISP[i].SNP << " " << ISP[i].bd << "." << ISP[i].bm << "." << ISP[i].by << endl;
  83.         }
  84.         BSort(ISP, 5, comp);
  85.         cout << "\n" << "new" << endl;
  86.         for (int i = 0; i < 5; i++)
  87.         {
  88.             cout << ISP[i].SNP << " " << ISP[i].bd << "." << ISP[i].bm << "." << ISP[i].by << endl;
  89.         }
  90.         return 0;
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement