-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchipelagoExpedition.cpp
More file actions
401 lines (363 loc) · 10.2 KB
/
ArchipelagoExpedition.cpp
File metadata and controls
401 lines (363 loc) · 10.2 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
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
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "proj6.h"
// default constructor that sets dynamic array of islands at 10
ArchipelagoExpedition::ArchipelagoExpedition() {
islands = new Island[11];
for (int i = 1; i < 11; i++) {
Island newI;
MyList* newL = new MyList();
newL->push_back(i);
newI.setnList(newL);
islands[i] = newI;
}
// # of islands (not storage)
size = 10;
files = new myLL();
}
// a function to process the input given based on a few commands
void ArchipelagoExpedition::processCommandLoop (FILE* inFile)
{
char buffer[300];
char* input;
input = fgets ( buffer, 300, inFile ); // get a line of input
// loop until all lines are read from the input
while (input != NULL)
{
// process each line of input using the strtok functions
char* command;
command = strtok (input , " \n\t");
printf ("*%s*\n", command);
if ( command == NULL )
printf ("Blank Line\n");
else if ( strcmp (command, "q") == 0) {
// delete inFile;
doArrClear();
delete islands;
islands = nullptr;
files->clear();
files = nullptr;
return;
}
else if ( strcmp (command, "?") == 0)
showCommands();
else if ( strcmp (command, "t") == 0)
doTravel();
else if ( strcmp (command, "r") == 0)
doResize();
else if ( strcmp (command, "i") == 0) {
doInsert();
}
else if ( strcmp (command, "d") == 0)
doDelete();
else if ( strcmp (command, "l") == 0)
doList();
else if ( strcmp (command, "f") == 0)
doFile();
else if ( strcmp (command, "#") == 0)
;
else
printf ("Command is not known: %s\n", command);
input = fgets ( buffer, 300, inFile ); // get the next line of input
}
}
// prints available commands to console/terminal
void ArchipelagoExpedition::showCommands()
{
printf ("The commands for this project are:\n");
printf (" q\n"); // quit program
printf (" ?\n"); // basically showCommands
printf (" #\n"); // ignore line of input
printf (" t <int1> <int2>\n"); // find shortest path from island1 to island2
printf (" r <int>\n"); // remove this island & resize islands array
printf (" i <int1> <int2>\n"); // insert a link between island1 to island2
printf (" d <int1> <int2>\n"); // remove island1's link to island2
printf (" l\n"); // list everything
printf (" f <filename>\n"); // read given file
}
// BFS helper function that returns the path list
MyList ArchipelagoExpedition::BFS(int x, int y) {
// setting all prev for the islands to unvisited
for (int i = 1; i < size + 1; i++) {
islands[i].setPrev(-1);
}
MyList iq;
iq.push_back(x);
if (!bfs(y, iq)) {
printf("You can NOT get from island %d to island %d\n", x, y);
return iq;
}
printf("You can get from island %d to island %d\n", x, y);
MyList pathList;
int currentIsland = y;
pathList.push_front(currentIsland);
while (1) {
currentIsland = islands[currentIsland].getPrev();
pathList.push_front(currentIsland);
if (currentIsland == x) {
break;
}
}
return pathList;
}
// BFS function that searches whether a path is possible
bool ArchipelagoExpedition::bfs(int b, MyList& IslandQueue) {
int a;
while (!IslandQueue.isEmpty()) {
a = IslandQueue.getHead()->getValue();
IslandQueue.remove(a);
MyList* oneRide = islands[a].getnList();
myNode* c = oneRide->getHead();
// next node since head == current island
c = c->getNext();
while (c != nullptr) {
// printf("currently c is: %d\n", c->getValue());
if (islands[c->getValue()].getPrev() == -1) {
islands[c->getValue()].setPrev(a);
if (c->getValue() == b) {
return true;
}
IslandQueue.push_back(c->getValue());
}
c = c->getNext();
}
}
return false;
}
// "travels" from one island to the next based on user input
void ArchipelagoExpedition::doTravel()
{
int val1 = 0;
int val2 = 0;
// get an integer value from the input
char* next = strtok (NULL, " \n\t");
if ( next == NULL )
{
printf ("Integer value expected\n");
return;
}
val1 = atoi ( next );
if ( val1 == 0 && strcmp (next, "0") != 0)
{
printf ("Integer value expected\n");
return;
}
// get another integer value from the input
next = strtok (NULL, " \n\t");
if ( next == NULL )
{
printf ("Integer value expected\n");
return;
}
val2 = atoi ( next );
if ( val2 == 0 && strcmp (next, "0") != 0)
{
printf ("Integer value expected\n");
return;
}
printf ("Performing the Travel Command from %d to %d\n", val1, val2);
MyList path = BFS(val1, val2);
if (path.getHead() == nullptr) {
// there exists no path
return;
}
printf("shortest path from %d to %d: ", val1, val2);
myNode* cur = path.getHead();
while (cur->getNext() != nullptr) {
printf("%d - ", cur->getValue());
cur = cur->getNext();
}
printf("%d\n", cur->getValue());
}
// member function to resize array of islands
void ArchipelagoExpedition::doResize()
{
int val1 = 0;
// get an integer value from the input
char* next = strtok (NULL, " \n\t");
if ( next == NULL )
{
printf ("Integer value expected\n");
return;
}
val1 = atoi ( next );
if ( val1 == 0 && strcmp (next, "0") != 0)
{
printf ("Integer value expected\n");
return;
}
printf ("Performing the Resize Command with %d\n", val1 );
// delete array contents
doArrClear();
delete islands;
// new array allocation
islands = new Island[val1 + 1];
for (int i = 1; i < val1 + 1; i++) {
Island newI;
MyList* newL = new MyList();
newL->push_back(i);
newI.setnList(newL);
islands[i] = newI;
}
size = val1;
}
// member function to insert a link between two islands
void ArchipelagoExpedition::doInsert() {
int val1 = 0;
int val2 = 0;
// get an integer value from the input
char* next = strtok (NULL, " \n\t");
if ( next == NULL )
{
printf ("Integer value expected\n");
return;
}
val1 = atoi ( next );
if ( val1 == 0 && strcmp (next, "0") != 0)
{
printf ("Integer value expected\n");
return;
}
if (val1 > size || val1 <= 0) {
printf("Invalid value for island\n");
return;
}
if (islands[val1].getnList() == nullptr) {
printf("Invalid value for island\n");
return;
}
// get another integer value from the input
next = strtok (NULL, " \n\t");
if ( next == NULL )
{
printf ("Integer value expected\n");
return;
}
val2 = atoi ( next );
if ( val2 == 0 && strcmp (next, "0") != 0)
{
printf ("Integer value expected\n");
return;
}
if (val2 > size || val2 <= 0) {
printf("Invalid value for island\n");
return;
}
if (val1 == val2) {
printf("Can not add ferry ride to self\n");
return;
}
// insert new link
printf("Performing the Insert Command for %d\n", val1);
if (islands[val1].getnList()->exist(val2)) {
printf("Ferry ride already added!\n");
return;
}
islands[val1].getnList()->push_back(val2);
}
// member function to delete a link between two islands
void ArchipelagoExpedition::doDelete()
{
int val1 = 0;
int val2 = 0;
// get an integer value from the input
char* next = strtok (NULL, " \n\t");
if ( next == NULL )
{
printf ("Integer value expected\n");
return;
}
val1 = atoi ( next );
if ( val1 == 0 && strcmp (next, "0") != 0)
{
printf ("Integer value expected\n");
return;
}
// get another integer value from the input
next = strtok (NULL, " \n\t");
if ( next == NULL )
{
printf ("Integer value expected\n");
return;
}
val2 = atoi ( next );
if ( val2 == 0 && strcmp (next, "0") != 0)
{
printf ("Integer value expected\n");
return;
}
printf ("Performing the Delete Command for %d\n", val1);
islands[val1].getnList()->remove(val2);
}
// member function that prints everything
void ArchipelagoExpedition::doList()
{
printf ("Displaying the adjacency list:\n");
for (int i = 1; i < size + 1; i++) {
printf("%d -->", i);
myNode* cur = islands[i].getnList()->getHead();
// skip the head which is a repeat of i
cur = cur->getNext();
if (cur != nullptr) {
printf(" ");
while (cur->getNext() != nullptr) {
printf("%d ", cur->getValue());
cur = cur->getNext();
}
printf("%d\n", cur->getValue());
}
else {
printf("\n");
}
}
}
// member function to read the file
void ArchipelagoExpedition::doFile()
{
// get a filename from the input
char* fname = strtok (NULL, " \r\n\t");
if ( fname == NULL )
{
printf ("Filename expected\n");
return;
}
printf ("Performing the File command with file: %s\n", fname);
// next steps: (if any step fails: print an error message and return )
// 1. verify the file name is not currently in use
if (files != nullptr) {
if (files->exist(fname)) {
printf("File is already in use\n");
return;
}
}
// 2. open the file using fopen creating a new instance of FILE*
FILE* infile = nullptr;
infile = fopen(fname, "r");
if (infile == nullptr) {
printf("File is already in use\n");
return;
}
files->push_back(fname);
// printf("THIS IS WHAT IS FILES HEAD RN: %s\n", files->getHead()->value);
// printf("THIS IS WHAT IS FILES TAIL RN: %s\n", files->getTail()->value);
// 3. recursively call processCommandLoop() with this new instance of FILE* as the parameter
processCommandLoop(infile);
// 4. close the file when processCommandLoop() returns
fclose(infile);
files->pop_back();
}
void ArchipelagoExpedition::doArrClear() {
for (int i = 1; i < size + 1; i++) {
if (islands[i].getnList() != nullptr) {
islands[i].getnList()->clear();
}
if (islands[i].getnList() != nullptr) {
delete islands[i].getnList();
}
}
size = 0;
}