// // Graphs // #include #include #include using namespace std ; #include "Graph.h" void Insert(Graph & gr) ; void Dijkstra(Graph & gr) ; char GetMenuChoice(void) ; int main(int argc, char * argv[]){ Graph gr ; char menuChoice = GetMenuChoice() ; while(menuChoice != 'X') { if(menuChoice == '1') Insert(gr) ; else if(menuChoice == '2') Dijkstra(gr) ; else cout << "Invalid Menu Choice." << endl ; menuChoice = GetMenuChoice() ; } cout << "Goodbye!" << endl ; return 0 ; } // Function Definitions // Pre - None // Post - Displays the Menu & Gets the user's choice & Returns it char GetMenuChoice(void) { char menuChoice[500] ; do { cout << "--------------------" << endl ; cout << "Homework 6 MAIN MENU" << endl ; cout << "--------------------" << endl ; cout << "1 - Insert Cities and Roads from File" << endl ; cout << "2 - Shortest Path, Dijkstras Algorithm" << endl ; cout << "X - EXIT PROGRAM" << endl ; cout << "--------------------" << endl ; cout << "Choose an Option (hit enter): " ; cin.getline(menuChoice, 499) ; }while(strlen(menuChoice) != 1) ; return toupper(menuChoice[0]) ; } void Insert(Graph & gr) { string filename ; cout << "Enter a filename: " ; cin >> filename ; cin.get() ; ifstream in ; in.open(filename.c_str(), ios::in) ; int numCities = 0 ; in >> numCities ; int counter = 0 ; while(counter < numCities) { string city ; in >> city ; gr.InsertCity(city) ; // debug cout << "read from file : " << city << endl ; counter++ ; } int numRoads = 0 ; in >> numRoads ; counter = 0 ; while(counter < numRoads) { string city1 ; string city2 ; int distance ; in >> city1 ; in >> city2 ; in >> distance ; gr.InsertRoad(city1, city2, distance) ; // debug cout << "read from file : " << city1 << " to " << city2 << " is " << distance << endl ; counter++ ; } in.close() ; } void Dijkstra(Graph & gr) { string City1 ; string City2 ; cout << "Enter City 1: " ; cin >> City1 ; cout << "Enter City 2: " ; cin >> City2 ; cin.get() ; cout << "Dijkstra's Algorithm says shortest distance is : " << gr.Dijkstra(City1, City2) << endl ; }