-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux_c_command.c
153 lines (131 loc) · 5.35 KB
/
aux_c_command.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
/* IAED-23 - ist1106635 - project2 ---------------------------------------------
* File: aux_c_command.c
* Author: Tiago Branquinho
* Description: a file with auxiliary functions to handle the command c (routes)
* and complement the main program that manages a bus network
------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "constants.h"
#include "prototypes.h"
/*------------------------------------------------------------------------------
* Function: create_route
* Description: function that creates a route given by the user
* Output: returns TRUE if the route was created successfully
* returns ERROR if it ran out of memory
------------------------------------------------------------------------------*/
int create_route(char **arguments, Route **routes, int *route_num) {
int length = strlen(arguments[0]);
if (*route_num == 0) {
*routes = (Route *) malloc(sizeof(Route));
}
else {
*routes = (Route *) realloc(*routes, ((*route_num)+1)*sizeof(Route));
}
if (*routes == NULL)
return ERROR;
(*routes)[*route_num].name = malloc(sizeof(char) * (length + 1));
if ((*routes)[*route_num].name == NULL)
return ERROR;
strcpy((*routes)[*route_num].name, arguments[0]);
(*routes)[*route_num].stops_number = 0;
(*routes)[*route_num].cost = 0;
(*routes)[*route_num].duration = 0;
(*routes)[*route_num].first_stop = NULL;
(*routes)[*route_num].last_stop = NULL;
(*routes)[*route_num].first_connection = NULL;
(*routes)[*route_num].last_connection = NULL;
(*route_num)++;
return TRUE;
}
/*------------------------------------------------------------------------------
* Function: get_stops_route
* Description: function that prints all the stops in a certain route from
* start to end
* Output: returns TRUE if the route exists, FALSE otherwise
------------------------------------------------------------------------------*/
int get_stops_route(char **arguments,
Route **routes, int route_num) {
int i;
int exists = FALSE;
Linked *aux;
for (i = 0; i < route_num; i++) {
if (strcmp((*routes)[i].name, arguments[0]) == EQUAL) {
exists = TRUE;
if ((*routes)[i].stops_number == 0)
break;
aux = (*routes)[i].first_connection;
printf("%s", (*routes)[i].first_stop);
while (aux != (*routes)[i].last_connection) {
printf(", %s", aux->spec_connection.final_stop);
aux = aux->next;
}
printf(", %s\n", (*routes)[i].last_stop);
break;
}
}
return exists;
}
/*------------------------------------------------------------------------------
* Function: get_inverted_stops_route
* Description: This function prints all the stops in a given route in
* reverse order, starting from the end and ending at the start
* Output: doesn't return anything
------------------------------------------------------------------------------*/
void get_inverted_stops_route(char **arguments,
Route **routes,
int route_num) {
int i;
Linked *aux;
/*it's the same as the function before, but we go from end to beginning*/
for (i = 0; i < route_num; i++) {
if (strcmp((*routes)[i].name, arguments[0]) == EQUAL) {
if ((*routes)[i].stops_number == 0)
break;
aux = (*routes)[i].last_connection;
printf("%s", (*routes)[i].last_stop);
while(aux != (*routes)[i].first_connection) {
printf(", %s", aux->spec_connection.initial_stop);
aux = aux->prev;
}
printf(", %s\n", (*routes)[i].first_stop);
}
}
}
/*------------------------------------------------------------------------------
* Function: is_inverted
* Description: function that checks if the user inputed the command "inverso"
* or abbreviations of it
* Output: returns TRUE if the input is in the correct format, FALSE otherwise
------------------------------------------------------------------------------*/
int is_inverted(char argument[]) {
int i;
int length = strlen(argument);
if ((length < INV_LEN ) || (length > INVERSO_LEN))
return FALSE;
for (i = 0; i < length; i++) {
if (INVERSO[i] != argument[i]) /*checking one by one*/
return FALSE;
}
return TRUE;
}
/*------------------------------------------------------------------------------
* Function: print_route_description
* Description: function that prints the description of a certain route
* Output: doesn't return anything
------------------------------------------------------------------------------*/
void print_route_description(Route spec_route) {
if (spec_route.stops_number == 0) {
printf("%s %d %.2f %.2f\n", spec_route.name,
spec_route.stops_number, spec_route.cost,
spec_route.duration);
}
else {
printf("%s %s %s %d %.2f %.2f\n", spec_route.name,
spec_route.first_stop, spec_route.last_stop,
spec_route.stops_number, spec_route.cost,
spec_route.duration);
}
}