一、北京地铁线路图
二、功能需求
1.查询北京地铁所有线路
map \\S
知识兔2.查询线路
line \\S+ -map \\S+ -o \\S+
知识兔3.查询两个站点间的最短路径并输出所有站点
station \\S+ \\S+ -map \\S+ -o \\S+
知识兔三、设计分析
Station定义站的结构
public class Station {
private String name;
public Station prev;
public Station next;
private Map<Station, LinkedHashSet<Station>> orderSetMap = new HashMap<Station, LinkedHashSet<Station>>();
public Station(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LinkedHashSet<Station> getAllPassedStations(Station station) {
if (orderSetMap.get(station) == null) {
LinkedHashSet<Station> set = new LinkedHashSet<Station>();
set.add(this);
orderSetMap.put(station, set);
}
return orderSetMap.get(station);
}
public Map<Station, LinkedHashSet<Station>> getOrderSetMap() {
return orderSetMap;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof Station) {
Station s = (Station) obj;
if (s.getName().equals(this.getName())) {
return true;
} else {
return false;
}
} else {
return false;
}
}
@Override
public int hashCode() {
return this.getName().hashCode();
}
}
知识兔CreateLine和CreateData用来创建线路和站点
找到两个站点最短路径上的站点:
public String calculate(Station s1, Station s2)
知识兔计算最短路径:
private Station getShortestPath(Station station)
知识兔得到所有站点
private List<Station> getAllLinkedStations(Station station)
知识兔将结果写入指定文件:
public static void writeFileString(String strings, String writeFileName)
知识兔主函数:根据输入参数输出结果
public static void main(String[] args) throws IOException {
String map = "-map \\S+ ";
String line = "-line \\S+ -map \\S+ -o \\S+ ";
String path = "-station \\S+ \\S+ -map \\S+ -o \\S+ ";
String arge = "";
for (String i : args) {
arge += i + " ";
}
if (arge.matches(map)) {
String readFile = args[1];
CreateLine.readFile(readFile);
for (List<Station> linename : CreateLine.lineSet) {
for (int i = 0; i < linename.size(); i++) {
if(i==linename.size()-1)
System.out.print(linename.get(i).getName());
else if(i==0)
System.out.print(linename.get(i).getName() + " : ");
else
System.out.print(linename.get(i).getName() + "——>");
}
System.out.println();
}
} else if (arge.matches(line)) {
String lineName = args[1];
String readFile = args[3];
String writeFile = args[5];
CreateLine.readFile(readFile);
Station station = new Station(lineName);
String allStations = "";
int flag = 0;
for (List<Station> linename : CreateLine.lineSet) {
if (linename.contains(station)) {
allStations+=linename.get(0).getName() + "站点:"+"\n";
for (int i = 1; i < linename.size(); i++) {
if(i==linename.size()-1)
allStations+=linename.get(i).getName();
else
allStations+=linename.get(i).getName() + " -> ";
}
flag=1;
}
}
if(flag==0){
System.out.println("该路线不存在");
}
else{
System.out.println(allStations);
writeFileString(allStations, writeFile);
}
} else if (arge.matches(path)) {
String start = args[1];
String end = args[2];
String readFile = args[4];
String writeFile = args[6];
CreateData.readFile(readFile);
Path sw = new Path();
String allStations = sw.calculate(new Station(start), new Station(end));
System.out.println(allStations);
writeFileString(allStations, writeFile);
}else{
System.out.println("输入错误");
}
}
知识兔四、示例
map Station.txt
知识兔-map Station.txt
line 10号线 -map Station.txt -o Result.txt
知识兔-line 10号线 -map Station.txt -o Result.txt
station 古城 平乐园 -map Station.txt -o Result.txt
知识兔-station 古城 平乐园 -map Station.txt -o Result.txt
line 20号线 -map Station.txt -o Result.txt
知识兔-line 20号线 -map Station.txt -o Result.txt
五、小结
本次作业在编写代码的时候,尤其是在最短路径的算法上遇到了困难,在网上查找了相关的代码之后,有点思路,进行代码的编写。通过本次的个人实验项目,我感觉对自身的代码水平有很大的提升。总的来说,对于项目需求的理解和个人现有的知识水平都要加强。