Advertisement
kokokozhina

datasort__11

Feb 18th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <iostream>//bug
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <iomanip>
  7. //Сначала по фамилии, потом по году рождения, потом по должности с помощью быстрой сортировки.
  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. string birthYear(string s)
  25. {
  26.     return s.substr(s.length() - 4, 4);
  27. }
  28.  
  29. void quickSort(vector<worker> &vec, int l, int r)
  30. {
  31.     int mid = l + (r - l) / 2;
  32.     int i = l;
  33.     int j = r;
  34.     while(i <= j)
  35.     {
  36.         while((vec[i].surname < vec[mid].surname) || (vec[i].surname == vec[mid].surname && birthYear(vec[i].birth_date) < birthYear(vec[mid].birth_date)) || (vec[i].surname == vec[mid].surname && birthYear(vec[i].birth_date) == birthYear(vec[mid].birth_date) && vec[i].position < vec[mid].position)) i++;
  37.         while((vec[j].surname > vec[mid].surname) || (vec[j].surname == vec[mid].surname && birthYear(vec[j].birth_date) > birthYear(vec[mid].birth_date)) || (vec[j].surname == vec[mid].surname && birthYear(vec[j].birth_date) == birthYear(vec[mid].birth_date) && vec[j].position > vec[mid].position)) j--;
  38.         if(i <= j)
  39.         {
  40.             swap(vec[i], vec[j]);
  41.             i++;
  42.             j--;
  43.         }
  44.     }
  45.     if (i < r)
  46.         quickSort(vec, i, r);
  47.     if (l < j)    
  48.         quickSort(vec, l, j);
  49.  
  50. }
  51.  
  52.  
  53. int main()
  54. {
  55.     ifstream in("in.txt");
  56.     ofstream out("out.txt");
  57.     vector<worker> vec;
  58.     while(in.peek() != EOF)
  59.     {
  60.         worker result;
  61.         in >> result.surname >> result.name >> result.patronymic >> result.position >> result.birth_date >> result.exp >> result.salary;
  62.         vec.push_back(result);
  63.     }
  64.     quickSort(vec, 0, vec.size() - 1);
  65.     for(int i = 0; i < vec.size(); i++)
  66.     {
  67.         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;
  68.     }
  69.     system("pause");
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement