Advertisement
Guest User

С++Сортировка массива структуры

a guest
Dec 22nd, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "string.h"
  3. #include "iostream"
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6. #include "math.h"
  7. #include "ctime"
  8. #include "cmath"
  9. #include "conio.h"
  10. #include "iomanip"
  11. using namespace std;
  12.  
  13. struct struc
  14. {
  15.     char* Fam;
  16.     char* Im;
  17.     char* Ot;
  18.     int day;
  19.     int month;
  20.     int year;
  21. };
  22.  
  23. int Kol,i,j;                  // Kol -  количество людей i - счетчик для массива  j - счетчик для сортировки массива
  24. int const k=100;                    // Максимальное количество символов в имени\фамилии\отчестве
  25. struc *FamImOtDatPoj;                          // Массив структур
  26. void Input(struc **FamImOtDatPoj);         // Ввод  несортированного массива структур
  27. void Output(struc **FamImOtDatPoj);        // Вывод отсортированного массива структур
  28. struc *Sort(struc **FamImOtDatPoj);        // Сортировка массива структур по дате
  29. struc *Sort2(struc **FamImOtDatPoj);            // Сортировка массива структур по фамилии
  30.  
  31. int _tmain(int argc, _TCHAR* argv[])
  32. {  
  33.     setlocale(LC_ALL, "rus");
  34.     Input(&FamImOtDatPoj);
  35.     (*FamImOtDatPoj)=*Sort(&FamImOtDatPoj);
  36.     printf("\n\n\n");
  37.     Output(&FamImOtDatPoj);
  38.     printf("\n\n\n");
  39.     (*FamImOtDatPoj)=*Sort2(&FamImOtDatPoj);
  40.     Output(&FamImOtDatPoj);
  41.     delete [] FamImOtDatPoj;
  42.     system("pause");
  43.     return 0;
  44. }
  45.  
  46. void Input (struc **FamImOtDatPoj)
  47. {
  48.     cout << "Введите число людей\n" ;
  49.     cin >> Kol ;
  50.     *FamImOtDatPoj=new struc[Kol];
  51.     for (i=0;i<Kol;i++) {
  52.         (*FamImOtDatPoj)[i].Im=new char[k];
  53.         (*FamImOtDatPoj)[i].Fam=new char[k];
  54.         (*FamImOtDatPoj)[i].Ot=new char[k];
  55.     }
  56.     for (i=0;i<Kol;i++) {
  57.         cout << "Введите имя,фамилию,отчество и дату рождения(день-месяц-год отдельно) [" << i+1 << "-го]:\n" ;
  58.         cin >> (*FamImOtDatPoj)[i].Im >> (*FamImOtDatPoj)[i].Fam >> (*FamImOtDatPoj)[i].Ot >> (*FamImOtDatPoj)[i].day >> (*FamImOtDatPoj)[i].month >> (*FamImOtDatPoj)[i].year ;
  59.     }
  60. }
  61. void Output(struc **FamImOtDatPoj)
  62. {
  63.      for (i=00;i<Kol;i++)
  64.         cout << (*FamImOtDatPoj)[i].Im << " " << (*FamImOtDatPoj)[i].Fam << " " << (*FamImOtDatPoj)[i].Ot << " "
  65.         << (*FamImOtDatPoj)[i].day << " " << (*FamImOtDatPoj)[i].month << " " << (*FamImOtDatPoj)[i].year << "\n" ;
  66. }    
  67.  
  68. struc *Sort(struc **FamImOtDatPoj)
  69. {
  70.      for (int i = 0; i<=Kol; i++) {
  71.          for (j=i;((((*FamImOtDatPoj)[j - 1].year * 365 + (*FamImOtDatPoj)[j - 1].month * 30 + (*FamImOtDatPoj)[j - 1].day)   <  
  72.              ((*FamImOtDatPoj)[j].year * 365 + (*FamImOtDatPoj)[j].month * 30 + (*FamImOtDatPoj)[j].day)) && (j > 0));j--);  
  73.          {
  74.             swap((*FamImOtDatPoj)[j], (*FamImOtDatPoj)[j-1]);
  75.            
  76.          }
  77.     }
  78.     return (*FamImOtDatPoj);
  79. }
  80. struc *Sort2(struc **FamImOtDatPoj)
  81. {    
  82.     for (int i = 0; i<=Kol; i++)
  83.    {
  84.          for (j=i;((((*FamImOtDatPoj)[j - 1].Fam) < ((*FamImOtDatPoj)[j].Fam)) && (j >0 ));j--);
  85.          {
  86.             swap((*FamImOtDatPoj)[j], (*FamImOtDatPoj)[j-1]);
  87.            
  88.          }
  89.    }
  90.     return (*FamImOtDatPoj);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement