I need to know what is the shortest route, in kilometers, between a city of origin and another destination. The cities and distances are loaded into the program, through a file that I have created, in the first option of the menu. The second is that by entering the city of origin and destination, it has to tell me which is the shortest route in kilometers.
This is the code I have:
case 1:
if((fichero = fopen("carreteras.txt", "r")) == NULL) {
fprintf(stderr, "Fallo al intentar abrir el fichero. \n");
} else {
bufferNodo[0] = '#';
fscanf(fichero,"%s",bufferNodo);
numNodo = malloc((strlen(bufferNodo) + 1) * sizeof(char));
strcpy(numNodo, bufferNodo);
while (bufferNodo[0] != '#') {
n = nuevo(n, numNodo);
fscanf(fichero,"%s",bufferNodo);
numNodo = malloc((strlen(bufferNodo) + 1) * sizeof(char));
strcpy(numNodo, bufferNodo);
contNodos++;
}
while (!feof(fichero)) {
fscanf(fichero,"%s",bufferOrigen);
origen = malloc((strlen(bufferOrigen) + 1) * sizeof(char));
strcpy(origen, bufferOrigen);
fscanf(fichero,"%s",bufferDestino);
destino = malloc((strlen(bufferDestino) + 1) * sizeof(char));
strcpy(destino, bufferDestino);
fscanf(fichero,"%s",bufferDistancia);
distancia = malloc((strlen(bufferDistancia) + 1) * sizeof(char));
strcpy(distancia, bufferDistancia);
if((aux_origen = buscar(n, origen)) != NULL) {
if((aux_destino = buscar(n, destino)) != NULL) {
arco(aux_origen, aux_destino, distancia);
} else {
aux_destino = buscar(n, destino);
arco(aux_origen, aux_destino, distancia);
}
}
printf("\nCiudad origen: %s\nCiudad destino: %s\nDistancia: %s\n", origen, destino, distancia);
printf("\n--------------------------");
}
}
fclose(fichero);
break;
Thanks.
What you have to do is in the case when you read the lines that must have the format abc save it in an array path[][]
(I think it's like this an example I say
). Then apply the floydWarshall algorithm as a function as follows:
You do this at the end of case 1 or at the beginning of 2 as you like. You create a flag to know if you have called the function in both case 1 and 2 (by flag we mean bool but since it does not exist in c, do it with an integer 1-true & 0-false). You make another function to know the distance, you simply pass it the origin and destination parameters and return path in those variables. I hope that now everything is clearer and sorry for yesterday's answer was wrong.