/************************************************ * Laboration 4 i * Datastrukturer och Algoritmer * Dijkstras Shortest Path ************************************************* * Christian Ohlsson * Karlstads universitet, 1999 * * main.cpp * Innehåller drivrutiner för * Dijkstra labben. * Skapat med Borland C++ 4.0 * Låt main.cpp och graph.cpp * finnas i projektet. * Kompilera för Easywin, Windows 3.x * 16-bitar, target: Small * Se till att filen network.txt finns * i samma katalog som dijkstra.exe *************************************************/ #include "header.h" /*=============================================== * cities * Alla städer i NeverNeverLand -------------------------------------------------*/ const char cities[numOfCities][citySize] = { {"Wonderland"}, {"Sanctuary"}, {"Battlefield"}, {"Octopus's Garden"}, {"Paradise City"}, {"Strawberry Fields"}, {"Chaos"}, {"El Dorado"}, {"Kozmic Place"}, {"Oz"}, {"Nowhereland"}, {"Shangrila"}, {"Southbound"} }; graph myGraph; //Global klass för grafen /*=============================================== * mataIn * Ser till att bara tal matas in -------------------------------------------------*/ int mataIn() { char buffer[BUFFERSIZE]; int f; do { cin >> buffer; if (strcmp(buffer,"0") == 0) return 0; else { f = atoi(buffer); if (f!=0) return f; else cout <<"NAN, try again: "; } }while (!f); return 0; } /*=============================================== * getName * Hämtar namn på stad, dvs det nummer staden * motsvarar -------------------------------------------------*/ int getName(char *text) { int i = 0; while(strcmp(text, cities[i]) !=0 && i < numOfCities) i++; if(i >= numOfCities) return -1; else return i; } /*=============================================== * readGraph * Läser in alla städer och tider från fil. -------------------------------------------------*/ void readGraph() { graphNode node; ifstream myFile; char temp[citySize]; if(!myGraph.makeGraph(numOfCities)) { //Skapar en graf cout<<"Could not make graph";getch(); return; } myFile.open("network.txt"); //Öppnar filen do { myFile.get(temp, citySize); node.startCity = getName(temp); //Hämtar startstad myFile.seekg(FORWARD, ios::cur); myFile.get(temp, citySize); node.endCity = getName(temp); //Hämtar slutstad myFile.seekg(FORWARD, ios::cur); myFile.get(temp, citySize); node.Start = atoi(temp); //Hämtar starttid myFile.seekg(FORWARD, ios::cur); myFile.get(temp, citySize); node.End = atoi(temp); //Hämtar sluttid myFile.seekg(FORWARD, ios::cur); myGraph.connect(node.startCity, node.endCity, node); //Sätter in noden } while(myFile.peek() != EOF); } /*=============================================== * printPath * Skriver ut kortaste vägen -------------------------------------------------*/ void printPath() { int index; char text[citySize * 4], time[MAXTIME]; cout << "The shortest path is:" << "\n________________________________________________________"; for(index = 0; index < myGraph.prevSize; index++) { text[0] = 0; time[0] = 0; cout << "\nFrom " << cities[myGraph.lastPath[index].startCity]; itoa(myGraph.lastPath[index].Start, time, BASE); cout << " "<< time << ":00\t"; time[0] = 0; cout << "to "; cout << cities[myGraph.lastPath[index].endCity]; itoa(myGraph.lastPath[index].End, time, BASE); cout << " " << time << ":00\t"; } text[0] = 0; time[0] = 0; itoa(myGraph.lastTime, time, BASE); cout << "\n________________________________________________________"; cout << "\nThe total travelling time is "<" << "\n Welcome to the RailWay Company" << "\n in the NeverNeverLand..." << "\n ____________________________________________________" << "\n | 0. Wonderland 7. El Dorado |" << "\n | 1. Sanctuary 8. Kozmic Place |" << "\n | 2. Battlefield 9. Oz |" << "\n | 3. Octopus's Garden 10. Nowhereland |" << "\n | 4. Paradise City 11. Shangrila |" << "\n | 5. Strawberry Fields 12. Southbound |" << "\n | 6. Chaos |" << "\n |__________________________________________________|" << "\n"; } /*=============================================== * main * Huvudfunktionen för programmet. * Läser in grafen och ber användaren * mata in mellan vilka städer han skall resa -------------------------------------------------*/ void main() { int start, end, time; char playAgain; readGraph(); //Läs in filen do{ printTowns(); //Skriv ut städerna do { cout <<"\nFrom wich town (0-"<<(numOfCities-1)<<"): "; start=mataIn(); }while(start<0 || start>numOfCities-1); do { cout <<"To wich town (0-"<<(numOfCities-1)<<"): "; end=mataIn(); }while(end<0 || end>numOfCities-1); do { cout <<"Time of departure (0-24): "; time=mataIn(); }while(time<0 || time>24); myGraph.dijkstra(start, end, time); //Beräkna kortaste väg if(myGraph.prevSize > 0) printPath(); //Skriv ut vägen else cout <<"\nStarttown and arrivaltown are the same! Are you stupid?"; cout <<"\n\nPlay Again (Y/N)? "; cin >> playAgain; }while(playAgain == 'y' || playAgain == 'Y'); }