I have coded a program that will will display a route from one town to
another, which was fine. Now my tutor has asked if i could include
distance and time taken to complete journey. I have no idea how to
alter my code to include this.
I was hoping someone could give me some advice how i should go about
this, as i am at a total loss.
Here is all the code i have produce so far
% car.pl
connected(From, From, [From], _):- !.
connected(From, To, [From| Way], Been):-
( no_stop(From, Through)
;
no_stop(Through, From)
),
not_been_before(Been, Through),
connected(Through, To, Way, Been).
no_stop('al_surra', 'qortuba').
no_stop('al_surra', 'bayan').
no_stop('qortuba', 'mishref').
no_stop('qortuba', 'nizha').
no_stop('mishref', 'rotha').
no_stop('mishref', 'yarmouk').
no_stop('quain', 'bayan').
no_stop('khaldiyah', 'bayan').
no_stop('khaldiyah', 'rotha').
not_been_before(Way, _) :- var(Way),!.
not_been_before([Been| Way], Am) :-
Been \== Am,
not_been_before(Way, Am).
/* car.c */
#include <stdio.h>
#include <sicstus/sicstus.h>
void write_path(SP_term_ref path)
{
char *text = NULL;
SP_term_ref
tail = SP_new_term_ref(),
via = SP_new_term_ref();
SP_put_term(tail,path);
while (SP_get_list(tail,via,tail))
{
if (text)
printf(" -> ");
SP_get_string(via, &text);
printf("%s",text);
}
printf("\n");
}
int user_main(int argc, char **argv)
{
int rval;
SP_pred_ref pred;
SP_qid goal;
SP_term_ref from, to, path;
/* Initialize Prolog engine. This call looks up SP_PATH in order to
* find the Runtime Library. */
if (SP_FAILURE == SP_initialize(argc, argv, NULL))
{
fprintf(stderr, "SP_initialize failed: %s\n",
SP_error_message(SP_errno));
exit(1);
}
rval = SP_restore("train.sav");
if (rval == SP_ERROR || rval == SP_FAILURE)
{
fprintf(stderr, "Could not restore \"train.sav\".\n");
exit(1);
}
/* car.c */
/* Look up connected/4. */
if (!(pred = SP_predicate("connected",4,"user")))
{
fprintf(stderr, "Could not find connected/4.\n");
exit(1);
}
/* Create the three arguments to connected/4. */
SP_put_string(from = SP_new_term_ref(), "al_surra");
SP_put_string(to = SP_new_term_ref(), "khaldiyah");
SP_put_variable(path = SP_new_term_ref());
/* Open the query. In a development system, the query would look
like:
*
* | ?- connected('al_surra','khaldiyah',X).
*/
if (!(goal = SP_open_query(pred,from,to,path,path)))
{
fprintf(stderr, "Failed to open query.\n");
exit(1);
}
/*
* Loop through all the solutions.
*/
while (SP_next_solution(goal)==SP_SUCCESS)
{
printf("Path: ");
write_path(path);
}
SP_close_query(goal);
exit(0);