-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
210 lines (207 loc) · 6.78 KB
/
main.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
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
#include "encode.hpp"
#include "list.hpp"
#include "tools.hpp"
#include "tree.hpp"
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
List ReadFile(const char *path);
int Menu(const char *path, List &list, List &queue, Tree &DOPA1);
void CreateQueue(List &list, List &queue);
int main() {
const char dataBasePath[] = "./testBase1.dat";
List list, searchQueue;
Tree DOPA1;
while (true) {
int res = Menu(dataBasePath, list, searchQueue, DOPA1);
if (!res) break;
}
return 0;
}
char CharToUppercase(char symbol) {
if ((int) symbol > -96 && (int) symbol < -64)
return (char) (symbol - 32);
return symbol;
}
int Menu(const char *path, List &list, List &queue, Tree &DOPA1) {
int key;
cout << "Select a menu item:" << endl;
cout << "0 - Exit" << endl;
cout << "1 - Download database on dynamic memory" << endl;
cout << "2 - Print current database state" << endl;
cout << "3 - Merge sort by fields: 1 - publisher 2 - author" << endl;
cout << "4 - Binary search by first three letters publisher house" << endl;
cout << "5 - Print queue by search results" << endl;
cout << "6 - Build optimal binary search tree A1" << endl;
cout << "7 - Tree traversal LNR" << endl;
cout << "8 - Search in tree by year" << endl;
cout << "9 - Encode database by Fano code" << endl;
cout << "10 - Digital sort by fields: 1 - publisher 2 - author" << endl;
cin >> key;
if (!CheckCin()) {
cout << " \x1b[31m Incorrect menu item selection! \x1b[0m" << endl;
return -1;
}
switch (key) {
case 0: {
cout << "Exit";
return 0;
}
case 1: {
if (list.isEmpty()) list = ReadFile(path);
list.createIndexArray();
cout << "\x1b[32m Database recorded on dynamic memory \x1b[0m" << endl;
break;
}
case 2: {
list.printList();
break;
}
case 3: {
if (list.isEmpty()) {
cout << "List is empty!" << endl;
} else {
if (!list.isSorted()) list.digitalSort();
cout << "\x1b[32m List successfully sorted \x1b[0m" << endl;
}
break;
}
case 4: {
list.createIndexArray();
CreateQueue(list, queue);
if (queue.isEmpty()) {
cout << "No records found for this search key!" << endl;
} else {
queue.createIndexArray();
cout << "\x1b[32m Queue successfully build \x1b[0m" << endl;
}
break;
}
case 5: {
if (queue.isEmpty())
cout << " \x1b[31m Queue not built yet! \x1b[0m" << endl;
else
queue.printList();
break;
}
case 6: {
if (queue.isEmpty()) {
cout << " \x1b[31m Queue for tree build doesn't ready! \x1b[0m" << endl;
break;
} else if (DOPA1.isEmpty()) {
DOPA1.setIndexArray(queue.getIndexArray(), queue.getIndexArraySize());
DOPA1.buildTreeA1();
}
cout << "\x1b[32m Tree successfully build \x1b[0m" << endl;
break;
}
case 7: {
if (DOPA1.isEmpty())
cout << " \x1b[31m Tree doesn't ready! \x1b[0m" << endl;
else {
Vertex *root = DOPA1.getRoot();
DOPA1.printLeftToRight(root);
}
break;
}
case 8: {
if (DOPA1.isEmpty()) {
cout << " \x1b[31m Tree doesn't ready! \x1b[0m" << endl;
} else {
Vertex *root = DOPA1.getRoot();
int searchKey;
cout << "Please enter year for records search: ";
cin >> searchKey;
if (!CheckCin()) {
cout << " \x1b[31m Incorrect entry of the search field! \x1b[0m" << endl;
break;
}
vector<record> result = DOPA1.search(searchKey, root);
if (result.empty()) {
cout << "No records with this year were found in queue!" << endl;
} else {
for (auto &i : result) {
cout << " ";
cout.width(sizeof(i.author));
cout << i.author;
cout.width(sizeof(i.title));
cout << i.title;
cout.width(sizeof(i.publishingHouse));
cout << i.publishingHouse;
cout.width(sizeof(numbersOutWidth));
cout << i.year;
cout.width(sizeof(numbersOutWidth));
cout << i.qtyPages << endl;
}
}
}
break;
}
case 9: {
if (list.isEmpty()) {
cout << "List is empty!" << endl;
} else {
Encode fano(list);
fano.encodeFano();
}
break;
}
case 10: {
if (list.isEmpty()) {
cout << "List is empty!" << endl;
} else {
if (!list.isSorted()) list.digitalSort();
cout << "\x1b[32m List successfully sorted \x1b[0m" << endl;
}
break;
}
default: {
cout << "\x1b[31m There is no such item in the menu! \x1b[0m" << endl;
break;
}
}
return 1;
}
void CreateQueue(List &list, List &queue) {
Node **arr = list.getIndexArray();
char searchKey[4];
int tryCounter = 0;
cout << "Enter the first three letters of the publisher to search for: ";
while (true) {
cin >> searchKey;
if (tryCounter != 0) {
cout << "\x1b[31m Input Error! try again \x1b[0m" << endl;
}
tryCounter++;
if (CheckCin()) break;
}
searchKey[0] = CharToUppercase(searchKey[0]);
int res = list.binarySearch(searchKey);
queue.setFoundIndex(res);
if (res != -1) {
queue.addNode(arr[res]->data);
do {
res++;
if (strncmp(arr[res]->data.publishingHouse, searchKey, strlen(searchKey)) == 0) {
queue.addNode(arr[res]->data);
} else {
break;
}
} while (true);
}
}
List ReadFile(const char *path) {
List tempList;
record temp{};
ifstream file(path, ios::in | ios::binary);
if (!file) {
cout << "\x1b[31m File open error! \x1b[0m";
exit(42);
}
while (file.read((char *) (&temp), sizeof(record))) {
tempList.addNode(temp);
}
file.close();
return tempList;
}