2
2
3
3
#define LINE_SIZE (TIME_STR_SIZE * 2 + 2)
4
4
5
- struct file_info {
5
+ typedef struct file_info {
6
6
size_t size ;
7
7
int data [][2 ];
8
- };
8
+ } FileInfo ;
9
9
static bool is_file_valid (char * f )
10
10
{
11
11
struct stat fs ;
@@ -18,14 +18,14 @@ static bool is_file_valid(char *f)
18
18
return true;
19
19
}
20
20
21
- static struct file_info * load_data (char * filename )
21
+ static FileInfo * load_data (char * filename )
22
22
{
23
23
FILE * fp ;
24
24
char line [LINE_SIZE ];
25
25
char d [TIME_STR_SIZE ], a [TIME_STR_SIZE ];
26
26
int m ;
27
27
bool read_error = false, data_error = false;
28
- struct file_info * fi = NULL ; /* NULL so first realloc is just like malloc() */
28
+ FileInfo * fi = NULL ; /* NULL so first realloc is just like malloc() */
29
29
size_t l ;
30
30
31
31
if (!is_file_valid (filename ))
@@ -37,7 +37,7 @@ static struct file_info *load_data(char *filename)
37
37
}
38
38
for (l = 0 ;fgets (line , sizeof (line ), fp ) != NULL ; l ++ )
39
39
{
40
- if (!(fi = realloc (fi , sizeof (struct file_info ) + (sizeof (int (* )[2 ])) * (l + 1 )))) {
40
+ if (!(fi = realloc (fi , sizeof (FileInfo ) + (sizeof (int (* )[2 ])) * (l + 1 )))) {
41
41
fprintf (stderr , "realloc: %s:%d\n" , __FILE__ , __LINE__ );
42
42
exit (EXIT_FAILURE );
43
43
}
@@ -72,11 +72,27 @@ static struct file_info *load_data(char *filename)
72
72
}
73
73
return fi ;
74
74
}
75
- void find_closest_flight (int requested_departure , int * departure_time , int * arrival_time )
75
+ static void overnight_search (FileInfo * flights , int requested_departure , int * departure , int * arrival )
76
+ {
77
+
78
+ /* adjust first flight in the morning to be
79
+ * a number 'larger than midnight' to
80
+ * simplify the comparison.
81
+ */
82
+ int morning_departure = flights -> data [0 ][0 ] + (24 * 60 );
83
+ int night_deaparture = flights -> data [flights -> size - 1 ][0 ];
84
+ if (requested_departure < flights -> data [0 ][0 ])
85
+ requested_departure += 24 * 60 ;
86
+
87
+ * departure = flights -> data [0 ][0 ];
88
+ * arrival = flights -> data [0 ][1 ];
89
+ if (abs (requested_departure - night_deaparture ) < abs (requested_departure - morning_departure )) {
90
+ * departure = night_deaparture ;
91
+ * arrival = flights -> data [flights -> size - 1 ][1 ];
92
+ }
93
+ }
94
+ static void single_day_search (FileInfo * flights , int requested_departure , int * departure , int * arrival )
76
95
{
77
- struct file_info * flights = load_data (FILE_PATH );
78
- int diff ;
79
- size_t f ;
80
96
/* start as max possible diff */
81
97
/*
82
98
* for each entry in departures list:
@@ -88,14 +104,23 @@ void find_closest_flight(int requested_departure, int *departure_time, int *arri
88
104
* store the difference as 'mininum_difference' for use in next comparison
89
105
* store the array index as 'closest' for use in accessing the flight info
90
106
*/
91
- int minimum_difference = 23 * 60 + 59 ;
107
+ int diff , minimum_difference = 23 * 60 + 59 ;
108
+ size_t f ;
92
109
for (f = 0 ; f < flights -> size ; f ++ ) {
93
110
diff = abs (requested_departure - flights -> data [f ][0 ]);
94
111
if (diff < minimum_difference ) {
95
112
minimum_difference = diff ;
96
- * departure_time = flights -> data [f ][0 ];
97
- * arrival_time = flights -> data [f ][1 ];
113
+ * departure = flights -> data [f ][0 ];
114
+ * arrival = flights -> data [f ][1 ];
98
115
}
99
116
}
117
+ }
118
+ void find_closest_flight (int requested_departure , int * departure_time , int * arrival_time )
119
+ {
120
+ FileInfo * flights = load_data (FILE_PATH );
121
+ if (requested_departure < flights -> data [0 ][0 ] || requested_departure > flights -> data [flights -> size - 1 ][0 ])
122
+ overnight_search (flights , requested_departure , departure_time , arrival_time );
123
+ else
124
+ single_day_search (flights , requested_departure , departure_time , arrival_time );
100
125
free (flights );
101
126
}
0 commit comments