计应193 第二组 张忙忙 地铁收费系统
|
PSP阶段 |
预估时间 |
实际所用时间 |
|
计划 |
16 |
10 |
|
15 |
10 |
|
开发 |
88 |
93 |
|
9 |
8 |
|
8 |
10 |
|
11 |
8 |
|
8 |
5 |
|
10 |
15 |
|
19 |
25 |
|
9 |
10 |
|
14 |
12 |
|
报告 |
12 |
10 |
|
4 |
3 |
|
4 |
2 |
|
6 |
5 |
|
总共花费时间 |
117 |
115 |
计划
明确需求和其他相关因素,估计每个阶段的时间成本。
开发
需求分析
起始站到终点站之间有效最短路径
起始站到终点站两点经过的站点数
代码规范
为目前的开发制定合适的规范
具体设计
首先将地铁规划图存储到文件中并读取,
然后使用迪杰斯特拉算法求出最短路径,并将结果展示,
最终根据最短路径来计算花费
具体编码和代码规范:
public class StationEnt
{
public string StationName { get; set; }
public Dictionary<LineEnt,int> PlaceLine { get; set; }
}
public class LineEnt
{
public string LineName { get; set; }
public List<StationEnt> Stations { get; set; }
public List<Tuple<LineEnt,StationEnt>> TranformStations { get; set; }
public bool IsRoundLine { get; set; }
}
public class Station2Station
{
public StationEnt FromStation { get; set; }
public LineEnt Line { get; set; }
public StationEnt ToStation { get; set; }
}
/// <summary>
/// 全线网站点集合
/// </summary>
protected Dictionary<string, StationEnt> _stationCollection;
/// <summary>
/// 全线网线路集合
/// </summary>
protected List<LineEnt> _lineCollection;
/// <summary>
/// 最短线路的站点数
/// </summary>
protected int _minLine;
/// <summary>
/// 最短换乘次数
/// </summary>
protected int _minTransCount;
/// <summary>
/// 最短的线路段集合
/// </summary>
protected List<List<Station2Station>> _shortestLines;
/// <summary>
/// 最短的线路集合
/// </summary>
protected List<List<StationEnt>> _shortestWays;
public MetroNetModel2()
{
_minLine = _minTransCount=int.MaxValue;
_shortestLines = new List<List<Station2Station>>();
_shortestWays = new List<List<StationEnt>>();
_lineCollection = new List<LineEnt>();
_stationCollection = new Dictionary<string, StationEnt>();
FieltLines();
}
public string GuedeMetroWay2(string fromStation, string toStation)
{
//验证站点存在
if (!_stationCollection.ContainsKey(fromStation))
return fromStation + " is not contain";
if (!_stationCollection.ContainsKey(toStation))
return toStation + " is not contain";
if (fromStation == toStation) return fromStation;
StationEnt start = _stationCollection[fromStation];
StationEnt end = _stationCollection[toStation];
List<Station2Station> stationList;
List<LineEnt> lineHis;
//重调两个最值
_minLine = _minTransCount = int.MaxValue;
//遍历这个起点站所在的线路,然后分别从这些线路出发去寻找目的站点
foreach (KeyValuePair<LineEnt,int> line in start.PlaceLine)
{
stationList = new List<Station2Station>();
lineHis = new List<LineEnt>() { line.Key };
GuideWay2(0, start, line.Key, end, stationList, lineHis);
}
//去除站点较多的线路
ClearLongerWays();
//生成线路的字符串
string result = ConvertStationList2String();
//清空整个查找过程中线路数据
_shortestLines.Clear();
_shortestWays.Clear();
return result;
}
测试代码:
Model.MetroNetModel2 metro = new Model.MetroNetModel2();
DateTime s = DateTime.Now;
Console.WriteLine(metro.GuedeMetroWay2("祖庙", "鹭江"));
Console.WriteLine(metro.GuedeMetroWay2("祖庙", "三元里"));
Console.WriteLine(metro.GuedeMetroWay2("祖庙", "沙园"));
Console.WriteLine(metro.GuedeMetroWay2("祖庙", "五山"));
Console.WriteLine(metro.GuedeMetroWay2("鹭江", "祖庙"));
Console.WriteLine(metro.GuedeMetroWay2("大学城北", "祖庙"));
Console.WriteLine(metro.GuedeMetroWay2("祖庙", "嘉禾望岗"));
Console.WriteLine(metro.GuedeMetroWay2("烈士陵园", "中大"));
Console.WriteLine(metro.GuedeMetroWay2("林和西", "体育中心"));
Console.WriteLine(metro.GuedeMetroWay2("林和西", "海心沙"));
Console.WriteLine(metro.GuedeMetroWay2("体育中心", "海心沙"));
Console.WriteLine(metro.GuedeMetroWay2("体育中心", "天河南"));
DateTime e = DateTime.Now;
Console.WriteLine(e - s);
Console.ReadLine();
运行结果:


代码复审:
首先使用debug一步一步的检查运行,对出现的错误进行修改
然后将重复的代码抽取成一个方法,减少代码的长度,增加可阅读性
总结:
第一次入手这个作业,借鉴了一些大佬的代码,以后会加强自己的基础,写出更完美的作业。

浙公网安备 33010602011771号