-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.cpp
116 lines (107 loc) · 3.33 KB
/
8.cpp
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
// 2. По году рождения с помощью сортировки подсчетом.
// В файле содержатся данные о сотрудниках предприятия: фамилия, должность, дата рождения (день, месяц, год), стаж работы, зарплата.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <iomanip>
using namespace std;
ifstream in("1.txt");
ofstream out("output.txt");
struct date
{ // дата
int dd, mm, yy;
};
struct people
{ // данные о человеке
string Surname; // фамилия
string position;
date DateOfBirth; // дата рождения
int experience;
int Salary; // зарплата
};
date Str_to_Date(string str)
{ // из строки в дату
date x;
string temp = str.substr(0, 2); // день
x.dd = atoi(temp.c_str());
temp = str.substr(3, 2); // месяц
x.mm = atoi(temp.c_str());
temp = str.substr(6, 4); // год
x.yy = atoi(temp.c_str());
return x;
}
vector<people> inFile()
{ // ввод из файла
vector<people> x;
people temp;
while (in.peek() != EOF)
{
in >> temp.Surname; // фамилия
in >> temp.position; // должность
string tmp; // дата рождения
in >> tmp;
temp.DateOfBirth = Str_to_Date(tmp);
in >> temp.experience;
in >> temp.Salary; // зарплата
x.push_back(temp);
}
return x;
}
void print(people x)
{ // вывод в файл
out << setw(15) << left << x.Surname; // по левому краю, 10 позиций для фамилии
out << setw(15) << left << x.position; // по левому краю, 10 позиций для должности
if (x.DateOfBirth.dd < 10)
out << left << '0' << x.DateOfBirth.dd << '.'; // добавляем 0
else
out << left << x.DateOfBirth.dd << '.';
if (x.DateOfBirth.mm < 10)
out << '0' << x.DateOfBirth.mm << '.';
else
out << x.DateOfBirth.mm << '.';
out << left << setw(6) << x.DateOfBirth.yy; // на год 6 позиций
out << setw(10) << left << x.experience;
out << left << setw(10) << x.Salary << endl; // запрлата
}
bool operator<(people a, people b)
{ // переопределяем оператор < в соотвествии с условием
if (a.DateOfBirth.yy < b.DateOfBirth.yy)
return true;
return false;
}
void counter_sort(vector<people> &x)
{
people min1 = x[0];
people max1 = x[0];
for (int i = 0; i < x.size(); i++)
{
if (max1 < x[i])
max1 = x[i];
if (x[i] < min1)
min1 = x[i];
}
int k = abs(max1.DateOfBirth.yy - min1.DateOfBirth.yy + 1);
vector<vector<people>> B2 (max1.DateOfBirth.yy + 1);
int temp = min1.DateOfBirth.yy;
while (temp <= max1.DateOfBirth.yy)
{
for (int i = 0; i < x.size(); i++) {
if (x[i].DateOfBirth.yy == temp)
B2[temp].push_back(x[i]); }
temp++;
}
for (auto c : B2) {
for (people r : c) {
print(r);
}
}
}
int main()
{
vector<people> x;
x = inFile();
counter_sort(x);
return 0;
}