-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
732 lines (598 loc) · 19.1 KB
/
main.c
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
* File: main.c
* Author: goncrust
* Description: First IAED project - Flight management system between airports.
*/
#include <stdio.h>
#include <string.h>
/* Constants */
/* Max number of airports and flights */
#define AIRPORTS_MAX 40
#define FLIGHTS_MAX 30000
/* Char array sizes */
#define AIRPORTID_SIZE 4
#define COUNTRY_SIZE 31
#define CITY_SIZE 51
#define FLIGHTID_SIZE 7
#define FLIGHTID_LETTERS 2
/* Error messages */
#define DATE_ERROR "invalid date\n"
#define AIRPORTID_NOTFOUND "%s: no such airport ID\n"
#define AIRPORTID_ERROR "invalid airport ID\n"
#define AIRPORTCOUNT_ERROR "too many airports\n"
#define AIRPORTDUP_ERROR "duplicate airport\n"
#define FLIGHTID_ERROR "invalid flight code\n"
#define FLIGHTDUP_ERROR "flight already exists\n"
#define FLIGHTCOUNT_ERROR "too many flights\n"
#define FLIGHTDURATION_ERROR "invalid duration\n"
#define FLIGHTCAP_ERROR "invalid capacity\n"
/* Date and time constants */
#define INITIAL_DAY 1
#define INITIAL_MONTH 1
#define INITIAL_YEAR 2022
#define FLIGHTDURATION_MAX 12
/* Other */
#define FLIGHTCAP_MIN 10
#define FLIGHTCAP_MAX 100
#define AIRPORTADD_MSG "airport %s\n"
/* Structures */
typedef struct {
int day;
int month;
int year;
} Date;
typedef struct {
int hour;
int minute;
} Time;
typedef struct {
Date date;
Time time;
} DateTime;
typedef struct {
char id[AIRPORTID_SIZE];
char country[COUNTRY_SIZE];
char city[CITY_SIZE];
} Airport;
typedef struct {
char id[FLIGHTID_SIZE];
char departure_id[AIRPORTID_SIZE];
char destination_id[AIRPORTID_SIZE];
Date date;
Time time;
Time duration;
int capacity;
} Flight;
/* Enums */
enum months{ Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
/* Date functions */
/* Returns 1 if date d1 is before date d2, otherwise returns 0 */
int date_before(Date d1, Date d2) {
if (d1.year < d2.year) return 1;
else if (d1.year > d2.year) return 0;
if (d1.month < d2.month) return 1;
else if (d1.month > d2.month) return 0;
if (d1.day < d2.day) return 1;
return 0;
}
/* Returns 1 if date d1 is more than 1 year ahead of date d2,
* otherwise returns 0
*/
int one_year_ahead(Date d1, Date d2) {
if (d1.year - d2.year > 1)
return 1;
else if (d1.year - d2.year == 1) {
if (d1.month > d2.month)
return 1;
else if (d1.month == d2.month) {
if (d1.day > d2.day)
return 1;
}
}
return 0;
}
/* Print given date to stdout, with dd-mm-yyyy format */
void print_date(Date date) {
if (date.day < 10)
printf("0%d-", date.day);
else
printf("%d-", date.day);
if (date.month < 10)
printf("0%d-", date.month);
else
printf("%d-", date.month);
printf("%d", date.year);
}
/* Returns 1 if date is invalid, otherwise returns 0.
* A given date is invalid if it is before or more than one year ahead
* of the current date (curr_date).
*/
int invalid_date(Date date, Date curr_date) {
if (date_before(date, curr_date) ||
one_year_ahead(date, curr_date)) {
printf(DATE_ERROR);
return 1;
}
return 0;
}
/* Returns 1 if dates d1 and d2 are equal, otherwise returns 0. */
int equal_dates(Date d1, Date d2) {
return (d1.day == d2.day && d1.month == d2.month && d1.year == d2.year);
}
/* Time functions */
/* Print given time to stdout, with hh:mm format */
void print_time(Time time) {
if (time.hour < 10)
printf("0%d:", time.hour);
else
printf("%d:", time.hour);
if (time.minute < 10)
printf("0%d", time.minute);
else
printf("%d", time.minute);
}
/* Returns 1 if time t1 is before time t2, otherwise returns 0 */
int time_before(Time t1, Time t2) {
if (t1.hour < t2.hour) return 1;
else if (t1.hour > t2.hour) return 0;
if (t1.minute < t2.minute) return 1;
return 0;
}
/* DateTime functions */
/* Returns 1 if date d1 at time t1 is before date d2 at time t2,
* otherwise returns 0.
*/
int date_time_before(Date d1, Time t1, Date d2, Time t2) {
if (d1.year < d2.year) return 1;
else if (d1.year > d2.year) return 0;
if (d1.month < d2.month) return 1;
else if (d1.month > d2.month) return 0;
if (d1.day < d2.day) return 1;
else if (d1.day > d2.day) return 0;
if (time_before(t1, t2)) return 1;
return 0;
}
/* Given a date, time and duration of a flight, calculates the date and
* time of the arrival and returns a structure composed by that values.
*/
DateTime calc_arrival(Date date, Time time, Time duration) {
int month;
DateTime arrival;
time.minute += duration.minute;
if (time.minute > 59) {
time.minute -= 60;
time.hour++;
}
time.hour += duration.hour;
if (time.hour > 23) {
time.hour -= 24;
date.day++;
}
month = date.month;
if (month == Jan || month == Mar || month == May || month == Jul
|| month == Aug || month == Oct || month == Dec) {
if (date.day > 31) {
date.day = 1;
date.month++;
}
} else if (month == Feb) {
if (date.day > 28) {
date.day = 1;
date.month = Mar;
}
} else {
if (date.day > 30) {
date.day = 1;
date.month++;
}
}
if (date.month == 13) {
date.month = 1;
date.year++;
}
arrival.date = date;
arrival.time = time;
return arrival;
}
/* Auxiliar functions */
/* Returns 1 if word1 is alphabetically before word2, otherwise returns 0. */
int alpha_before(char w1[], char w2[], int word_size) {
int i;
for (i = 0; i < word_size; i++) {
if (w1[i] < w2[i])
return 1;
else if (w1[i] > w2[i])
return 0;
}
return 0;
}
/* Returns the number of flights with departure at the airport with given id. */
int airport_flight_count(char id[], Flight flights[], int flight_count) {
int i, total = 0;
for (i = 0; i < flight_count; i++) {
if (strcmp(id, flights[i].departure_id) == 0)
total++;
}
return total;
}
/* Prints to stdout the external representation of an airport, given its id. */
void print_airport_by_id(char id[], Airport airports[], int airport_count,
Flight flights[], int flight_count) {
int i;
for (i = 0; i < airport_count; i++) {
if (strcmp(airports[i].id, id) == 0) {
printf("%s %s %s %d\n", airports[i].id, airports[i].city,
airports[i].country,
airport_flight_count(id, flights, flight_count));
return;
}
}
printf(AIRPORTID_NOTFOUND, id);
}
/* Return 1 if the given str is a valid flight code, otherwise returns 0. */
int valid_flightid(char str[]) {
int i = 0, error = 0;
while (str[i] != '\0') {
if (i < FLIGHTID_LETTERS) {
if (str[i] < 'A' || str[i] > 'Z') {
error = 1;
break;
}
} else {
if (str[i] == '0' && i == FLIGHTID_LETTERS) {
error = 1;
break;
}
if (str[i] < '0' || str[i] > '9') {
error = 1;
break;
}
}
i++;
if (i >= FLIGHTID_SIZE) {
error = 1;
break;
}
}
if (error || i < 3) {
printf(FLIGHTID_ERROR);
return 0;
}
return 1;
}
/* Given a departure and destination airport ids, returns 1 if they are
* registered in the system. Otherwise returns 0.
*/
int flight_airport_notfound(Airport airports[], int airport_count,
char departure_id[], char destination_id[]) {
int i, found_departure = 0, found_destination = 0;
for (i = 0; i < airport_count; i++) {
if (strcmp(departure_id, airports[i].id) == 0) {
found_departure = 1;
}
if(strcmp(destination_id, airports[i].id) == 0) {
found_destination = 1;
}
}
if (!(found_departure)) {
printf(AIRPORTID_NOTFOUND, departure_id);
return 1;
}
if (!(found_destination)) {
printf(AIRPORTID_NOTFOUND, destination_id);
return 1;
}
return 0;
}
/* Reads airport id from stdin and checks if it's in the system.
* Airport id will be stored in id char array.
* Auxiliar function for list_departures and list_arrivals.
*/
void read_id(char id[], Airport airports[], int airport_count) {
int found = 0, i;
char new_line;
scanf("%s%c", id, &new_line);
/* Errors */
for (i = 0; i < airport_count; i++) {
if (strcmp(airports[i].id, id) == 0) {
found = 1;
break;
}
}
if (!found) {
printf(AIRPORTID_NOTFOUND, id);
return;
}
}
/* Command functions */
/* Function for 'a' command (add airport to system) */
int add_airport(Airport airports[], int airport_count) {
int i;
char space, c;
Airport new_airport;
scanf("%s%s%c", new_airport.id, new_airport.country, &space);
i = 0;
while ((c = getchar()) != '\n') {
new_airport.city[i] = c;
i++;
}
new_airport.city[i] = '\0';
/* Errors */
for (i = 0; i < AIRPORTID_SIZE-1; i++) {
if (new_airport.id[i] < 'A' || new_airport.id[i] > 'Z') {
printf(AIRPORTID_ERROR);
return 1;
}
}
if (airport_count >= AIRPORTS_MAX) {
printf(AIRPORTCOUNT_ERROR);
return 1;
}
for (i = 0; i < airport_count; i++) {
if (strcmp(new_airport.id, airports[i].id) == 0) {
printf(AIRPORTDUP_ERROR);
return 1;
}
}
airports[airport_count] = new_airport;
printf(AIRPORTADD_MSG, airports[airport_count].id);
return 0;
}
/* Function for 'l' command (list airports in system) */
void list_airports(Airport airports[], int airport_count,
Flight flights[], int flight_count) {
int sort[AIRPORTS_MAX], i, j, aux;
/* Initialize sort vector */
for (i = 0; i < airport_count; i++) sort[i] = i;
for (i = airport_count-1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (alpha_before(airports[sort[j+1]].id,
airports[sort[j]].id, AIRPORTID_SIZE-1)) {
aux = sort[j];
sort[j] = sort[j+1];
sort[j+1] = aux;
}
}
}
for (i = 0; i < airport_count; i++)
printf("%s %s %s %d\n", airports[sort[i]].id, airports[sort[i]].city,
airports[sort[i]].country,
airport_flight_count(airports[sort[i]].id, flights, flight_count));
}
/* Function for 'l' command (list airports specified by the user) */
void list_airports_specified(Airport airports[], int airport_count,
Flight flights[], int flight_count) {
char id[AIRPORTID_SIZE];
int i = 0, end = 0;
while (!end) {
id[i] = getchar();
if (id[i] == '\n')
end = 1;
if (i == 3) {
id[3] = '\0';
print_airport_by_id(id, airports, airport_count,
flights, flight_count);
i = 0;
continue;
}
i++;
}
}
/* Function for 'v' command (add flight to system) */
int add_flight(Flight flights[], int flight_count, Airport airports[],
int airport_count, Date curr_date) {
int i;
char space, dash, colon, new_line;
Flight new_flight;
/* Airports ID */
scanf("%s%s%s%c", new_flight.id, new_flight.departure_id,
new_flight.destination_id, &space);
/* Departure date */
scanf("%d%c%d%c%d%c", &new_flight.date.day, &dash, &new_flight.date.month,
&dash, &new_flight.date.year, &space);
/* Departure time */
scanf("%d%c%d%c", &new_flight.time.hour, &colon, &new_flight.time.minute,
&space);
/* Duration */
scanf("%d%c%d%c", &new_flight.duration.hour, &colon,
&new_flight.duration.minute, &space);
/* Capacity */
scanf("%d%c", &new_flight.capacity, &new_line);
/* Errors */
if (!valid_flightid(new_flight.id))
return 1;
for (i = 0; i < flight_count; i++) {
if (strcmp(new_flight.id, flights[i].id) == 0 &&
equal_dates(new_flight.date, flights[i].date)) {
printf(FLIGHTDUP_ERROR);
return 1;
}
}
if (flight_airport_notfound(airports, airport_count,
new_flight.departure_id, new_flight.destination_id))
return 1;
if (flight_count >= FLIGHTS_MAX) {
printf(FLIGHTCOUNT_ERROR);
return 1;
}
if (invalid_date(new_flight.date, curr_date))
return 1;
if (new_flight.duration.hour > FLIGHTDURATION_MAX ||
(new_flight.duration.hour == FLIGHTDURATION_MAX &&
new_flight.duration.minute > 0)) {
printf(FLIGHTDURATION_ERROR);
return 1;
}
if (new_flight.capacity < FLIGHTCAP_MIN ||
new_flight.capacity > FLIGHTCAP_MAX) {
printf(FLIGHTCAP_ERROR);
return 1;
}
flights[flight_count] = new_flight;
return 0;
}
/* Function for 'v' command (list flights in system) */
void list_flights(Flight flights[], int flight_count) {
int i;
for (i = 0; i < flight_count; i++) {
printf("%s %s %s ", flights[i].id, flights[i].departure_id,
flights[i].destination_id);
print_date(flights[i].date);
printf(" ");
print_time(flights[i].time);
printf("\n");
}
}
/* Function for 'p' command (list flights in system, with given departure) */
void list_departures(Flight flights[], int flight_count,
Airport airports[], int airport_count) {
char id[AIRPORTID_SIZE];
int selected_indexes[FLIGHTS_MAX], selected_indexes_count = 0, i, j, aux;
read_id(id, airports, airport_count);
/* Select flights with desired departure id */
for (i = 0; i < flight_count; i++) {
if (strcmp(flights[i].departure_id, id) == 0) {
selected_indexes[selected_indexes_count] = i;
selected_indexes_count++;
}
}
/* Sort by date */
for (i = selected_indexes_count-1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (date_time_before(flights[selected_indexes[j+1]].date,
flights[selected_indexes[j+1]].time,
flights[selected_indexes[j]].date,
flights[selected_indexes[j]].time)) {
aux = selected_indexes[j];
selected_indexes[j] = selected_indexes[j+1];
selected_indexes[j+1] = aux;
}
}
}
/* Print */
for (i = 0; i < selected_indexes_count; i++) {
printf("%s %s ", flights[selected_indexes[i]].id,
flights[selected_indexes[i]].destination_id);
print_date(flights[selected_indexes[i]].date);
printf(" ");
print_time(flights[selected_indexes[i]].time);
printf("\n");
}
}
/* Function for 'p' command (list flights in system, with given destination) */
void list_arrivals(Flight flights[], int flight_count,
Airport airports[], int airport_count) {
char id[AIRPORTID_SIZE];
int selected_indexes[FLIGHTS_MAX], selected_indexes_count = 0, i, j, aux;
DateTime arrival_date_time[FLIGHTS_MAX], auxDT;
read_id(id, airports, airport_count);
/* Select flights with desired destination id */
for (i = 0; i < flight_count; i++) {
if (strcmp(flights[i].destination_id, id) == 0) {
selected_indexes[selected_indexes_count] = i;
selected_indexes_count++;
}
}
for (i = 0; i < selected_indexes_count; i++) {
arrival_date_time[i] = calc_arrival(flights[selected_indexes[i]].date,
flights[selected_indexes[i]].time,
flights[selected_indexes[i]].duration);
}
/* Sort by date */
for (i = selected_indexes_count-1; i > 0; i--) {
for (j = 0; j < i; j++) {
if (date_time_before(arrival_date_time[j+1].date,
arrival_date_time[j+1].time,
arrival_date_time[j].date,
arrival_date_time[j].time)) {
aux = selected_indexes[j];
selected_indexes[j] = selected_indexes[j+1];
selected_indexes[j+1] = aux;
auxDT = arrival_date_time[j];
arrival_date_time[j] = arrival_date_time[j+1];
arrival_date_time[j+1] = auxDT;
}
}
}
/* Print */
for (i = 0; i < selected_indexes_count; i++) {
printf("%s %s ", flights[selected_indexes[i]].id,
flights[selected_indexes[i]].departure_id);
print_date(arrival_date_time[i].date);
printf(" ");
print_time(arrival_date_time[i].time);
printf("\n");
}
}
/* Function for 't' command (advance date) */
Date change_date(Date curr_date) {
char dash, new_line;
Date new_date;
scanf("%d%c%d%c%d%c", &new_date.day, &dash, &new_date.month, &dash,
&new_date.year, &new_line);
if (invalid_date(new_date, curr_date))
return curr_date;
print_date(new_date);
printf("\n");
return new_date;
}
/* Main function. Reads command and calls corresponding function. */
int main() {
/* Airports in the system */
Airport airports[AIRPORTS_MAX];
int airport_count = 0;
/* Flights in the system */
Flight flights[FLIGHTS_MAX];
int flight_count = 0;
/* System date */
Date date = { INITIAL_DAY, INITIAL_MONTH, INITIAL_YEAR };
/* if running == 0 program termanted */
/* error is used to store the return of functions (0-success, 1-error) */
int running = 1, error;
/* Main Loop */
while (running) {
/* Read command */
char command, last_char;
scanf("%c%c", &command, &last_char);
switch (command) {
case 'q':
running = 0;
break;
case 'a':
error = add_airport(airports, airport_count);
if (!error)
airport_count++;
break;
case 'l':
if (last_char == ' ')
list_airports_specified(airports, airport_count,
flights, flight_count);
else if (last_char == '\n')
list_airports(airports, airport_count,
flights, flight_count);
break;
case 'v':
if (last_char == ' ') {
error = add_flight(flights, flight_count, airports,
airport_count, date);
if (!error)
flight_count++;
} else if (last_char == '\n') {
list_flights(flights, flight_count);
}
break;
case 'p':
list_departures(flights, flight_count, airports, airport_count);
break;
case 'c':
list_arrivals(flights, flight_count, airports, airport_count);
break;
case 't':
date = change_date(date);
break;
default:
break;
}
}
return 0;
}