-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.cpp
444 lines (337 loc) · 13 KB
/
listener.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "listener.h"
Listener::Listener(const std::vector<std::string> &sd, const std::vector<std::string> &strd)
: how_many_words_do_you_want(10),
doc_ptr(new Document(sd)),
trie_ptr1(new Trie(strd, mints::identity_str, mints::identity_str)),
trie_ptr2(new Trie(strd, mints::reversed_str, mints::reversed_str)) {}
Listener::~Listener() {
if (doc_ptr != nullptr) {
delete doc_ptr;
}
if (trie_ptr1 != nullptr) {
delete trie_ptr1;
}
if (trie_ptr2 != nullptr) {
delete trie_ptr2;
}
}
std::string Listener::listen() {
int command, idx, new_holder_type;
unsigned int _i, _j;
std::cout << "Accessed to " << doc_ptr->get_filename() << " file.\n";
std::cout << "To see the manual, input 0.\n\n";
while (true) {
std::cout << "at Document-modifier INPUT COMMAND : ";
std::cin >> command;
std::cin.ignore(1000, '\n');
if (command < 0) {
break;
}
try {
switch (command) {
case 0:
print_manual(); break;
case 1:
doc_ptr->print(); break;
case 2:
doc_ptr->print_infos(); break;
case 10:
std::cout << "Put an index number to print : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
if (idx >= doc_ptr->size()) {
throw mints::input_out_of_range("at Listener::listen, case 10");
}
doc_ptr->at(idx)->print(); break;
case 11:
std::cout << "Put an index number to access : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
if (idx >= doc_ptr->size()) {
throw mints::input_out_of_range("at Listener::listen, case 10");
}
access(idx); break;
case 20:
std::cout << "Choose the type of new holder to push :" << std::endl;
std::cout << "1 StringHolder\t 2 TableHolder\t 3 LineHolder\t 4 HistogramHolder\t -1 cancel\t >> ";
std::cin >> new_holder_type;
std::cin.ignore(1000, '\n');
switch (new_holder_type) {
case -1234:
doc_ptr->push_tes(); break;
case 1:
doc_ptr->push_str(); break;
case 2:
std::cout << "Input the number of rows and columns in order : ";
std::cin >> _i >> _j;
std::cin.ignore(1000, '\n');
if (_i >= 70 || _j >= 15) {
std::cout << "The number of rows and columns are restricted under 70 and 15 respectively.";
throw mints::input_out_of_range("at Listener::listen, case 20-2");
}
doc_ptr->push_tab(_i, _j); break;
case 3:
doc_ptr->push_lin(); break;
case 4:
doc_ptr->push_his(); break;
default:
break;
}
break;
case 21:
std::cout << "Choose the type of new holder to push :" << std::endl;
std::cout << "1 StringHolder\t 2 TableHolder\t 3 LineHolder\t 4 HistogramHolder\t -1 cancel\t >> ";
std::cin >> new_holder_type;
std::cin.ignore(1000, '\n');
std::cout << "Enter the number immediately after the location you want to insert : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
switch (new_holder_type) {
case -1234:
doc_ptr->push_tes_infrontof(idx); break;
case 1:
doc_ptr->push_str_infrontof(idx); break;
case 2:
std::cout << "Input the number of rows and columns in order : ";
std::cin >> _i >> _j;
std::cin.ignore(1000, '\n');
if (_i >= 70 || _j >= 15) {
std::cout << "The number of rows and columns are restricted under 70 and 15 respectively.";
throw mints::input_out_of_range("at Listener::listen, case 21-2");
}
doc_ptr->push_tab_infrontof(idx, _i, _j); break;
case 3:
doc_ptr->push_lin_infrontof(idx); break;
case 4:
doc_ptr->push_his_infrontof(idx); break;
default:
break;
}
break;
case 23:
int idx_to_delete;
std::cout << "Input the index of holder to delete : ";
std::cin >> idx_to_delete;
std::cin.ignore(1000, '\n');
--idx_to_delete;
if (idx_to_delete >= doc_ptr->size()) {
throw mints::input_out_of_range("at Listener::listen, case 22");
}
doc_ptr->remove_holder(idx_to_delete); break;
default:
break;
}
} catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
return save();
}
void Listener::access(int n) {
if (n >= doc_ptr->size()) {
// Wrong access; Do nothing
throw mints::input_out_of_range("at Listener::access");
}
Holder* p = doc_ptr->at(n);
// Check the type of holder and call prefer access functions.
if (p->get_type() == Holder::STRING_HOLDER) {
auto* q = dynamic_cast<StringHolder*>(p);
modify_str(q);
return;
}
if (p->get_type() == Holder::TABLE_HOLDER) {
auto* q = dynamic_cast<TableHolder*>(p);
modify_tab(q);
return;
}
if (p->get_type() == Holder::LINE_HOLDER || p->get_type() == Holder::HISTOGRAM_HOLDER) {
auto* q = dynamic_cast<LineHolder*>(p);
modify_cha(q);
return;
}
throw std::exception();
}
void Listener::modify_str(StringHolder *p) {
std::cout << "Accessed to the StringHolder named " << p->get_title() << std::endl;
std::cout << "To see the manual, input 0.\n\n";
int command, idx, len;
std::string input_str;
while (true) {
p->print();
std::cout << "at StringHolder-modifier INPUT COMMAND : ";
std::cin >> command;
std::cin.ignore(1000, '\n');
if (command < 0) {
break;
}
switch (command) {
case 0:
print_str_manual(); break;
case 1:
p->print(); break;
case 3:
std::cout << "Put your new title : ";
getline(std::cin, input_str);
std::cin.ignore(1000, '\n');
p->set_title(input_str); break;
case 10:
std::cout << "Put your text you want to push back : " << std::endl;
getline(std::cin, input_str);
p->push(input_str); break;
case 11:
std::cout << "Put the index where you want to insert : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
std::cout << "Put your text you want to push back : " << std::endl;
getline(std::cin, input_str);
p->insert(idx, input_str); break;
case 20:
std::cout << "Put the index where you want to delete : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
if (idx >= p->get_size()) {
std::cout << "Invalid index" << std::endl; break;
}
std::cout << "Put the length of text you want to delete : ";
std::cin >> len;
std::cin.ignore(1000, '\n');
p->remove(idx, len); break;
case 21:
std::cout << "Put the index where you want to replace : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
if (idx >= p->get_size()) {
std::cout << "Invalid index"; break;
}
std::cout << "Put the text you want to insert : ";
getline(std::cin, input_str);
p->insert(idx, input_str); break;
case 50:
p->title_on(); break;
case 51:
p->title_off(); break;
case 99:
p->spellcheck(*trie_ptr1, *trie_ptr2, how_many_words_do_you_want); break;
default:
break;
}
}
}
void Listener::modify_tab(TableHolder *p) {
std::cout << "Accessed to the TableHolder named " << p->get_title() << std::endl;
std::cout << "To see the manual, input 0.\n\n";
int command, _i, _j;
std::string input_str;
while (true) {
p->print();
std::cout << "at TableHolder-modifier INPUT COMMAND : ";
std::cin >> command;
std::cin.ignore(1000, '\n');
if (command < 0) {
break;
}
switch (command) {
case 0:
print_tab_manual(); break;
case 1:
p->print(); break;
case 3:
std::cout << "Put your new title : ";
getline(std::cin, input_str);
p->set_title(input_str); break;
case 10:
std::cout << "Put the index number of rows and columns in order : ";
std::cin >> _i >> _j;
std::cin.ignore(1000, '\n');
--_i; --_j;
std::cout << "Put the text you want to replace : " << std::endl;
getline(std::cin, input_str);
p->put(input_str, _i, _j); break;
case 50:
p->title_on(); break;
case 51:
p->title_off(); break;
default:
break;
}
}
}
void Listener::modify_cha(ChartHolder *p) {
std::cout << "Accessed to the ChartHolder named " << p->get_title() << std::endl;
std::cout << "To see the manual, input 0.\n\n";
int command, height, idx;
double data;
std::string input_str;
while (true) {
p->print();
std::cout << "at ChartHolder-modifier INPUT COMMAND : ";
std::cin >> command;
std::cin.ignore(1000, '\n');
if (command < 0) {
break;
}
switch (command) {
case 0:
print_cha_manual();
break;
case 1:
p->print();
break;
case 3:
std::cout << "Put your new title : ";
getline(std::cin, input_str);
p->set_title(input_str);
break;
case 10:
std::cout << "Put the text you want to push : ";
getline(std::cin, input_str);
std::cout << "Put the number you want to push : ";
std::cin >> data;
std::cin.ignore(1000, '\n');
p->push(input_str, data);
break;
case 11:
p->pop();
break;
case 20:
std::cout << "Put the vertical size you want : ";
std::cin >> height;
std::cin.ignore(1000, '\n');
p->modify_i_size(height);
break;
case 21:
std::cout << "Put the index number : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
std::cout << "Put the text you want to replace : ";
getline(std::cin, input_str);
p->modify_dataname(input_str, idx);
break;
case 22:
std::cout << "Put the index number : ";
std::cin >> idx;
std::cin.ignore(1000, '\n');
--idx;
std::cout << "Put the height you want to replace : ";
std::cin >> height;
std::cin.ignore(1000, '\n');
p->modify_height(height, idx);
break;
case 50:
p->title_on();
break;
case 51:
p->title_off();
break;
default:
break;
}
}
}