-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting4.cpp
More file actions
144 lines (117 loc) · 5.28 KB
/
Sorting4.cpp
File metadata and controls
144 lines (117 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
ifstream in("input.txt");
ofstream out("output.txt");
struct date { //дата
int dd, mm, yy; //день, месяц, год
};
struct people { //данные о сотруднике
string surname; //фамилия
string position; //должность
date birthday; //дата рождения
int experiance; //стаж(в годах)
int salary; //зарплата
};
date stringToDate(string str) { //перевод строки в дату
date x;
string tmp = str.substr(0, 2); //день
x.dd = stoi(tmp);
tmp = str.substr(3, 2); //месяц
x.mm = stoi(tmp);
tmp = str.substr(6, 4); //год
x.yy = stoi(tmp);
return x;
}
vector<people> inFile() { //ввод из файла
vector<people> staff;
people p;
while (in.peek() != EOF) {
in >> p.surname; //фамилия
in >> p.position; //должность
string tmp; in >> tmp;
p.birthday = stringToDate(tmp); //дата рождения
in >> p.experiance; //стаж
in >> p.salary; //зарплата
staff.push_back(p);
}
return staff;
}
void print(people p) { //вывод в файл
out << setw(15) << left << p.surname; //по левому краю, 15 позиций для фамилии
out << setw(15) << left << p.position; //по левому краю, 15 позиций для должности
if (p.birthday.dd < 10)
out << left << '0' << p.birthday.dd << '.'; //добавляем 0, если день рождения меньше 10
else
out << left << p.birthday.dd << '.';
if (p.birthday.mm < 10)
out << left << '0' << p.birthday.mm << '.'; //добавляем 0, если месяц рождения меньше 10
else
out << left << p.birthday.mm << '.';
out << left << setw(6) << p.birthday.yy; //по левому краю, 6 позиций для года рождения
out << left << setw(4) << p.experiance; //по левому краю, 4 позиций для стажа
out << left << setw(10) << p.salary << '\n'; //по левому краю, 10 позиций для зарплаты
}
bool operator < (people first, people second) { //переопределяем оператор <
if (first.experiance < second.experiance)
return true;
return false;
}
bool operator > (people first, people second) { //переопределяем оператор >
if (first.experiance > second.experiance)
return true;
return false;
}
void InsertionSort(vector<people> &staff) { //сортировка вставкой
for (int i = 1; i < staff.size(); i++) {
int j = i;
while (j > 0 && staff[j] < staff[j - 1]) {
swap(staff[j], staff[j - 1]);
j--;
}
}
}
int getMaxExperiance(vector<people> staff) { //поиск максимального опыта
people maxExp = staff[0]; //предположительный максимум
for (auto p : staff)
maxExp = p > maxExp ? p : maxExp; //сравниваем текущий максимум с элементом
return maxExp.experiance;
}
int getMinExperiance(vector<people> staff) { //поиск минимального опыта
people minExp = staff[0]; //предположительный минимум
for (auto p : staff) //сравниваем текущий минимум с элементом
minExp = p < minExp ? p : minExp;
return minExp.experiance;
}
void BucketSort(vector<people> &staff) { //блочная сортировка
const int P = 5; //количество корзин
int maxExperiance = getMaxExperiance(staff); //максимальный опыт
int minExperiance = getMinExperiance(staff); //минимальный опыт
const int m = (maxExperiance - minExperiance) / P; //коэффициент m
vector< vector<people> > segments(5); //отрезки-корзины
for (int i = 0; i < staff.size(); i++) { //распределение элементов исходного массива по корзинам
int k = (staff[i].experiance - minExperiance) / m; //определение номера корзины
if (k == P) //если k равно количеству корзин,
segments[P - 1].push_back(staff[i]); //то ддобавляем элемент в последнюю корзину
else
segments[k].push_back(staff[i]);
}
for (int i = 0; i < P; i++) { //в каждой корзине проводим
InsertionSort(segments[i]); //сортировку вставкой
}
staff.clear(); //очищаем исходный массив
for (int i = 0; i < P; i++) //покорзинно
for (auto p : segments[i]) //добавляем в массив элементы
staff.push_back(p);
}
int main() {
vector<people> staff; //объявляем вектор с персоналом
staff = inFile(); //заполняем вектор
BucketSort(staff); //сортируем вектор поблочно
for (auto p : staff) //выводим персонал в output.txt
print(p);
return 0;
}