DasShelmer

11.1

Feb 18th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. struct Student {
  8.     string Imya, Familiya, Otchestvo;
  9.     int Kurs, Gruppa;
  10.     double Time;
  11.     string _fioSort = "";
  12.    
  13.    
  14.     string fioSort();
  15.    
  16.     void initialize(ifstream &in);
  17.  
  18.     string toString();
  19. };
  20.  
  21. void Student::initialize(ifstream &in){
  22.         string temp;
  23.         in >> this->Familiya >> this->Imya >> this->Otchestvo >> temp;
  24.         this->Kurs = stoi(temp);
  25.         in >> temp;
  26.         this->Gruppa = stoi(temp);
  27.         in >> temp;
  28.         this->Time = stod(temp);
  29.  
  30. }
  31.  
  32. string Student::toString() {
  33.     string res = this->Familiya + " "
  34.         + this->Imya + " "
  35.         + this->Otchestvo + " "
  36.         + to_string(this->Kurs) + " "
  37.         + to_string(this->Gruppa) + " "
  38.         + to_string(this->Time);
  39.     return res;
  40. }
  41.  
  42. string Student::fioSort (){
  43.         if (!this->_fioSort.length){
  44.             this->_fioSort = Familiya + Imya + Otchestvo;
  45.         }
  46.         return this->_fioSort;
  47. }
  48.    
  49. void shellSort(Student *arr, int length){
  50.     Student temp;
  51.     int i, j, incr = length / 2;
  52.     while (incr > 0){
  53.         for(i = incr; i < length; i++){
  54.             j = i - incr;
  55.             while(j >= 0)
  56.                 if (arr[j + incr].fioSort().compare(arr[j].fioSort())){
  57.                     temp = arr[j];
  58.                     arr[j] = arr[j + incr];
  59.                     arr[j + incr] = temp;
  60.                     j = j - incr;
  61.                 }
  62.                 else{
  63.                     j = -1;
  64.                 }
  65.             incr = incr / 2;
  66.         }
  67.     }
  68. }
  69.  
  70. int main() {
  71.     setlocale(LC_ALL, "Russian");
  72.     long studCount; string temp; int gruppa;
  73.     ifstream in("input.txt");
  74.     ofstream out("output.txt");
  75.     if (!in) return 0; // Файл пуст
  76.     in >> temp;
  77.     studCount = stol(temp);
  78.  
  79.  
  80.     auto students = new Student[studCount];
  81.    
  82.     cout << "Не отсортированные:";
  83.     for (auto s = students; s < students + studCount; s++) {
  84.         s->initialize(in);
  85.        
  86.         cout << "   " << s->toString();
  87.     }
  88.     in.close();
  89.     shellSort(students, studCount);
  90.    
  91.     cout << "Отсортированные:";
  92.     for (auto s = students; s < students + studCount; s++) {
  93.         temp = s->toString();
  94.         cout << "   " + temp;
  95.         out << temp;
  96.     }
  97.    
  98.     out.close();
  99.  
  100.     return 0;
  101. }
Add Comment
Please, Sign In to add comment