-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.cs
More file actions
365 lines (338 loc) · 14.1 KB
/
DataManager.cs
File metadata and controls
365 lines (338 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace TrainTimetable
{
/// <summary>
/// 列车开行方案、运行图、交路图数据管理工具
/// </summary>
class DataManager
{
/// <summary>
/// 所有列车列表
/// </summary>
public List<Train> TrainList;
/// <summary>
/// 上行列车列表
/// </summary>
public List<Train> upTrainList;
/// <summary>
/// 下行列车列表
/// </summary>
public List<Train> downTrainList;
/// <summary>
/// 所有列车字典
/// </summary>
public Dictionary<string, Train> TrainDic;
/// <summary>
/// 上行列车字典
/// </summary>
public Dictionary<string, Train> UpTrainDic;
/// <summary>
/// 下行列车字典
/// </summary>
public Dictionary<string, Train> DownTrainDic;
/// <summary>
/// 车站列表
/// </summary>
public List<Station> stationList;
/// <summary>
/// 车站名列表
/// </summary>
public List<String> stationStringList;
/// <summary>
/// 车站里程列表
/// </summary>
public List<double> stationMileList;
/// <summary>
/// 动车段车站名称列表
/// </summary>
public List<String> depotStringList;
/// <summary>
/// 动车段车站里程列表
/// </summary>
public List<double> depotMileList;
/// <summary>
/// 列车衔接信息
/// </summary>
public Dictionary<string, List<List<string>>> ConnInfo;
/// <summary>
/// 将时间格式转化为分钟格式
/// </summary>
/// <param name="trainList">列车列表</param>
public void ToMinute(List<Train> trainList)
{
foreach (Train train in TrainList)
{
train.MinuteDic = new Dictionary<string, List<int>>();
foreach (KeyValuePair<string, List<string>> time in train.staTimeDic)
{
List<int> minutelist = new List<int>();
if (train.staTimeDic[time.Key][0] != "")
{
if (train.staTimeDic[time.Key][0].Contains(":"))
{
string[] trainarrminute = train.staTimeDic[time.Key][0].Split(':');
minutelist.Add(int.Parse(trainarrminute[0]) * 60 + int.Parse(trainarrminute[1]));
}
else
{
minutelist.Add(Convert.ToInt32(train.staTimeDic[time.Key][0]));
}
}
else minutelist.Add(0);
if (train.staTimeDic[time.Key][1] != "")
{
if (train.staTimeDic[time.Key][1].Contains(":"))
{
string[] traindepminute = train.staTimeDic[time.Key][1].Split(':');
minutelist.Add(int.Parse(traindepminute[0]) * 60 + int.Parse(traindepminute[1]));
}
else
{
minutelist.Add(Convert.ToInt32(train.staTimeDic[time.Key][1]));
}
}
else minutelist.Add(0);
train.MinuteDic.Add(time.Key, minutelist);
}
}
}
/// <summary>
/// 读取车站文件(车站编号,车站名,车站里程,是否有动车所)
/// </summary>
/// <param name="Filename">车站文件名</param>
public void ReadStation(string Filename)
{
StreamReader sr = new StreamReader(Filename, Encoding.UTF8);
string str = sr.ReadLine();
stationList = new List<Station>();
stationStringList = new List<String>();
stationMileList = new List<double>();
while (str != null)
{
Station sta = new Station();
str = str.Replace("\r", string.Empty).Replace("\"", string.Empty).Replace("\t", string.Empty).Replace("'", string.Empty).Replace("\\", string.Empty).Replace("\0", string.Empty).Replace("?", string.Empty).Replace("*", string.Empty);
String[] strr = str.Split(',');
sta.stationNo = strr[0];
sta.stationName = strr[1];
sta.totalMile = int.Parse(strr[2]);
stationList.Add(sta);
stationStringList.Add(sta.stationName);
stationMileList.Add(sta.totalMile);
str = sr.ReadLine();
}
sr.Close();
}
/// <summary>
/// 读取列车信息(车次号,车站名,到点,发点,起讫站)
/// </summary>
/// <param name="Filename">列车时刻表文件</param>
public void ReadTrain(string Filename)
{
TrainDic = new Dictionary<string, Train>();
TrainDic.Clear();
StreamReader sr = new StreamReader(Filename, Encoding.UTF8);
sr.ReadLine();
string str = sr.ReadLine();
while (str != null)
{
Train tra = new Train();
str = str.Replace("\r", string.Empty).Replace("\"", string.Empty).Replace("\t", string.Empty).Replace("'", string.Empty).Replace("\\", string.Empty).Replace("\0", string.Empty).Replace("?", string.Empty).Replace("*", string.Empty);
String[] strr = str.Split(',');
tra.TrainNo = strr[0];
string staname = strr[1];
if (Convert.ToInt16(strr[4]) == 1)//1表示始发站
{
tra.OriSta = staname;
}
else if (Convert.ToInt16(strr[4]) == 2)//2表示终点站
{
tra.DesSta = staname;
}
string LastNumber = tra.TrainNo.Substring(tra.TrainNo.Length - 1, 1);
if ((LastNumber == "0") || (LastNumber == "2") || (LastNumber == "4") || (LastNumber == "6") || (LastNumber == "8"))
{
tra.Dir = "up";
}
else
{
tra.Dir = "down";
}
if (!TrainDic.ContainsKey(tra.TrainNo))
{
tra.staTimeDic = new Dictionary<string, List<string>>();
if (!tra.staTimeDic.ContainsKey(staname))
{
List<string> timelist = new List<string>();
timelist.Add(strr[2]);
timelist.Add(strr[3]);
tra.staTimeDic.Add(staname, timelist);
}
TrainDic.Add(tra.TrainNo, tra);
}
else
{
if (!TrainDic[tra.TrainNo].staTimeDic.ContainsKey(staname))
{
List<string> timelist = new List<string>();
timelist.Add(strr[2]);
timelist.Add(strr[3]);
TrainDic[tra.TrainNo].staTimeDic.Add(staname, timelist);
}
}
str = sr.ReadLine();
}
sr.Close();
TrainList = new List<Train>();
foreach (KeyValuePair<string, Train> trainNumber in TrainDic)//给trainList赋值
{
TrainList.Add(TrainDic[trainNumber.Key]);
}
for (int i = 0; i < TrainList.Count(); i++)
{
TrainList[i].staList = new List<string>();
foreach (KeyValuePair<string, List<string>> stationName in TrainList[i].staTimeDic)
{
TrainList[i].staList.Add(stationName.Key);
}
}
ToMinute(TrainList);
}
/// <summary>
/// 根据车次尾号将列车存入上行或下行列车字典
/// </summary>
public void DivideUpDown()
{
UpTrainDic = new Dictionary<string, Train>();
DownTrainDic = new Dictionary<string, Train>();
for (int i = 0; i < TrainList.Count; i++)
{
string LastNumber = TrainList[i].TrainNo.Substring(TrainList[i].TrainNo.Length - 1, 1);
if ((LastNumber == "0") || (LastNumber == "2") || (LastNumber == "4") || (LastNumber == "6") || (LastNumber == "8"))
{
UpTrainDic.Add(TrainList[i].TrainNo, TrainList[i]);
}
else if ((LastNumber == "1") || (LastNumber == "3") || (LastNumber == "5") || (LastNumber == "7") || (LastNumber == "9"))
{
DownTrainDic.Add(TrainList[i].TrainNo, TrainList[i]);
}
}
upTrainList = new List<Train>();
foreach (KeyValuePair<string, Train> trainNumber in UpTrainDic)//给uptrainList赋值
{
upTrainList.Add(UpTrainDic[trainNumber.Key]);
}
for (int i = 0; i < upTrainList.Count(); i++)
{
upTrainList[i].staList = new List<string>();
foreach (KeyValuePair<string, List<string>> trainNumber in upTrainList[i].staTimeDic)
{
upTrainList[i].staList.Add(trainNumber.Key);
}
}
ToMinute(upTrainList);
downTrainList = new List<Train>();
foreach (KeyValuePair<string, Train> trainNumber in DownTrainDic)//给uptrainList赋值
{
downTrainList.Add(DownTrainDic[trainNumber.Key]);
}
for (int i = 0; i < downTrainList.Count(); i++)
{
downTrainList[i].staList = new List<string>();
foreach (KeyValuePair<string, List<string>> trainNumber in downTrainList[i].staTimeDic)
{
downTrainList[i].staList.Add(trainNumber.Key);
}
}
ToMinute(downTrainList);
}
/// <summary>
/// 把车和车站关联,生成车站的列车列表
/// </summary>
public void AddTra2sta()
{
for (int i = 0; i < stationList.Count; i++)
{
stationList[i].upStaTraArrList = new List<Train>();
stationList[i].upStaTraDepList = new List<Train>();
for (int j = 0; j < upTrainList.Count; j++)
{
if (upTrainList[j].staList.Contains(stationList[i].stationName))
{
stationList[i].upStaTraArrList.Add(upTrainList[j]);
stationList[i].upStaTraDepList.Add(upTrainList[j]);
}
}
stationList[i].upStaTraArrList.Sort(delegate (Train x, Train y)
{
return x.MinuteDic[stationList[i].stationName][0].CompareTo(y.MinuteDic[stationList[i].stationName][0]);
});
stationList[i].upStaTraDepList.Sort(delegate (Train x, Train y)
{
return x.MinuteDic[stationList[i].stationName][1].CompareTo(y.MinuteDic[stationList[i].stationName][1]);
});
}
for (int i = 0; i < stationList.Count; i++)
{
stationList[i].downStaTraArrList = new List<Train>();
stationList[i].downStaTraDepList = new List<Train>();
for (int j = 0; j < downTrainList.Count; j++)
{
if (downTrainList[j].staList.Contains(stationList[i].stationName))
{
stationList[i].downStaTraArrList.Add(downTrainList[j]);
stationList[i].downStaTraDepList.Add(downTrainList[j]);
}
}
stationList[i].downStaTraArrList.Sort(delegate (Train x, Train y)
{
return x.MinuteDic[stationList[i].stationName][0].CompareTo(y.MinuteDic[stationList[i].stationName][0]);
});
stationList[i].downStaTraDepList.Sort(delegate (Train x, Train y)
{
return x.MinuteDic[stationList[i].stationName][1].CompareTo(y.MinuteDic[stationList[i].stationName][1]);
});
}
}
/// <summary>
/// 将上下行的停站存入isStopDic字典里
/// </summary>
public void GetStop()
{
for (int i = 0; i < upTrainList.Count; i++)
{
upTrainList[i].isStopDic = new Dictionary<string, bool>();
for (int j = 0; j < upTrainList[i].staList.Count; j++)
{
if (upTrainList[i].staTimeDic[upTrainList[i].staList[j]][0] == upTrainList[i].staTimeDic[upTrainList[i].staList[j]][1])
{
upTrainList[i].isStopDic.Add(upTrainList[i].staList[j], false);
}
else
{
upTrainList[i].isStopDic.Add(upTrainList[i].staList[j], true);
}
}
}
for (int i = 0; i < downTrainList.Count; i++)
{
downTrainList[i].isStopDic = new Dictionary<string, bool>();
for (int j = 0; j < downTrainList[i].staList.Count; j++)
{
if (downTrainList[i].staTimeDic[downTrainList[i].staList[j]][0] == downTrainList[i].staTimeDic[downTrainList[i].staList[j]][1])
{
downTrainList[i].isStopDic.Add(downTrainList[i].staList[j], false);
}
else
{
downTrainList[i].isStopDic.Add(downTrainList[i].staList[j], true);
}
}
}
}
}
}