本项目已上传Github:
-
问题重述:
-
任务:
- 实现一个帮助进行地铁出行线路规划的命令行程序
- 实现加载地铁线路信息
- 实现查询指定地铁线路信息
- 实现从指定出发地到目的地的最短路径查询
- 实现一个帮助进行地铁出行线路规划的命令行程序
-
设计:
- 采用语言:java
- GitHub链接:https://github.com/xixihaha54/subway
-
主要模块介绍:
序号 模块名 功能 对应java类 1 主模块(subway) 对输入参数的控制 Subway.java 2 文件输入输出模块 txt文件的读写 DataBuilder.java 3 求最短路径(dij算法) 实现两个类的相互连接 Station.java - 需求1:文本的导入
-
public static List<String> getFileContent(String path) {
List<String> strList = new ArrayList<String>();
File file = new File(path);
InputStreamReader read = null;
BufferedReader reader = null;
try {
read = new InputStreamReader(new FileInputStream(file));
reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
strList.add(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return strList;
} - 需求二未完成
- 需求三
-
public class Subway {
private List<Station> outList = new ArrayList<Station>();//记录已经分析过的站点private void search(Station string) {
// TODO Auto-generated method stub
if(outList.size() == DataBuilder.totalStaion){
String a = string.getLinename();
}
return;
}
public void calculate(Station s1,Station s2){
if(outList.size() == DataBuilder.totalStaion){
System.out.println("找到目标站点:"+s2.getName()+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.println(station.getName()+" (" +station.getLinename()+")");
}
return;
}
if(!outList.contains(s1)){
outList.add(s1);
}
if(s1.getOrderSetMap().isEmpty()){
List<Station> Linkedstations = getAllLinkedStations(s1);
for(Station s : Linkedstations){
s1.getAllPassedStations(s).add(s);
}
}
Station parent = getShortestPath(s1);
if(parent == s2){
System.out.println("找到目标站点:"+s2+",共经过"+(s1.getAllPassedStations(s2).size()-1)+"站");
for(Station station : s1.getAllPassedStations(s2)){
System.out.print(station.getName()+" (" +station.getLinename()+")");
}
return;
}
for(Station child : getAllLinkedStations(parent)){
if(outList.contains(child)){
continue;
}
int shortestPath = (s1.getAllPassedStations(parent).size()-1) + 1;
if(s1.getAllPassedStations(child).contains(child)){
if((s1.getAllPassedStations(child).size()-1) > shortestPath){
//重置S1到周围各站的最小路径
s1.getAllPassedStations(child).clear();
s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
s1.getAllPassedStations(child).add(child);
}
} else {
//如果s1还没有计算过到此child的经过距离
s1.getAllPassedStations(child).addAll(s1.getAllPassedStations(parent));
s1.getAllPassedStations(child).add(child);
}
}
outList.add(parent);
calculate(s1,s2);
}
private Station getShortestPath(Station station){
int minPatn = Integer.MAX_VALUE;
Station rets = null;
for(Station s :station.getOrderSetMap().keySet()){
if(outList.contains(s)){
continue;
}
LinkedHashSet<Station> set = station.getAllPassedStations(s);
if(set.size() < minPatn){
minPatn = set.size();
rets = s;
}
}
return rets;
}
private List<Station> getAllLinkedStations(Station station){
List<Station> linkedStaions = new ArrayList<Station>();
for(List<Station> line : DataBuilder.lineSet){
if(line.contains(station)){
Station s = line.get(line.indexOf(station));
if(s.prev != null){
linkedStaions.add(s.prev);
}
if(s.next != null){
linkedStaions.add(s.next);
}
}
}
return linkedStaions;
} - 输出结果
-
总结
- 本次作业并没有完成所有的要求,同时也发现了自身的编程能力存在不足,同时在想修改代码时发现总体的代码框架也需要修改,下次做编程时一开始就得有大的方向