void prompt(void) { while(true) { std::string city_name, hop; std::cout << "City name: "; std::getline(std::cin, city_name); std::cout << "Hop: "; std::getline(std::cin, hop); bstnode *n = bst->get_node_by_key(city_name); if(n == NULL) std::cout << "Error: City name '" << city_name << "' not found." << std::endl; else { int city_code = n->element->value; g.traverse(city_code, atoi(hop.c_str())); } } }
int load(const int argc, const char *argv[]) { /*************************** * CHECK IF IS FILE EXISTS * ***************************/ if(argc < 2) { fprintf(stdout, "Warning: Resource file not loaded.\nUsage: %s [path_of_file]\n", argv[0]); return 1; } parser fp; if(fp.open(argv[1]) != true) { fprintf(stdout, "Error: File '%s' not found.\n", argv[1]); return 2; } clock_t begin, end; double elapsed_secs = 0; /*************************** * READ FILE * ***************************/ std::cout << "Step 1 of 2: Read a file, add to the vertex of the graph, add to the BST (together) . . . "; begin = clock(); fp.parse( [&] (const std::string &line) { std::string arr[5]; fp.divide_by_delimiter(arr, line, 5, '\t'); /* arr[0] = StateAbb arr[1] = PlaceName arr[2] = longitude arr[3] = latitude arr[4] = DistancetoCapitalCity_km */ std::string city_name = arr[1]; double longitude, latitude; try { longitude = stod(arr[2]); latitude = stod(arr[3]); } catch(const std::invalid_argument &e) { return; /* Invalid line, so we should not add this line */ } /* Add to graph */ int key = g.add_vertex(graphelement(arr[1])); if(key == -1) /* Duplicate city name */ { char *temp = new char[5](); for(int i = 2; ; ++i) /* suffix start with #2 */ { std::snprintf(temp, 5, "%d", i); if((key = g.add_vertex(graphelement(std::string(city_name + " #" + temp)))) != -1) { city_name = city_name + " #" + temp; break; } } delete temp; } /* Add to BST */ bstelement *element = new bstelement(city_name, key, longitude, latitude); bst->add(element); } ); end = clock(); std::cout << "Finished (" << elapsed_secs(begin, end) << "sec)" << std::endl; //bst->printall(); std::cout << "Step 2 of 2: Add the edges to each vertex . . . "; begin = clock(); double lat1, lon1, lat2, lon2; bst->traverse_level_order( [&] (bstnode *node) { bst->binary_search_tree::traverse_level_order( [&] (bstnode *n) { if(10000 >= calc_distance(node->element->latitude, node->element->longitude, n->element->latitude, n->element->longitude)) { if(node->element->latitude < n->element->latitude && node->element->longitude < n->element->longitude) { bool is_connected = false; g.traverse(n->element->value, [&] (graph<graphelement>::node* edgn) { if(edgn->key == node->element->value) is_connected = true; } ); if(! is_connected) g.add_edge(node->element->value, n->element->value); } } } ); } ); end = clock(); std::cout << "Finished (" << elapsed_secs(begin, end) << "sec)" << std::endl; g.print(); }