-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight.cpp
713 lines (599 loc) · 15.3 KB
/
flight.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*Author: <Taylor Roozen>
Program: <flight.cpp>
Last Modified: <11/30/2016>
Description:
The ATC class takes flight information, schedule requests, and flight
adjustment commands, and uses that information to update the flight
schedule for a runway.
Each line entered for a new flight should include the flight number,
flight type (either landing(L) or takeoff(T)) departure location, arrival
locationand flight time. It should be entered in the following format:
<36 L SEA BOS 14:15>.
Each line entered for a schedule request should specify whether the request is
for the most recent flight (R), or a list of flights (L). Then it should list
the flight type. It should be entered in the following format:
<L T>.
Flight adjustments should first list the adjustment type, then follow the
following instructions per adjustment type:
Delay(D) should be followed by the flight number, and the number of minutes
that the flight is delayed by. It should be entered in the following format:
<D 76 15>
Cancellations(C) should be followed by the flight number. It should be entered
in the following format:
<C 92>
Finished(F) should be followed by flight number. It should be entered in the
following format:
<F 32>
All cancellations and finished flights should be entered before delayed flights
so cancelled flight times can be allocated to delayed flights.
*/
#include "flight.h"
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
/*
The consructor ATC() allocates the root of the BST of flights,
and the string vector that stores all of the output strings
until main calls generateLine() to get lines for the output file.
*/
ATC::ATC()
{
flightTimes = new flight;
flightTimes = NULL;
nFT = 0;
nT = 0;
nL = 0;
rT = -1;
rL = -1;
lT = -1;
lL = -1;
schedule.reserve(50);
numS = 0;
}
/*
The deconstructor returns the scheduling tree memory when main goes out of
scope.
*/
ATC::~ATC()
{
deleteTree(flightTimes);
return;
}
/*
generateLine() is a public function that accepts no arguments and returns a
string. When this function is called, it returns the results of users
shedule requests. Each time it is called it returns one line,
and it returns the scheduled lines in the order that they
were requested by the user.
*/
string ATC::generateLine()
{
ostringstream numRequests;
numRequests << schedule.size();
static int lineNum = -2;
lineNum++;
if (lineNum == -1)
return "There were " + numRequests.str() + " requests:";
else if (lineNum >= int(schedule.size()))
return "END";
return schedule[lineNum];
}
/*
inputFlight() is a public function. It accepts a string and an integer
as arguments. It takes the flight information that is entered, and
assigns creates a new flight from the information. The flight
is then entered into the flight schedule.
*/
void ATC::inputFlight(string info, int lineNumber)
{
int id, hour, minute;
char type, colon;
string source, dest;
flight* newF = new flight;
istringstream iss(info);
iss >> id >> type >> source >> dest >> hour >> colon >> minute;
if (iss.fail())
{
cout << "Flight details on line " << lineNumber << " in wrong format, couldn't add." << endl;
return;
}
newF->ID = id;
newF->type = toupper(type);
newF->source = source;
newF->dest = dest;
newF->hour = hour;
newF->minute = minute;
newF->T = hour * 60 + minute;
if (newF->type == 'T')
nT++;
else
nL++;
flightTimes = insert(flightTimes, newF);
}
/*
processCommands() is a public function, it accepts a string and an integer
and does not return anything.
If the first character of the command line
is a 'C', the flight is removed from the flightTimes BST, and the schedule
is updated to reflect the cancelled flight.
If the first character of the command line is an 'F', the
flight is removed from the flightTimes BST, and the schedule is updated to
reflect the cancelled flight.
IF the first character enterd is an 'R', the first unused schedule request
element is filled with the most recent flight information for
the specified type of flight.
If the first charater entered is an 'L', the first nused schedule request
element is filled with a list of all of the flight information for all
the flights of the specified type of flight.
*/
void ATC::processCommands(string command, int lineNumber)
{
char initCommand, flightType, initCh;
string stringGetter;
int id, delay;
flight* affectedFlight, *newF;
initCh = toupper(command[0]);
if (initCh != 'C' && initCh != 'F' && initCh != 'D' && initCh != 'R' && initCh != 'L')
{
cout << "Disallowed command request on line " << lineNumber << " unable to process." << endl;
return;
}
switch (initCh)
{
case 'C':
{
istringstream iss(command);
iss >> initCommand >> id;
if (iss.fail())
{
cout << "Disallowed command request on line " << lineNumber << " unable to process." << endl;
return;
}
affectedFlight = getFlight(flightTimes, id);
if (affectedFlight == NULL)
{
cout << "Flight on line " << lineNumber << " does not exist, unable to process." << endl;
return;
}
if (affectedFlight->type == 'T')
{
flightTimes = removeFlight(flightTimes, affectedFlight->T);
nT--;
remakeSchedule('T');
}
else
{
flightTimes = removeFlight(flightTimes, affectedFlight->T);
nL--;
remakeSchedule('L');
}
break;
}
case 'F':
{
istringstream iss1(command);
iss1 >> initCommand >> id;
if (iss1.fail())
{
cout << "Disallowed command request on line " << lineNumber << " unable to process." << endl;
return;
}
affectedFlight = getFlight(flightTimes, id);
if (affectedFlight == NULL)
{
cout << "Flight on line " << lineNumber << " does not exist, unable to process." << endl;
return;
}
if (affectedFlight->type == 'T')
{
flightTimes = removeFlight(flightTimes, affectedFlight->T);
nT--;
remakeSchedule('T');
}
else
{
flightTimes = removeFlight(flightTimes, affectedFlight->T);
nL--;
remakeSchedule('L');
}
break;
}
case 'D':
{
newF = new flight;
istringstream iss2(command);
iss2 >> initCommand >> id >> delay;
if (iss2.fail())
{
cout << "Disallowed command request on line " << lineNumber << " unable to process." << endl;
return;
}
affectedFlight = getFlight(flightTimes, id);
if (affectedFlight == NULL)
{
cout << "Flight on line " << lineNumber << " does not exist, unable to process." << endl;
return;
}
copyFlight(affectedFlight, newF);
flightTimes = removeFlight(flightTimes, affectedFlight->T);
onDemandAssignment(flightTimes, newF, newF->T + delay);
newF->minute = newF->T % 60;
newF->hour = (newF->T / 60) % 24;
flightTimes = insert(flightTimes, newF);
remakeSchedule(newF->type); //in case a flight is delayed after request for
break;
}
case 'R':
{
istringstream iss3(command);
iss3 >> initCommand >> flightType;
if (iss3.fail())
{
cout << "Disallowed command request on line " << lineNumber << " unable to process." << endl;
return;
}
if (flightType == 'T')
{
schedule.push_back(getString(getArray('T', min(1,nT)),min(1,nT)));
rT = numS;
numS++;
}
else
{
schedule.push_back(getString(getArray('L',min(1, nL)), min(1,nL)));
rL = numS;
numS++;
}
break;
}
case 'L':
{
istringstream iss4(command);
iss4 >> initCommand >> flightType;
if (iss4.fail())
{
cout << "Disallowed command request on line " << lineNumber << " unable to process." << endl;
return;
}
if (flightType == 'T')
{
schedule.push_back(getString(getArray('T', nT), nT));
lT = numS;
numS++;
}
else
{
schedule.push_back(getString(getArray('L', nL), nL));
lL = numS;
numS++;
}
break;
}
}
}
//PRIVATE FUNCTIONS START HERE
/*
insert() accepts a new flight as an argument, and
the flightTimes tree. It inserts the new flight into the
flight schedule recursively.
*/
ATC::flight* ATC::insert(flight* tree, flight* newF)
{
if (tree == NULL)
{
newF->left = NULL;
newF->right = NULL;
return newF;
}
if (newF->T < tree->T)
tree->left = insert(tree->left, newF);
else
tree->right = insert(tree->right, newF);
return tree;
}
/*
copyFlight() takes two flights as arguments, and copys the information
from the first flight into the second flight.
*/
void ATC::copyFlight(flight* flight1, flight* flight2)
{
flight2->ID = flight1->ID;
flight2->type = flight1->type;
flight2->source = flight1->source;
flight2->dest = flight1->dest;
flight2->hour = flight1->hour;
flight2->minute = flight1->minute;
flight2->T = flight1->T;
}
/*
getFlight() accepts the flightTimes BST and a flight number as arguments.
It does a tree traversal and when it finds the node with the requested
flight number, it returns that node.
*/
ATC::flight* ATC::getFlight(flight* tree, int flightID)
{
flight* l;
flight* r;
if(tree == NULL||tree->ID==flightID)
return tree;
l = getFlight(tree->left, flightID);
r = getFlight(tree->right, flightID);
if (l != NULL)
return l;
else if (r != NULL)
return r;
else
return NULL;
}
/*
onDemandAssignment() accepts the flightTimes BST a flight and the adjusted
minimum time that a delayed flight can takeoff or land based on the delay
request. The tree is searched until the first open time slot with 3 minutes
of buffer on either side of it is found. It then sets the new takeoff/
landing time of the specified flight to the new time slot.
*/
void ATC::onDemandAssignment(flight* node, flight* f, int minTime)
{
flight* big;
while (minTime < node->T && node->left != NULL)
{
node = node->left;
}
if (node->left == NULL && node->T - 3 >= minTime)
{
f->T = minTime;
return;
}
big = successor(node);
while (big != NULL)
{
if (node->T + 3 < minTime)
{
if (big->T - 3 >= minTime)
{
f->T = minTime;
return;
}
}
else if (big->T - 6 >= node->T)
{
f->T = node->T + 3;
return;
}
node = big;
big = successor(node);
}
f->T = max(node->T + 3, minTime);
}
/*
getArray takes a char and an integer as arguments, and returns a flight array.
It compiles a list of flights of a given type. The size of the list is
specified by the integer input. It enters flights from soonest time
to last time.
*/
ATC::flight* ATC::getArray(char typ, int numFlights)
{
flight* f, *arr;
int n = 0;
if (numFlights == 0)
return NULL;
arr = new flight[numFlights];
f = minimum(flightTimes);
while (n < numFlights)
{
if (f->type == typ)
{
arr[n] = *f;
n++;
}
f = successor(f);
}
return arr;
}
/*
getString() accepts an flight array (from getArray()) and an integer
which represents the number of flights to be added to a schedule line.
It returns a string, which is a line of flight details.
*/
string ATC::getString(flight* fArr, int numFlights)
{
string returner;
ostringstream oss;
if (fArr == NULL)
return "No flights exist for this request";
for (int i = 0; i<numFlights; i++)
{
oss << fArr[i].ID << " " << fArr[i].type;
oss << " " << fArr[i].source << " ";
oss << fArr[i].dest << " " << fArr[i].hour;
oss << ":" << setw(2) << setfill('0') << fArr[i].minute;
if (i != numFlights - 1)
oss << ";";
}
delete []fArr;
returner = oss.str();
oss.str(string());
oss.clear();
return returner;
}
/*
remakeSchedule() accepts a flight type int as an argument.
It enables command requests to be taken before flight inputs.
It removes all of the schedule strings from the vector that they're stored in
up to and including the first instance of a schdule request of the specified type.
*/
void ATC::remakeSchedule(int typ)
{
string* temp;
int vecSize = int(schedule.size());
int recentType, listType, numFlights, j;
if (typ == 'T')
{
recentType = rT;
listType = lT;
numFlights = nT;
temp = new string[vecSize];
}
else
{
recentType = rL;
listType = lL;
numFlights = nL;
temp = new string[vecSize];
}
if (recentType >= 0)
{
j = 0;
for (int i = vecSize - 1; i > recentType; i--)
{
temp[j] = schedule.at(i);
schedule.pop_back();
j++;
}
schedule.pop_back();
schedule.push_back(getString(getArray(typ, 1), 1));
for (int i = j-1; i >= 0; i--)
{
schedule.push_back(temp[i]);
}
}
if (listType >= 0)
{
j = 0;
for (int i = vecSize - 1; i > listType; i--)
{
temp[j] = schedule.at(i);
schedule.pop_back();
j++;
}
schedule.pop_back();
schedule.push_back(getString(getArray(typ, numFlights), numFlights));
for (int i = j-1; i >= 0; i--)
{
schedule.push_back(temp[i]);
}
}
delete[]temp;
}
/*
deleteTree() does a complete traversal and deletion of the BST. It's
used by the destructor.
*/
void ATC::deleteTree(flight * tree)
{
if (tree == NULL)
return;
deleteTree(tree->left);
deleteTree(tree->right);
delete tree;
return;
}
/*
printTraverse is used for debugging (in case you need it...you shouldn't)
*/
void ATC::printTraverse(flight* tree)
{
if (tree == NULL)
return;
printTraverse(tree->left);
cout << tree->T << endl;
printTraverse(tree->right);
return;
}
/*
removeFlight deletes a flight of specified flight time from the fightTimes
BST.
*/
ATC::flight* ATC::removeFlight(flight* node, int t)
{
flight* walk;
if (node == NULL)
return NULL;
if (node->T > t)
{
node->left = removeFlight(node->left, t);
}
else if (node->T < t)
{
node->right = removeFlight(node->right, t);
}
else
{
if (node->left == NULL && node->right == NULL)
{
delete node;
return NULL;
}
else if (node->left == NULL)
{
walk = node->right;
delete node;
return walk;
}
else if (node->right == NULL)
{
walk = node->left;
delete node;
return walk;
}
walk = successor(node);
copyFlight(walk, node);
node->right = removeFlight(node->right, walk->T);
}
return node;
}
/*
successor() is a standard BST successor equation. It takes a flight as an
argument. It finds the node with the flight time following the flight time
of the specified flight.
*/
ATC::flight* ATC::successor(flight* node)
{
flight* walk;
flight* returner;
if (node->right != NULL)
return minimum(node->right);
returner = NULL;
walk = flightTimes;
while (walk != NULL)
{
if (walk->T > node->T)
{
returner = walk;
walk = walk->left;
}
else if (walk->T < node->T)
{
walk = walk->right;
}
else
{
return returner;
}
}
return returner;
}
/*
minimum() finds the flight with the lowest flight time
in the flightTimes BST.
*/
ATC::flight* ATC::minimum(flight * node)
{
if (node->left == NULL)
return node;
return minimum(node->left);
}
/*
maximum() finds the flight with the highest flight time
in the flightTimes BST.
*/
ATC::flight* ATC::maximum(flight * node)
{
if (node->right == NULL)
return node;
return maximum(node->right);
}