33 #define KEYSIZ BUFSIZ/4
34 #define ERRSIZ BUFSIZ/4
35 #define COMMENTCHAR '#'
36 #define MYFILLCHAR '-'
49 #define MAIN_MENU_ROW "--- EU CITY CRISS-CROSS TOUR DEMO ---\nMENU: 0=Exit 1=Make_Tour 2=Min_Span_Tree 3=Print_Graph\nSelection"
53 void my_destroy(
void *data);
void print(
const void *data);
54 int my_match(
const void *,
const void *);
55 int my_cmp(
const void *key1,
58 void vtx_prt(
const void *vtxdata);
59 void edge_prt(
const void *edgedata);
62 void make_tour(
Graph gr);
63 void min_span_tree(
Graph gr);
64 void final_status(
Graph gr);
67 int read_graphdata(
Graph gr,
char *filename);
68 int read_vertices(
Graph gr, FILE *fp);
69 int read_edges(
Graph gr, FILE *fp);
71 void error_mess(
const char *str);
72 void print_vertices(
Graph gr);
73 void prt_fill(
char *str,
char fillchar,
80 void my_destroy(
void *data)
94 return strchr(
"YyNn", ch) ? 1 : 0;
98 void printvtx(
const void *data)
106 printf(
"%02d", mcd->id);
110 void printedge(
const void *data)
118 printf(
"%02d|%-3.3s|", mcd->id, mcd->name);
122 int my_match(
const void *k1,
const void *k2)
132 return mcd1->id == mcd2->id;
136 int my_cmp(
const void *key1,
const void *key2)
146 return mcd1->id - mcd2->id;
149 int read_graphdata(
Graph gr,
char *filename)
152 char errmess[ERRSIZ];
154 FILCHK((fp = fopen(filename,
"rt")));
156 if (read_vertices(gr, fp) != OK)
158 sprintf(errmess,
"\nError when reading data(=vertices) from file %s!", filename);
166 if (read_edges(gr, fp) != OK)
168 sprintf(errmess,
"\nError when reading data(=edges) from file %s!", filename);
180 int read_vertices(
Graph gr, FILE *fp)
182 char buf[BUFSIZ], bufcopy[BUFSIZ], tmp[KEYSIZ];
183 char *commentptr, *pc;
184 char errmess[ERRSIZ];
191 while (fgets(buf, BUFSIZ, fp) != NULL)
194 if ((commentptr = strchr(buf, COMMENTCHAR)) != NULL)
198 strtrim(buf, isspace, isspace);
204 strcpy(bufcopy, buf);
207 pc = strtok(buf,
",;");
215 vtxdata->
data = my_city;
219 strtrim(tmp, isspace, isspace);
224 sprintf(errmess,
"\nData error - when reading key data(=%s) - on this line:\n%s",
235 my_city->id = atoi(tmp);
238 pc = strtok(NULL,
",;");
244 strcpy(my_city->name, pc);
245 strtrim(my_city->name, isspace, isspace);
249 sprintf(errmess,
"\nData missing - when reading data(=%s) - on this line:\n%s",
261 sprintf(errmess,
"\nError when inserting vertex data into graph - from this line:\n%s", bufcopy);
274 int read_edges(
Graph gr, FILE *fp)
277 char buf[BUFSIZ], bufcopy[BUFSIZ], tmp[KEYSIZ];
278 char *commentptr, *pc;
279 char errmess[ERRSIZ];
289 tmpvtxdata.data = &cty_data;
290 tmpedgedata.data = &cty_data2;
293 while (fgets(buf, BUFSIZ, fp) != NULL)
296 if ((commentptr = strchr(buf, COMMENTCHAR)) != NULL)
300 strtrim(buf, isspace, isspace);
306 strcpy(bufcopy, buf);
309 pc = strtok(buf,
",;");
313 strtrim(tmp, isspace, isspace);
318 sprintf(errmess,
"\nData error - when reading key data(=%s) - on this line:\n%s",
325 cty_data.id = atoi(tmp);
328 pc = strtok(NULL,
",;");
333 sprintf(errmess,
"\nData missing - when reading data - on this line:\n%s", bufcopy);
342 while ((pc = strtok(NULL,
",;")) != NULL)
352 edata->
data = mycitydata;
356 strtrim(tmp, isspace, isspace);
361 sprintf(errmess,
"\nData error - when reading key data(=%s) - on this line:\n%s",
370 mycitydata->id = atoi(tmp);
374 cty_data2.id = mycitydata->id;
378 assert(vtxnode!=NULL);
389 strtrim(tmp, isspace, isspace);
393 sprintf(errmess,
"\nData error - when reading edge weight data(=%s) - on this line:\n%s", tmp, bufcopy);
399 edata->weight = atof(tmp);
402 if ((retval =
GRAPHinsedge(gr, &tmpvtxdata, edata)) != OK)
404 sprintf(errmess,
"\nError when inserting edge from %d to %d - on this line:\n%s",
405 cty_data.id, mycitydata->id, bufcopy);
419 sprintf(errmess,
"\nData corrupt (wrong number of fields?) - on this line:\n%s", bufcopy);
438 printroute(vtxdata->parent);
441 printf(
"%02d %-10.10s --> %.1f km\n", cty_data->id, cty_data->name, vtxdata->distance);
444 void error_mess(
const char *str)
446 fprintf(stderr,
"%s\n", str);
449 void vtx_prt(
const void *vtxdata)
462 printf(
"%02d %-12.12s", cty_data->id, cty_data->name);
466 void edge_prt(
const void *edgedata)
474 printf(
"%02d|%-3.3s|%4.1f|", cty_data->id, cty_data->name, edata->weight);
478 void prt_fill(
char *str,
char fillchar,
int fieldlen)
488 (i - slen) < 0 ? putchar(*pc) : putchar(fillchar);
495 void print_vertices(
Graph gr)
509 printf(
"\n %02d ", cty_data->id);
513 printf(
" %02d ", cty_data->id);
515 prt_fill(cty_data->name, MYFILLCHAR, 11);
520 void make_tour(
Graph gr)
522 int start, dest, nr_of_vtcs, retval;
528 char startname[KEYSIZ];
529 char destname[KEYSIZ];
531 Slist my_destpaths = NULL;
534 printf(
"--- MAKE A TOUR - ENTER START & DESTINATION ---\n");
535 printf(
"\nDestinations:");
538 tmpvtxdata.data = &tmpctydata;
542 start =
read_int(
"Start ", 1, nr_of_vtcs);
544 tmpctydata.id = start;
546 assert(vtxnode != NULL);
550 strcpy(startname, mcd->name);
551 printf(
"Start : %d %-11.11s", start, startname);
554 if ((retval =
ALGOdsp(gr, &tmpvtxdata, &my_destpaths, my_match)) != OK)
556 error_mess(
"Something went wrong in call to 'ALGOdsp()' - bailing out..!");
560 assert(my_destpaths != NULL);
564 dest =
read_int(
"Destination", 1, nr_of_vtcs);
566 tmpctydata.id = dest;
568 assert(vtxnode != NULL);
572 strcpy(destname, mcd->name);
573 printf(
"Destination: %d %-11.11s", dest, destname);
576 printf(
"\nOptimized route:\n");
579 reply =
read_char(
"\nTry another destination (y/n)? ", 0, 0, my_chchk);
581 if (reply ==
'y' || reply ==
'y')
584 printf(
"--- MAKE A TOUR - ENTER NEW DESTINATION ---\n");
585 printf(
"\nDestinations:");
588 printf(
"Start: %d %-11.11s", start, startname);
590 }
while (reply ==
'y' || reply ==
'Y');
596 void min_span_tree(
Graph gr)
601 printf(
"--- MIMIMAL SPANNING TREE ---");
606 void final_status(
Graph gr)
615 char grdatafile[] =
"dsp_data.txt";
622 if ((mygraph =
GRAPHinit(my_match, my_destroy)) == NULL)
624 printf(
"\nFatal error - bailing out...\n!");
628 if (read_graphdata(mygraph, grdatafile) != OK)
630 printf(
"\nFailed to read data from file: %s", grdatafile);
634 menu_choice =
menu(MAIN_MENU_ROW, 0, 3);
642 min_span_tree(mygraph);
652 final_status(mygraph);