Github源代码地址:https://github.com/zyhJohn/Subway
Task:
功能
1、查询地铁线路
2、查询最短地铁换乘路径
实现语言
java
主要算法
dijkstra(迪杰斯特拉)算法
File:
将地铁线路信息存储为Station.txt文件,放置于eclipse-workspace\test(eclipse中创建的project名)目录下,地铁路线如下图所示:
文件存放位置如下图所示:
Code:
Route.java:model,计算最短路径时存储结果
public class Route { private Station start; private Station end; private int distance; private Station passStations; private String line; private int linechange; ………… }
Station.java:model,存储站点信息
public class Station { private String name; private Listline=new ArrayList (); private List linkStations = new ArrayList (); ………… }
DataSet.java:数据预处理等,项目代码核心
public class DataSet { public static LinkedHashSet> LineSet = new LinkedHashSet
>(); public DataSet(String pathname) throws IOException{ File filename = new File(pathname); InputStreamReader reader = new InputStreamReader( new FileInputStream(filename)); BufferedReader br = new BufferedReader(reader); String content=br.readLine(); int linenum=Integer.parseInt(content); for(int i=0;i
) { content=br.readLine(); List line=new ArrayList (); String[] linearea=content.split(" "); String linename=linearea[0]; for(int j=1;j ) { int flag=0; for(List l:LineSet) { for(int k=0;k ) { if(l.get(k).getName().equals(linearea[j])) { List newline=l.get(k).getLine(); newline.add(linename); l.get(k).setLine(newline); line.add(l.get(k)); flag=1; break; } } if(flag==1) break; } if(j==linearea.length-1&&linearea[j].equals(linearea[1])) { line.get(0).getLinkStations().add(line.get(line.size()-1)); line.get(line.size()-1).getLinkStations().add(line.get(0)); flag=1; } if(flag==0) line.add(new Station(linearea[j],linename)); } for(int j=0;j ) { List newlinkStations=line.get(j).getLinkStations(); if(j==0) { newlinkStations.add(line.get(j+1)); line.get(j).setLinkStations(newlinkStations); } else if(j==line.size()-1) { newlinkStations.add(line.get(j-1)); line.get(j).setLinkStations(newlinkStations); } else { newlinkStations.add(line.get(j+1)); newlinkStations.add(line.get(j-1)); line.get(j).setLinkStations(newlinkStations); } } LineSet.add(line); } br.close(); } private static List analysisList = new ArrayList<>(); private static HashMap routeMap = new HashMap<>(); ………… }
Output.java:程序入口及出口
public class Output { public static void main(String[] args) throws IOException { if(args[0].equals("-map")) new DataSet(args[1]); else if(args[0].equals("-a")) { if(args[2].equals("-map")) new DataSet(args[3]); else return; Liststations=DataSet.getLineStation(args[1]); String content=""; if(stations==null) content="不存在该地铁线路"; else { for(int i=0;i ) { if(i==stations.size()-1&&stations.get(i).getLinkStations().contains(stations.get(0))) content=content+stations.get(i).getName()+" "; else content=content+stations.get(i).getName()+" "; } } if(args[4].equals("-o")) { printToTxt(content,args[5]); } else return; } else if(args[0].equals("-b")) { if(args[3].equals("-map")) new DataSet(args[4]); else return; Station start=null; Station end=null; for(List l:DataSet.LineSet){ for(int k=0;k ) { if(l.get(k).getName().equals(args[1])) { start=l.get(k); } if(l.get(k).getName().equals(args[2])) { end=l.get(k); } } } String context=""; if(start==null||end==null) context="站点名不存在"; else { Route r=DataSet.shortestPath(start,end); List path=DataSet.getPath(r); context="共"+(r.getDistance()+1)+"站\n"; for(String s:path) context=context+s+"\n"; } if(args[5].equals("-o")) { printToTxt(context,args[6]); } else return; } } public static void printToTxt(String content,String path) throws IOException { ………… } }
Test:
Task1:
-a 1号线 -map Station.txt -o Route.txt
结果:
Task2:
-b 中关村 东直门 -map Station.txt -o Route.txt
结果:
Exception:
-b 浙江大学城市学院 天安门 -map Station.txt -o Route.txt
结果:显示不存在此站点