北京地铁线路规划

1.仓库代码:https://github.com/zjtggssg/-

2.需求分析:https://www.cnblogs.com/31701060zjt/p/11562918.html

3.模块分析:主模块和存储模块。主模块包含了算法与所需的功能。存储模块为站点模块,与线路模块。

4.程序演示:

4.1.支持一个新的命令行参数 -a进行地铁线路查询,并通过命令参数-o进行输出

-a 1号线 -map station.txt -o routine.txt
文件格式:我是生成一个txt文件

运行结果:

北京地铁线路规划_第1张图片

 

 

4.2.利用参数-b查询两个站点之间最短(经过的站点数最少)路线,并输出

-b 西直门 北京大学东门 -map station.txt -o routine.txt

  运行结果:

北京地铁线路规划_第2张图片

 

 

 

-b 动物园 和平里北街 -map station.txt -o routine.txt

  运行结果:

北京地铁线路规划_第3张图片

 

 

 

-b 上地 回龙观 -map station.txt -o routine.txt

  运行结果:

北京地铁线路规划_第4张图片

 

 

-b 打井 回龙观 -map station.txt -o routine.txt

  运行结果:

北京地铁线路规划_第5张图片

5.具体代码分析:

5.1 存储模块

存储站点信息的Station类

  • stationName存储站点名称
  • line存储经过该站点的轨道
  • coon存储在地铁线路上与该站点相临的站点
    private String stationName;
    private Setline;
    private Setconn;

存储线路信息Line类

  • lineName存储线路名称
  • stations存储该线路经过的站点
  • private String lineName;
    private Liststations;

5.2 主模块

主模块分为输入输出模块,制图模块,与功能实现模块。

制图模块:因为我要采用弗洛伊德算法,所以我将txt中的数据,通过存储转换成了邻接矩阵的形式。

 

private static void init(String filename) throws Exception {
        map = new HashMap();
        numMap = new HashMap();
        set = new HashSet();
        lines = new HashMap();
        readSet=readline(filename);

    }//统一管理,初始化

    private static void statisticsStation() {
        for (List lineList : readSet) {
            for (int i = 1; i < lineList.size(); i++) {
                set.add(lineList.get(i));
            }
        }
    }//去除重复的站点
    private static void getHashMap() {
        count = 0;
        for (String s : set) {
            Station station = new Station(s);
            map.put(count, station);
            numMap.put(s, count);
            count++;
        }
    }//建立下标与站点的映射
    private static void buildMap() {
        for (List lineList : readSet) {
            Line line = new Line(lineList.get(0));//得到线路名
            for (int i = 1; i < lineList.size(); i++) {
                Station station = map.get(numMap.get(lineList.get(i)));
                station.addLine(line);
                if (i != 1) {
                    Station laStation = map.get(numMap.get(lineList.get(i - 1)));
                    station.addConn(laStation);
                    laStation.addConn(station);
                }
                line.addStation(station);
            }
            lines.put(lineList.get(0), line);
        }
    }//将所有站点去除第一行线路名进行编号连接。
    public static void totable(){
      len = map.size();
      table =new int[len][len];
      for(int i=0;i 
 

  这样我就可以进行接下里的查询与最短路径寻找的操作了。

功能实现模块:查询模块:查询模块很简单,通过我所存储的Line类,我可以直接通过线路名称将所需求的线路进行输出。

public static List check(String linename){
        Line line=lines.get(linename);
        Liststations=line.getStations();
        Liststrings=new ArrayList();
        for(Station station:stations){
            strings.add(station.getStationName());
        }
     return strings;
    }

  这样就可以输出一条路线的信息了。

最短路径模块:因为已经通过制图模块将北京路线都弄在了一个只有INF与1的邻接矩阵中,这样操作起来就很方便了,直接使用floyd函数就可以求出最短的路径长度与站点了。

 

public static void floyd(String begin ,String end) {
     int n=table.length;
      start =numMap.get(begin);
      stop =numMap.get(end);
     findCheapestPath(start, stop, table);
     List list = result;

           
     }
    public static void findCheapestPath(int begin, int end, int[][] table) {
        floyd(table);
        result.add(begin);
        findPath(begin, end);
        result.add(end);
    }

    public  static void findPath(int i, int j) {
        int k = path[i][j];
        if (k == -1) {
            return;
        }
        findPath(i, k);   //递归
        result.add(k);
        findPath(k, j);
    }

    public static void floyd(int[][] table) {
        int size = len;
        path = new int[size][size];
        dist = new int[size][size];
        //initialize dist and path
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                path[i][j] = -1;
                dist[i][j] = table[i][j];
            }
        }
        for (int k = 0; k < size; k++) {
            for (int i = 0; i < size; i++) {
                for (int j = 0; j < size; j++) {
                    if (dist[i][k] != INF &&
                            dist[k][j] != INF &&
                            dist[i][k] + dist[k][j] < dist[i][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                        path[i][j] = k;
                    }
                }
            }
        }

    }

  

输入输出模块:因为我用txt模式来存储站点内容。

FileInputStream inputStream = new FileInputStream(filename);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));通过这代码读入txt的内容。保存在Set当中,好进行制图操作。
    public static Set> readline(String filename) throws Exception {
        FileInputStream inputStream = new FileInputStream(filename);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        Set>set=new HashSet>();
        String str = null;
        while ((str = bufferedReader.readLine()) != null) {

            String[] lineInformations = str.split(" ");
            Liststations=new ArrayList();
            for (String s : lineInformations) {
                stations.add(s);

            }
            set.add(stations);
        }
        //close
        inputStream.close();
        bufferedReader.close();
        return set;
    }
输出模块:因为要求有两个,我分别用两种格式输出。
public static void writeLine(String linename,String fileName) throws Exception {

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));

out.write(linename);
out.newLine();
for(Station s:lines.get(linename).getStations()){
out.write(s.getStationName());
out.newLine();
}
out.flush();
out.close();

} //用于查找路线的输出
public static void writeLine2(String fileName) throws Exception {

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));

out.write(map.get(start).getStationName() + " to " + map.get(stop).getStationName() + ",the cheapest path is:");
out.newLine();
out.write(dist[start][stop]+"");
out.newLine();
for(Integer r: result){
out.write(map.get(r).getStationName());
out.newLine();
}
out.flush();
out.close();
}//用于查询最短路径的输出
 

你可能感兴趣的:(北京地铁线路规划)