-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
362 lines (327 loc) · 10.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include "includes/tokenizer/ftokenize.h"
#include "includes/bplustree/multimap.h"
#include "includes/bplustree/map.h"
#include "includes/sql/sql.h"
#include <string>
#include <cctype>
#include <random>
using namespace std;
MMap<string, long> get_word_indices(char* file_name);
void swap(int &x, int &y);
int random(int low, int high);
void shuffle(int *a, unsigned int sz);
void solitude_test();
void intro();
void sql_test();
void actual();
void automat(string in);
void info();
void btree_test();
int main(int argc, char *argv[])
{
intro();
return 0;
}
void intro() {
cout << endl << setw(35) << "Main Menu" << setw('*');
cout << "\n\nCommand List:" << endl;;
cout << endl << "[Run Solitude (S)]" << endl << "[Run SQL (Q)]" <<
endl << "[Run BTree Test (B)]" << endl << "[Instructions/Info (I)]" << endl << "[Quit (E)]" << endl << endl;
cout << "Please input a command" << endl;
char input;
cin >> input;
input = toupper(input);
while (input != 'S' && input != 'Q' && input != 'I' && input != 'B' && input != 'E') {
cout << "Invalid Input please try again" << endl;
cin >> input;
input = toupper(input);
}
switch (input) {
case 'S':
cout << "Running Solitude Test" << endl;
// _sleep(3);
solitude_test();
break;
case 'Q':
cout << "Running SQL Test" << endl;
// _sleep(3);
sql_test();
break;
case 'I':
info();
break;
case 'B':
cout << "Running BTree Test" << endl;
// _sleep(3);
btree_test();
break;
case 'E':
terminate();
}
}
void btree_test() {
int b[201] = {};
int c[201] = {};
for (int i = 0; i < 201; i++) {
b[i] = i;
}
cout << "Before scramble" << endl;
for (auto i : b) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < 201; i++) {
c[i] = i;
}
cout << "\nShuffling an array of numbers" << endl << endl;
shuffle(c, 201);
for (auto i : c) {
cout << i << " ";
}
cout << endl << endl;
BPlusTree<int> bts(c, 201);
cout << bts << endl;
char yn;
cout << "Back to main menu?" << endl;
cin >> yn;
yn = toupper(yn);
while (yn != 'Y' && yn != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> yn;
yn = toupper(yn);
}
if (yn == 'Y') {
intro();
}
}
void info() {
cout << endl << setw(35) << "INFO" << setw('*');
vectorstr info = {
"<CREATE | MAKE> : { <create | make> table <TABLE_NAME> fields <FIELD_NAME> [, <FIELD_NAME>...] }",
"<INSERT> : { insert <INTO> <TABLE_NAME> values <VALUE> [, <VALUE>...] }",
"<SELECT> : { select <* | FIELD_NAME> [, ,FIELD_NAME>...] from <TABLE_NAME>",
"where <FIELD_NAME> <RELATIONAL_OPERATOR> <VALUE>",
"[<LOGICAL_OPERATOR> <FIELD_NAME> <RELATIONAL_OPERATOR> <VALUE>...] }",
"<VALUE> : A string of alphanumeric characters, or a string of alphanumeric characters and spaces enclosed by double quotation marks: \"Jean Luise\", Finch, 1923",
"<RELATIONAL OPERATOR> : [ = | > | < | >= | <= ]",
"<LOGICAL OPERATOR> : [and | or]" };
cout << "\n\nCommand List:" << endl;;
cout << endl << "[Info about Solitude (S)]" << endl << "[Info about SQL (Q)]" <<
endl << "[Info about BTree (B)]" << endl << endl;
cout << "Please input a command" << endl;
char input;
cin >> input;
input = toupper(input);
while (input != 'S' && input != 'Q' && input != 'B') {
cout << "Invalid Input please try again" << endl;
cin >> input;
input = toupper(input);
}
switch (input) {
case 'S':
cout << "\n\n\nSolitude: " << endl;
cout << "Solitude tests how the multimap functions. The Solitude file is tokenized and each word is assigned the data of position they appear in. Each word should have multiple values in order to show a functioning multimap" << endl;
cout << endl << endl;
break;
case 'Q':
cout << "\n\n\nSQL Commands" << endl << info << endl << endl;
cout << "Those are the command templates; make/create to initiate table, insert to add data, select to choose certain data" << endl;
cout << "Uses Map and Multimaps with a B+Tree base which writes into a binary and text file for storage" << endl << endl << endl;
break;
case 'B':
cout << "\n\nBTree: " << endl;
cout << "A self balancing tree where it maintains sorted data and allows for searches, insertions, access, and deletion in log(n)" << endl << endl;
break;
}
char yn;
cout << "Back to main menu?" << endl;
cin >> yn;
yn = toupper(yn);
while (yn != 'Y' && yn != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> yn;
yn = toupper(yn);
}
if (yn == 'Y') {
intro();
}
}
void sql_test() {
cout << endl << setw(35) << "SQL Test" << setw('*');
cout << "\n\nCommand List:" << endl;;
cout << endl << "[Run from File (F)]" << endl << "[New Input (N)]" << endl;
cout << "Please input a command" << endl;
char input;
cin >> input;
input = toupper(input);
while (input != 'F' && input != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> input;
input = toupper(input);
}
switch (input) {
case 'N':
actual();
break;
case 'F':
cout << "Enter file name with txt included" << endl;
string input;
cin >> input;
automat(input);
break;
}
char yn;
cout << "Back to main menu?" << endl;
cin >> yn;
yn = toupper(yn);
while (yn != 'Y' && yn != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> yn;
yn = toupper(yn);
}
if (yn == 'Y') {
intro();
}
}
void automat(string in) {
SQL s(in);
char yn;
cout << "Back to main menu?" << endl;
cin >> yn;
yn = toupper(yn);
while (yn != 'Y' && yn != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> yn;
yn = toupper(yn);
}
if (yn == 'Y') {
intro();
}
}
void actual() {
SQL sql;
Table t;
string in;
char yn;
bool done = false;
while (!done) {
cout << "Enter SQL Command" << endl;
getline(cin >> ws, in);
// cout << in;
if (in.size() > 3 && in[0] == 'S') {
// cout << "Test" << endl;
cout << sql.command(in);
}
else {
sql.command(in);
}
cout << "Next command? y/n" << endl;
cin >> yn;
yn = toupper(yn);
while (yn != 'Y' && yn != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> yn;
yn = toupper(yn);
}
if (yn == 'N') {
done = true;
}
}
cout << "Back to main menu?" << endl;
cin >> yn;
yn = toupper(yn);
while (yn != 'Y' && yn != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> yn;
yn = toupper(yn);
}
if (yn == 'Y') {
intro();
}
}
void solitude_test(){
MMap<string, long> word_indices;
word_indices = get_word_indices("solitude.txt");
cout<<endl<<endl<<endl;
//list all nodes of the index mmap:
for (MMap<string, long>::Iterator it = word_indices.begin();
it != word_indices.end(); it++){
cout<<*it<<endl;
}
cout<<endl<<endl<<endl;
cout<<"---------------------------------------------------"<<endl;
string this_word = "ice";
cout<<"---------------------------------------------------"<<endl;
cout<<"Indices of \""<<this_word<<"\""<<endl;
//list indices of this_word:
if (word_indices.contains(this_word)){
cout<<this_word<<": "<<word_indices[this_word]<<endl;
}
cout<<endl<<endl<<endl;
cout<<"---------------------------------------------------"<<endl;
string from = "ask";
string to = "asker";
//list from .. to:
cout<<"listing indices from \""<<from<<"\" to \""<<to<<"\""<<endl;
cout<<"---------------------------------------------------"<<endl;
for (MMap<string, long>::Iterator it =
word_indices.lower_bound(from);
it != word_indices.upper_bound(to) &&
it != word_indices.end(); it++){
cout<<*it<<endl;
}
cout <<endl<<endl<<endl<< "========== E N D ====================" << endl;
char yn;
cout << "Back to main menu?" << endl;
cin >> yn;
yn = toupper(yn);
while (yn != 'Y' && yn != 'N') {
cout << "Invalid Input please try again" << endl;
cin >> yn;
yn = toupper(yn);
}
if (yn == 'Y') {
intro();
}
}
MMap<string, long> get_word_indices(char* file_name){
const bool debug = false;
MMap<string, long> word_indices;
FTokenizer ftk("solitude.txt");
Token t;
long count = 0;
ftk >> t;
while (ftk.more()){
//only the "words"
if (t.type_string() == "ALPHA"){
string s;
s = t.token_str();
word_indices[s] += count;
count++;
if (debug)
cout<<"|"<<t.token_str()<<"|"<<endl;
}
ftk >> t;
}
return word_indices;
}
int random(int low, int high){
//call srand in main if you want better random numbers.
return static_cast<int>(low + (rand() % static_cast<int>((high - low + 1))));
}
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
void shuffle(int *a, unsigned int sz) {
int s = sz -1;
for (int i = 0; i < s; i++) {
int rdm1 = random(0, s);
int rdm2 = random(0, s);
swap(a[rdm1], a[rdm2]);
}
}