Skip to content

Commit 40a7ea5

Browse files
committed
Update parser & algorithm
Update parser to work around line breaks as file ending, update algorithm to print errors when target could not be reached in the defined map
1 parent 2006089 commit 40a7ea5

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

germany_map_false.csv

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1-
Stuttgart, Karlsruhe, 75
2-
Karlsruhe, Stuttgart, 75
3-
Stuttgart, Nürnberg, 210
4-
Nürnberg, Stuttgart, 210
51
Stuttgart, München, 220
62
München, Stuttgart, 220
7-
München, Nürnberg, 170
8-
Nürnberg, München, 170
93
München, Linz, 370
104
Linz, München, 370
11-
Nürnberg, Hamburg, 620
12-
Hamburg, Nürnberg, 620
135
Bonn, Düsseldorf, 70
146
Düsseldorf, Bonn, 70
157
Karlsruhe, Düsseldorf, 350
168
Düsseldorf, Karlsruhe, 350
179
Hamburg, Karlsruhe, 620
1810
Karlsruhe, Hamburg, 620
19-
Karlsruhe, Nürnberg, 250
20-
Nürnberg, Karlsruhe, 250
2111
Nürnberg, Dresden, 310
2212
Dresden, Nürnberg, 310
2313
Stuttgart, Freiburg, 200
2414
Freiburg, Stuttgart, 200
2515
München, Augsburg, 70
2616
Augsburg, München, 70
27-
Nürnberg, Regensburg, 120
28-
Regensburg, Nürnberg, 120
2917
Berlin, Potsdam, 40
3018
Potsdam, Berlin, 40
3119
Hamburg, Bremen, 120

src/logic/algo.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ ROUTE* get_route_min_dist(ROUTE* routes, const int size,
5555
int min_distance = -1;
5656
ROUTE* route = nullptr;
5757
for (int i = 0; i < size; i++) {
58-
if (min_distance < 0 && !city_visited(routes[i].destination.name,
59-
applicable_cities,
60-
cities_size) ||
61-
routes[i].distance > 0 && routes[i].distance < min_distance &&
58+
if (routes[i].distance == 0 && !city_visited(routes[i].destination.name,
59+
applicable_cities,
60+
cities_size) ||
61+
routes[i].distance > 0 && (
62+
min_distance < 0 || routes[i].distance < min_distance) &&
6263
!city_visited(routes[i].destination.name, applicable_cities,
6364
cities_size)) {
64-
min_distance = routes[i].distance;
65+
if (routes[i].distance != 0) {
66+
min_distance = routes[i].distance;
67+
}
6568
route = &routes[i];
6669
}
6770
}
@@ -136,8 +139,8 @@ int dijkstra(
136139
routes, DICT_SIZE, all_cities,
137140
DICT_SIZE);
138141
if (shortest_route == nullptr) {
139-
printf("No path could be found\n");
140-
exit(1);
142+
// No further path can be found here
143+
break;
141144
}
142145
const CITY sr_destination = shortest_route->destination;
143146
const VALUE* value = get_value(dict, sr_destination.name);
@@ -172,9 +175,14 @@ int dijkstra(
172175

173176
// Print route to target
174177
const ROUTE* target_route = get_route_for_city(routes, DICT_SIZE, target);
175-
printf("Destination %s reached:\n", target);
176-
print_route(target_route, start, complete_route, debug);
177-
const int distance = target_route->distance;
178+
int distance = 0;
179+
if (target_route->distance != -1) {
180+
printf("Destination %s reached:\n", target);
181+
print_route(target_route, start, complete_route, debug);
182+
distance = target_route->distance;
183+
} else {
184+
printf("No path could be found\n");
185+
}
178186

179187
// Free mem
180188
for (int i = 0; i < DICT_SIZE; i++) {

src/main.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,18 @@ void calculate_distances(
202202
*complete_route = tmp_complete_route;
203203
strcat(*complete_route, " -> ");
204204
}
205-
if (!reallife) {
206-
printf(
207-
"Estimated price and fuel consumption based on average data (7.7l/km - 1.85€/l):\n"
208-
);
205+
if (distance != 0) {
206+
if (!reallife) {
207+
printf(
208+
"Estimated price and fuel consumption based on average data (7.7l/km - 1.85€/l):\n"
209+
);
210+
}
211+
const double liter =
212+
calculate_fuel_consumption(distance, fuel_efficiency);
213+
calculate_liter_price(liter, price_per_liter);
214+
*complete_distance += distance;
215+
printf("\n");
209216
}
210-
const double liter =
211-
calculate_fuel_consumption(distance, fuel_efficiency);
212-
calculate_liter_price(liter, price_per_liter);
213-
*complete_distance += distance;
214-
printf("\n");
215217
}
216218
}
217219

@@ -311,7 +313,8 @@ int main(const int argc, char* argv[]) {
311313
// calculate distances
312314
calculate_distances(waypoints, waypoints_size, dictionary, reallife, debug,
313315
&complete_route, &complete_distance);
314-
if (strlen(complete_route) > 0 && waypoints_size > 2) {
316+
if (complete_route != nullptr && strlen(complete_route) > 0 && waypoints_size
317+
> 2) {
315318
printf("%s\n", complete_route);
316319
const double liter =
317320
calculate_fuel_consumption(complete_distance, fuel_efficiency);

src/parsing/parser.c

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
int csv_column;
1717
int char_column_index;
18-
CITY *start_city;
19-
CITY *dest_city;
20-
FILE *map_file;
21-
DICTIONARY *dictionary;
18+
CITY* start_city;
19+
CITY* dest_city;
20+
FILE* map_file;
21+
DICTIONARY* dictionary;
2222

2323
/**
2424
* Initializes the global variables for parsing a new csv row.
@@ -45,13 +45,13 @@ void next_column() {
4545
* @param dest_city destination city
4646
* @param dist distance between start and destination city
4747
*/
48-
bool add_connection_to_dict(DICTIONARY *dictionary, const CITY *start_city,
49-
const CITY *dest_city, const int dist) {
48+
bool add_connection_to_dict(DICTIONARY* dictionary, const CITY* start_city,
49+
const CITY* dest_city, const int dist) {
5050
if (start_city == nullptr || dest_city == nullptr) {
5151
return false;
5252
}
5353

54-
VALUE *val = get_value(dictionary, start_city->name);
54+
VALUE* val = get_value(dictionary, start_city->name);
5555
if (val == nullptr) {
5656
val = malloc(sizeof(VALUE));
5757
if (val == nullptr) {
@@ -66,7 +66,7 @@ bool add_connection_to_dict(DICTIONARY *dictionary, const CITY *start_city,
6666
}
6767

6868
const CONNECTION connection = (CONNECTION){
69-
.city = *start_city, .destination = *dest_city, .distance = dist};
69+
.city = *start_city, .destination = *dest_city, .distance = dist};
7070

7171
if (connection_in_values(val, &connection)) {
7272
return true;
@@ -79,7 +79,7 @@ bool add_connection_to_dict(DICTIONARY *dictionary, const CITY *start_city,
7979
/**
8080
* Frees given parameters and closes file stream.
8181
*/
82-
void free_mem(FILE *file, char *val) {
82+
void free_mem(FILE* file, char* val) {
8383
free(val);
8484
fclose(file);
8585
}
@@ -111,7 +111,7 @@ bool malloc_start_dest_city() {
111111
* @param city city to set the name
112112
* @return a bool if the name was set successfully
113113
*/
114-
bool set_city_name(const char *name, CITY *city) {
114+
bool set_city_name(const char* name, CITY* city) {
115115
city->name = malloc(strlen(name) + 1);
116116
if (city->name == nullptr) {
117117
print_error_general("Name of city is NULL");
@@ -127,11 +127,11 @@ bool set_city_name(const char *name, CITY *city) {
127127
* @param val string to convert
128128
* @return the converted integer
129129
*/
130-
int get_distance_from_str(const char *val) {
130+
int get_distance_from_str(const char* val) {
131131
char tmp[char_column_index + 1];
132132
strncpy(tmp, val + '\0', char_column_index);
133133
tmp[char_column_index] = '\0';
134-
return (int) strtoumax(tmp, nullptr, 10);
134+
return (int)strtoumax(tmp, nullptr, 10);
135135
}
136136

137137
/**
@@ -140,7 +140,7 @@ int get_distance_from_str(const char *val) {
140140
* @param current_word
141141
* @return
142142
*/
143-
bool handle_csv_column(const char *current_word) {
143+
bool handle_csv_column(const char* current_word) {
144144
switch (csv_column) {
145145
case 0: {
146146
set_city_name(current_word, start_city);
@@ -189,13 +189,14 @@ bool init_parser(const char path[]) {
189189
* @param current_word string to resize.
190190
* @return true if reallocation succeeded, false otherwise.
191191
*/
192-
bool resize_buffer(int *buf_size, char **current_word) {
193-
if (current_word == nullptr || *current_word == nullptr || buf_size == nullptr) {
192+
bool resize_buffer(int* buf_size, char** current_word) {
193+
if (current_word == nullptr || *current_word == nullptr || buf_size ==
194+
nullptr) {
194195
return false;
195196
}
196197

197198
*buf_size += BUFFER_SIZE;
198-
char *tmp = realloc(*current_word, *buf_size);
199+
char* tmp = realloc(*current_word, *buf_size);
199200
if (tmp == NULL) {
200201
return false;
201202
}
@@ -227,7 +228,7 @@ DICTIONARY* parse(const char path[]) {
227228
return nullptr;
228229
}
229230

230-
char *current_word = malloc(32 * sizeof(char));
231+
char* current_word = malloc(32 * sizeof(char));
231232
if (current_word == NULL || !malloc_start_dest_city()) {
232233
fclose(map_file);
233234
free(dictionary);
@@ -251,6 +252,14 @@ DICTIONARY* parse(const char path[]) {
251252
if (current_char == '\n' || current_char == EOF) {
252253
line_number++;
253254
first_column_printable_char = false;
255+
// if char_column_index is 0 at this point, we have two consecutive line breaks, either skip or abort if not allowed
256+
if (char_column_index == 0) {
257+
if (current_char == EOF) {
258+
break;
259+
} else {
260+
continue;
261+
}
262+
}
254263
const int dist_str = get_distance_from_str(current_word);
255264
if (dist_str == 0) {
256265
print_error_general("Distance has to be 1 or greater");
@@ -259,7 +268,8 @@ DICTIONARY* parse(const char path[]) {
259268

260269
add_connection_to_dict(dictionary, start_city, dest_city, dist_str);
261270

262-
if (start_city == NULL || dest_city == NULL || !malloc_start_dest_city()) {
271+
if (start_city == NULL || dest_city == NULL || !
272+
malloc_start_dest_city()) {
263273
free_mem(map_file, current_word);
264274
return nullptr;
265275
}
@@ -291,7 +301,7 @@ DICTIONARY* parse(const char path[]) {
291301
}
292302

293303
if (is_printable_char(current_char) || first_column_printable_char) {
294-
current_word[char_column_index++] = (char) current_char;
304+
current_word[char_column_index++] = (char)current_char;
295305
first_column_printable_char = true;
296306
}
297307
increment_char_index();

0 commit comments

Comments
 (0)