-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmshah71proj6.cpp
More file actions
378 lines (326 loc) · 8.93 KB
/
Copy pathmshah71proj6.cpp
File metadata and controls
378 lines (326 loc) · 8.93 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
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include "Classes.h"
class TravelNetwork
{
private:
// Create the Data Members for the Travel Network here
Airport* All_airports;
int CAPACITY;
int carrCAP;
char** filearr;
public:
// Use a constructor to initialize the Data Members for Travel Network
TravelNetwork()
{
CAPACITY = 10;
carrCAP = 1;
All_airports = new Airport[CAPACITY];
filearr = (char**)malloc((carrCAP)*sizeof(char*));
}
TravelNetwork(int newCAPACITY)
{
CAPACITY = newCAPACITY;
All_airports = (Airport*)malloc((CAPACITY) * sizeof(Airport));
}
void grow() {
printf("growing file array\n");
char **temp;
temp = (char**)malloc((carrCAP * 2) * sizeof(char*));
int i;
for (i = 0; i < carrCAP; i++)
temp[i] = filearr[i];
free (filearr);
filearr = temp;
carrCAP = carrCAP * 2;
}
void Free_TN() {
printf("freeing airports ");
for (int i = 0; i < CAPACITY; i++) {
Airport temp = All_airports[i];
printf(" %d", i + 1);
temp.Airport_free();
}
printf("\n");
delete(All_airports);
CAPACITY = 0;
All_airports = NULL;
}
void Free_filearr(){
free(filearr);
filearr = NULL;
}
void List_TN() {
for (int i = 0; i < CAPACITY; i++) {
Airport temp = All_airports[i];
printf("Airports which can be reached from Airport %d directly\n", i + 1);
temp.printnetwork();
}
}
bool dfs(int a, int b) {
//printf("current x:%d current y:%d \n", a, b);
Airport temp1 = All_airports[a - 1];
Mylist* c = temp1.getlistp();
MyNODE* p = c->gethead();
while(p) {
//Airport temp2 = All_airports[p->getkey() - 1];
//temp2.printnetwork();
if (p->getkey() == b) { return true; }
//printf("airport %d is visited? %d\n", p->getkey(), temp2.getVISITED());
if (All_airports[p->getkey() - 1].getVISITED()==false) {
All_airports[p->getkey() - 1].T_VISITED();
//printf("not implimented\n"); return false;
//printf("\nairport %d is now marked as visited -------%d\n", p->getkey(), temp2.getVISITED());
if (dfs(p->getkey(), b) == true) {
return true;
}
}
p = p->getNextp();
}
return false;
}
void DepthfirstsearchH(int x, int y) {
for (int i = 0; i < CAPACITY; i++) {All_airports[i].F_VISITED();}
if (dfs(x, y)) {
printf("you can get from airport %d to airport %d in one or more flights\n",x,y);
}
else{ printf("you can't get from airport %d to airport %d in one or more flights\n", x, y); }
}
// The main loop for reading in input
void 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)
exit(1);
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
}
}
void showCommands()
{
printf("The commands for this project are:\n");
printf(" q \n");
printf(" ? \n");
printf(" # \n");
printf(" t <int1> <int2> \n");
printf(" r <int> \n");
printf(" i <int1> <int2> \n");
printf(" d <int1> <int2> \n");
printf(" l \n");
printf(" f <filename> \n");
}
void 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;
}
if (val2 > CAPACITY || val1 > CAPACITY) { printf("Not valid input\n"); return; }
if (val2 < 1 || val1 < 1) { printf("Not valid input\n"); return; }
printf("Performing the Travel Command from %d to %d\n",
val1, val2);
DepthfirstsearchH(val1, val2);
return;
}
void 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);
Free_TN();
CAPACITY = val1;
All_airports = new Airport[CAPACITY];
printf("printing travelnetwork for check on resize\n");
List_TN();
}
void 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;
}
// 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 > CAPACITY || val1 > CAPACITY) { printf("Not valid input\n"); return; }
if (val2 < 1 || val1 < 1) { printf("Not valid input\n"); return; }
printf("creating edge from %d to %d\n",
val1, val2);
for (int i = 0; i < CAPACITY; i++) {
Airport temp = All_airports[i];
if (i+1 == val1) { temp.insert_edge(val2); return; }
}
}
void 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("deleting edge from %d to %d\n",
val1, val2);
if (val2 > CAPACITY || val1 > CAPACITY) { printf("Not valid input\n"); return; }
if (val2 < 1 || val1 < 1) { printf("Not valid input\n"); return; }
for (int i = 0; i < CAPACITY; i++) {
Airport temp = All_airports[i];
//printf("Airports which can be reached from Airport %d directly\n", i + 1);
if (i+1 == val1) { temp.del_edge(val2); return; }
}
}
void doList()
{
List_TN();
}
void doFile()
{
// get a filename from the input
char* fname = strtok(NULL, " \n\t");
if (fname == NULL)
{
printf("Filename expected\n");
return;
}
printf("Performing the File command with file: %s\n", fname);
int x = 0;
int i = 0;
for ( i = 0; i < carrCAP+1; i++) {
if (i == carrCAP) { grow(); }
if (filearr[i] != NULL && strcmp(filearr[i], fname) == 0) { x = 0; printf("file has already been opened\n"); break; }
if (filearr[i] == NULL) { x = 1; filearr[i] = fname; break; }
}
if(x){ FILE* newfile= fopen(fname, "r");
processCommandLoop(newfile);
fclose(newfile);
if (strcmp(filearr[i], fname) == 0) {filearr[i] = NULL;}
}
// next steps: (if any step fails: print an error message and return )
// 1. verify the file name is not currently in use
// 2. open the file using fopen creating a new instance of FILE*
// 3. recursively call processCommandLoop() with this new instance of FILE* as the parameter
// 4. close the file when processCommandLoop() returns
}
};
int main(int argc, char** argv)
{
// set up the variable inFile to read from standard input
FILE* inFile = stdin;
// set up the data needed for the airport adjcency list
TravelNetwork airportData;
// call the method that reads and parses the input
airportData.processCommandLoop(inFile);
//airportData.List_TN();
airportData.Free_TN();
airportData.Free_filearr();
printf("Goodbye\n");
return 1;
}