-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice11.cpp
More file actions
191 lines (179 loc) · 7.76 KB
/
practice11.cpp
File metadata and controls
191 lines (179 loc) · 7.76 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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
struct VideoСassette {
int cassetteCode;
std::string filmName;
std::string filmDirector;
int year;
VideoСassette(int cassetteCode, std::string filmName, std::string filmDirector, int year) : cassetteCode{ cassetteCode }, filmName{ filmName },
filmDirector{ filmDirector }, year{ year } {};
void outputVideoCassette() {
std::cout << this->cassetteCode << std::endl;
std::cout << this->filmName << std::endl;
std::cout << this->filmDirector << std::endl;
std::cout << this->year << std::endl;
}
};
void inputN(int& n) {
std::cout << "Введите количество считываемых объектов из файла" << std::endl;
while (true) {
std::cout << "n: ";
std::cin >> n;
if (n > 0 && n <= 50) break;
else std::cout << "Вы ввели неверное значение n. Попробуйте снова." << std::endl;
}
}
void inputPath(std::string& path) {
std::cout << "Введите путь до файла, с которым хотите работать." << std::endl;
while (true) {
std::cout << "path: ";
std::cin >> path;
if (path.find(".txt") != std::string::npos) break;
else std::cout << "Вы ввели неверный путь до файла. Попробуйте снова." << std::endl;
}
}
void inputVideoCassetteArray(const std::string path, int& n, std::vector<VideoСassette*>& videoCassetteArray) {
int startN = n;
std::ifstream fin;
fin.open(path);
if (fin.is_open()) {
std::string info;
short int operationCount = 0;
int cassetteCode;
std::string filmName;
std::string filmDirector;
int year;
while (!fin.eof() && startN) {
std::getline(fin, info);
if (operationCount == 0) cassetteCode = std::stoi(info);
else if (operationCount == 1) filmName = info;
else if (operationCount == 2) filmDirector = info;
else if (operationCount == 3) year = std::stoi(info);
else {
videoCassetteArray.push_back(new VideoСassette(cassetteCode, filmName, filmDirector, year));
operationCount = -1;
startN--;
}
operationCount++;
}
if (startN > 0) {
videoCassetteArray.push_back(new VideoСassette(cassetteCode, filmName, filmDirector, year));
startN--;
}
if (startN != 0) {
n -= startN;
std::cout << "Корректировка размера коллекции на n = " << n << std::endl;
}
}
else std::cout << "Неверное имя файла.";
}
void sortVideoCassetteArray(std::vector<VideoСassette*>& videoCassetteArray) {
std::sort(std::begin(videoCassetteArray), std::end(videoCassetteArray), [](VideoСassette* a, VideoСassette* b) -> bool {
return a->cassetteCode < b->cassetteCode;
});
}
void outputVideoCassetteArray(const int n, std::vector<VideoСassette*>& videoCassetteArray) {
sortVideoCassetteArray(videoCassetteArray);
for (size_t i = 0; i < n; i++)
videoCassetteArray[i]->outputVideoCassette();
}
void findVideoCassette(const int n, const int cassetteCode, const std::vector<VideoСassette*> videoCassetteArray) {
int start = 0, end = n - 1, mid;
while (start <= end) {
mid = (start + end) / 2;
if (videoCassetteArray[mid]->cassetteCode < cassetteCode) start = mid + 1;
else if (videoCassetteArray[mid]->cassetteCode > cassetteCode) end = mid - 1;
else { videoCassetteArray[mid]->outputVideoCassette(); return; }
}
std::cout << "Видеокассета с данным номером не найдена." << std::endl;
}
void writeByteVideoCassetteArray(const int n, const std::string path, std::vector<VideoСassette*>& videoCassetteArray) {
sortVideoCassetteArray(videoCassetteArray);
std::ofstream fout;
fout.open(path);
if (fout.is_open()) {
for (size_t i = 0; i < n; i++)
fout.write((char*)&(*videoCassetteArray[i]), sizeof(VideoСassette));
}
else std::cout << "Неверное имя файла." << std::endl;
}
void readByteVideoCassetteArray(const std::string path, int& n, std::vector<VideoСassette*>& videoCassetteArray) {
int startN = n;
std::vector<VideoСassette*> newVideoCassetteArray;
newVideoCassetteArray.push_back(new VideoСassette(0, "", "", 0));
size_t count = 0;
std::ifstream fin;
fin.open(path);
if (fin.is_open()) {
while (fin.read((char*)&(*newVideoCassetteArray[count]), sizeof(VideoСassette)) && startN) {
count++;
newVideoCassetteArray.push_back(new VideoСassette(0, "", "", 0));
startN--;
}
newVideoCassetteArray.erase(std::begin(newVideoCassetteArray) + newVideoCassetteArray.size() - 1);
videoCassetteArray = newVideoCassetteArray;
if (startN != 0) {
n -= startN;
std::cout << "Корректировка размера коллекции на n = " << n << std::endl;
}
}
else std::cout << "Неверное имя файла." << std::endl;
}
int main() {
setlocale(LC_ALL, "ru");
int operation;
int n;
std::string path;
std::vector<VideoСassette*> videoCassetteArray;
while (true) {
std::cout << "Введите операцию:\n"
<< "1. Ввод данных об n объектах из текстового файла в массив структур (0<n<=50)\n"
<< "2. Сортировка массива структур по возрастанию поля \"Код видеокассеты\"\n"
<< "3. Вывод данных об объектах на экран в упорядоченном по возрастанию виде\n"
<< "4. Поиск объекта по значению поля \"Код видеокассеты\"\n"
<< "5. Запись упорядоченных данных об объектах в двоичный файл\n"
<< "6. Чтение двоичного файла\n"
<< "0. Выход\n>>> ";
std::cin >> operation;
switch (operation) {
case 1: {
inputN(n);
inputPath(path);
inputVideoCassetteArray(path, n, videoCassetteArray);
break;
}
case 2:
sortVideoCassetteArray(videoCassetteArray);
break;
case 3:
outputVideoCassetteArray(n, videoCassetteArray);
break;
case 4: {
int cassetteCode;
std::cout << "Введите номер кассеты, которую хотите найти: ";
std::cin >> cassetteCode;
sortVideoCassetteArray(videoCassetteArray);
findVideoCassette(n, cassetteCode, videoCassetteArray);
break;
}
case 5: {
inputPath(path);
writeByteVideoCassetteArray(n, path, videoCassetteArray);
break;
}
case 6: {
inputN(n);
inputPath(path);
readByteVideoCassetteArray(path, n, videoCassetteArray);
break;
}
case 0:
std::cout << "Завершение программы." << std::endl;
exit(0);
default: { std::cout << "Неверно введена операция. Попробуйте снова." << std::endl; break; }
}
}
}