private void btnSearch_Click(object sender, RoutedEventArgs e) //event that happens when button is clicked { string station = cmbxStationStrChooser.Text; //gets input from user string result = string.Empty; //empty string Station resultStat = new Station(); //creates new Station Object if (radStatName.IsChecked == true) //if StationName checkbox is checked { resultStat = Guide.SearchByStationName(station); //search for the input station code based on inputted station name } else //else if StationCode checkbox is checked { resultStat = Guide.SearchByStationCd(station); //search by station code based on user input } foreach (string StationCodeStr in resultStat.StationCode) //foreach loop { result += Guide.DisplayRoute(StationCodeStr); //output string result += "\r\n"; } DisplayResults LineResult = new DisplayResults(); //create new instance of DisplayResults object LineResult.Show(); //show DisplayResults window LineResult.txtBoxDisplay.Text = "Displaying Line : \r\n" + "# - Represents the station that you selected\r\n" + result; //display output in textbox in DisplayResults window LineResult.tripDetails.Visibility = Visibility.Collapsed; this.Hide(); //hides current windoxw }
public static string initTraverseDijkstra(int sourceGraphIndex, int destinationGraphIndex) //find route. Dijikstra algorithm referenced from here: https://www.codingame.com/playgrounds/1608/shortest-paths-with-dijkstras-algorithm/dijkstras-algorithm { double[,] distanceTable = new double[size, 2]; //the first column will have the distance of that node from the starting node, the second column is the index of the node that came before it. List <int> visitedIndex = new List <int>(); //new list for (int i = 0; i < size; i++) //for loop { distanceTable[i, 0] = int.MaxValue; distanceTable[i, 1] = -1; } distanceTable[sourceGraphIndex, 0] = 0; //Distance from the starting station from the starting station is 0, thus setting it. int currentNodeIndex = sourceGraphIndex; //sets value of current node index to value of the source graph index TraverseDijkstra(distanceTable, visitedIndex, currentNodeIndex); //Calling Dijkstra method for (int i = 0; i < size; i++) //for loop { Console.WriteLine("{0} - Distance:{1} - Comes From:{2}", i, distanceTable[i, 0], distanceTable[i, 1]); //for debug to list the entire distance table value } List <int> routeGraphIndex = new List <int>() { destinationGraphIndex }; //new list int currentIndex = destinationGraphIndex; while (currentIndex != sourceGraphIndex) //while loop { currentIndex = (int)distanceTable[currentIndex, 1]; routeGraphIndex.Add(currentIndex); //add current index to routeGraphIndex list } routeGraphIndex.Reverse(); //reverses the routeGraphIndex list List <Station> routeStation = new List <Station>(); //new list foreach (int gI in routeGraphIndex) //foreach loop { routeStation.Add(SearchByStationGraphIndex(gI)); //add station objects to the routeStation list } List <string> routeStationCd = new List <string>(); //new list for (int idx = 0; idx < routeStation.Count; idx++) //for loop { string prevStatLineCd = ""; string nextStatLineCd = ""; if ((idx == 0) && (routeStation[idx].IsInterchange)) //if index = 0 and the station is an interchange { List <string> stationLineCd = new List <string>(); //new list foreach (string statCd in routeStation[idx].StationCode) //foreach loop { stationLineCd.Add(statCd.Substring(0, 2)); //add station code to the stationLineCd list } foreach (string statCd in routeStation[idx + 1].StationCode) //foreach loop { if (stationLineCd.Contains(statCd.Substring(0, 2))) //if stationLineCd list contains station code of next station in routeStation list { nextStatLineCd = statCd.Substring(0, 2); //set nextStatLineCd to be equals to the station code } } foreach (string statCd in routeStation[idx].StationCode) //foreach loop { if (statCd.Contains(nextStatLineCd)) //if routeStation list contains nextStatLineCd station code { routeStationCd.Add(statCd); //add the station code found to the routeStationCd list } } } else if ((idx == routeStation.Count - 1) && (routeStation[idx].IsInterchange)) //else if { List <string> stationLineCd = new List <string>(); //new list foreach (string statCd in routeStation[idx].StationCode) //foreach loop { stationLineCd.Add(statCd.Substring(0, 2)); //add station code to the stationLineCd list } foreach (string statCd in routeStation[idx - 1].StationCode) //foreach loop { if (stationLineCd.Contains(statCd.Substring(0, 2))) //if stationLineCd list contains station code of previous station in routeStation list { prevStatLineCd = statCd.Substring(0, 2); //set prevStatLineCd to be equals to the station code } } foreach (string statCd in routeStation[idx].StationCode) //foreach loop { if (statCd.Contains(prevStatLineCd)) //if routeStation List contains prevStatLineCd station code { routeStationCd.Add(statCd); //add station code to the routeStationCd list } } } else if (routeStation[idx].IsInterchange) //else if station in RouteStation list is an interchange { List <string> stationLineCd = new List <string>(); //new list foreach (string statCd in routeStation[idx].StationCode) //foreach loop { stationLineCd.Add(statCd.Substring(0, 2)); //add stationcode to stationLineCd list } foreach (string statCd in routeStation[idx - 1].StationCode) //foreach loop { if (stationLineCd.Contains(statCd.Substring(0, 2))) //if stationLineCd list contains station code of previous station in routeStation list { prevStatLineCd = statCd.Substring(0, 2); //set prevStatLineCd to be equal to the station code } } foreach (string statCd in routeStation[idx + 1].StationCode) //foreach loop { if (stationLineCd.Contains(statCd.Substring(0, 2))) //if stationLineCd list contains station code of next station in routeStation list { nextStatLineCd = statCd.Substring(0, 2); //set nextStatLineCd to be equal to the station code } } foreach (string statCd in routeStation[idx].StationCode) //foreach loop { if (statCd.Contains(prevStatLineCd)) //if routeStation list contains prevStatLineCd { routeStationCd.Add(statCd); //add station code to the routeStationCd list } } if (!prevStatLineCd.Equals(nextStatLineCd)) //if prevStatLineCd is not equal to nextStatLineCd { foreach (string statCd in routeStation[idx].StationCode) //foreach loop { if (statCd.Contains(nextStatLineCd)) //if routeStation list contains nextStatLineCd { routeStationCd.Add(statCd); //add station code to the routeStationCd list } } } } else //else { routeStationCd.Add(routeStation[idx].StationCode[0]); //add Station Code from routeStation list to routeStationCd list } } string outputRoute = string.Format("Display Route from {0} to {1} - Taking {2} stations\r\n", routeStation[0].StationName, routeStation[routeStation.Count - 1].StationName, routeStation.Count); //output string outputRoute += "-- Start of Route --\r\n"; foreach (string statCd in routeStationCd) //foreach loop { outputRoute += string.Format("{0} - {1}\r\n", statCd, Guide.SearchByStationCd(statCd).StationName); //output string } outputRoute += "-- End of Route --"; //output string return(outputRoute); //return string }