-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
232 lines (177 loc) · 5.18 KB
/
main.cpp
File metadata and controls
232 lines (177 loc) · 5.18 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>
#include "termlib.h"
#include "srlz.h"
class Position {
unsigned int id;
std::string name;
public:
static const int NAME_LENGTH = 20;
static unsigned int count;
Position() {
std::cout << "Enter name: ";
std::cin >> std::setw(NAME_LENGTH) >> name;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
id = count++;
}
Position(std::string _name, const unsigned int _id){
name = _name; id = _id;
}
static void displayByName(std::vector<Position*> positions) {
if (auto posCount = positions.size()) {
std::string toSearch;
std::cout << "Enter name to search: ";
std::cin >> std::setw(Position::NAME_LENGTH) >> toSearch;
std::cout << std::endl;
std::vector<Position*> foundPos;
for (auto i = 0U; i < posCount; ++i) {
if (positions[i]->getName() == toSearch) {
foundPos.push_back(positions[i]);
}
}
if (auto foundCount = foundPos.size())
Position::print(foundPos);
else
std::cout << "No positions found" << std::endl;
} else
std::cout << "List is empty.";
}
static void print(std::vector<Position*> positions) {
std::cout << " " << std::setw(3) << "#" << " | " << std::setw(Position::NAME_LENGTH) << "Name" << " | " << std::endl;
for (unsigned int i = 0U, printCount = positions.size(); i < printCount; ++i)
std::cout << " " << std::setw(3) << positions[i]->getId() << " | " << std::setw(Position::NAME_LENGTH) << positions[i]->getName() << " | " << std::endl;
}
unsigned int getId() const {
return id;
}
std::string getName() {
return name;
}
};
unsigned int Position::count = 0;
int main() {
std::vector<Position*> allPos;
std::ifstream in("data.dat", std::ios::in);
//Считываем данные из файла, если такие есть
if (in) {
std::size_t posCount = 0;
in.read(reinterpret_cast<char*>(&posCount), sizeof(posCount));
in.read(reinterpret_cast<char*>(&Position::count), sizeof(Position::count));
for (auto i = 0U; i < posCount; i++) {
std::string _name;
unsigned int _id;
srlz::read(in, _name);
in.read(reinterpret_cast<char*>(&_id), sizeof(_id));
//Проверяем входящий id на уникальность
bool isDuplicate = std::count_if(allPos.begin(), allPos.end(), [_id](auto pos) {
auto state = pos->getId() == _id;
term::clrscr();
return state;
});
if (isDuplicate) {
std::cout << "File 'data.dat' is corrupted" << std::endl
<< "1 - Clear list and continue" << std::endl
<< "2 - Exit " << std::endl;
switch (term::getch()) {
case 49: {
in.close();
std::fstream of("data.dat", std::ios::trunc);
of.write("",0);
of.close();
allPos.clear();
break;
}
case 50: {
return 0;
}
default: {
term::clrscr();
std::cout << "Invalid key.";
break;
}
}
break;
}
allPos.push_back(new Position(_name, _id));
}
}
while (true) {
term::clrscr();
std::cout << "___________Menu___________" << std::endl
<< "1. Add new position" << std::endl
<< "2. Print position's list" << std::endl
<< "3. Find positions by name" << std::endl
<< "4. Delete position by id" << std::endl
<< "5. Exit" << std::endl;
switch (term::getch()) {
case 49 : { //1
term::clrscr();
allPos.push_back(new Position); // delete on p.5
break;
}
case 50: {//2
term::clrscr();
if (auto posCount = allPos.size())
Position::print(allPos);
else
std::cout << "List is empty." << std::endl;
std::cout << "Press any key." << std::endl;
term::getch();
break;
}
case 51: {//3
term::clrscr();
Position::displayByName(allPos);
std::cout << std::endl
<< "Press any key." << std::endl;
term::getch();
break;
}
case 52 : {//4
term::clrscr();
if (allPos.size()) {
unsigned int _id;
std::cout << "Enter id of deleting element: " << std::endl;
std::cin >> _id;
auto newEnd = std::remove_if(allPos.begin(), allPos.end(), [_id](auto pos) {
auto state = pos->getId() == _id;
term::clrscr();
std::cout << (state ? "Position has removed" : "Position not found.") << std::endl;
return state;
});
allPos.erase(newEnd, allPos.end());
} else
std::cout << "List is empty." << std::endl;
std::cout << "Press any key." << std::endl;
term::getch();
break;
}
case 53: {//5
//Записываем изменения в файл
std::ofstream out("data.dat", std::ios::binary);
std::size_t posCount = allPos.size();
out.write(reinterpret_cast<char*>(&posCount), sizeof(posCount));
out.write(reinterpret_cast<char*>(&Position::count), sizeof(Position::count));
for (auto i = 0U; i < posCount; i++) {
auto _id = allPos[i]->getId();
auto _name = allPos[i]->getName();
srlz::write(out, _name);
out.write(reinterpret_cast<char*>(&_id), sizeof(_id));
}
allPos.clear();
return 0;
}
default: {
term::clrscr();
std::cout << "Invalid key.";
break;
}
}
}
return 0;
}