Advertisement
kokokozhina

datasort_6

Feb 18th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  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 insertionSort(vector<worker> &vec, int n)
  30. {
  31.     for(int i = 1; i < n; i++)    
  32.         for(int j = i; j > 0 && (vec[j - 1].surname > vec[j].surname || vec[j - 1].surname == vec[j].surname && birthYear(vec[j - 1].birth_date) > birthYear(vec[j].birth_date)); j--)
  33.             swap(vec[j - 1], vec[j]);
  34. }
  35.  
  36. int main()
  37. {
  38.     ifstream in("in.txt");
  39.     ofstream out("out.txt");
  40.     vector<worker> vec;
  41.     while(in.peek() != EOF)
  42.     {
  43.         worker result;
  44.         in >> result.surname >> result.name >> result.patronymic >> result.position >> result.birth_date >> result.exp >> result.salary;
  45.         vec.push_back(result);
  46.     }
  47.     insertionSort(vec, vec.size());
  48.     for(int i = 0; i < vec.size(); i++)
  49.     {
  50.         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;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement