Advertisement
kokokozhina

datasort_1

Feb 16th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <iomanip>
  7. //По фамилии с помощью сортировки Шелла с шагом 𝑑 = 2^𝑖 − 1.
  8. using namespace std;
  9. //В файле содержатся данные о сотрудниках предприятия: ФИО, должность,
  10. //дата рождения, стаж работы, зарплата (не менее 20 человек). В новый файл вывести
  11. //данные, отсортированные по какому-либо ключу
  12.  
  13. struct worker
  14. {
  15.     string surname;
  16.     string name;
  17.     string patronymic;
  18.     string position;
  19.     string birth_date;
  20.     string exp;
  21.     string salary;
  22. };
  23.  
  24. void shell(vector<worker> &vec, int n) //сортировка Шелла //n = vec.size();
  25. {
  26.     int d = int(log(n + 1) / log(2));
  27.     while (d > 0)
  28.     {
  29.         for (int i = 0; i < n - d; i++)
  30.         {
  31.             int j = i;
  32.             while (j >= 0 && vec[j].surname > vec[j + d].surname)
  33.             {
  34.                 swap(vec[j], vec[j + d]);
  35.                 j--;
  36.             }
  37.         }
  38.         d /= 2;
  39.     }
  40. }
  41.  
  42. int main()
  43. {
  44.     ifstream in("in.txt");
  45.     ofstream out("out.txt");
  46.     vector<worker> vec;
  47.     while(in.peek() != EOF)
  48.     {
  49.         worker result;
  50.         in >> result.surname >> result.name >> result.patronymic >> result.position >> result.birth_date >> result.exp >> result.salary;
  51.         vec.push_back(result);
  52.     }
  53.     shell(vec, vec.size());
  54.     for(int i = 0; i < vec.size(); i++)
  55.     {
  56.         out << left << setw(20) << vec[i].surname << setw(20) << vec[i].name << setw(20) << vec[i].patronymic << setw(20) << vec[i].position << setw(20) << vec[i].birth_date << setw(20) << vec[i].exp << setw(20) << vec[i].salary << endl;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement