-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrainScheduler.cpp
More file actions
311 lines (283 loc) · 10.5 KB
/
TrainScheduler.cpp
File metadata and controls
311 lines (283 loc) · 10.5 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
#include "TrainScheduler.h"
// 列车调度类,储存单个列车信息
namespace trainsys {
/**
* @brief 构造函数,初始化一个空的列车调度对象
* @note 初始化站点数量和座位数为0
*/
TrainScheduler::TrainScheduler() {
this->passingStationNum = 0;
this->seatNum = 0;
}
/**
* @brief 析构函数
* @note 使用固定数组,无需额外清理
*/
TrainScheduler::~TrainScheduler() {
}
/**
* @brief 在列车路线末尾添加新站点
* @param station 要添加的站点ID
* @throw std::runtime_error 当站点数量达到最大限制时抛出异常
* @note 新添加站点的运行时间和票价初始化为0
*/
void TrainScheduler::addStation(const StationID &station) {
if (this->passingStationNum >= MAX_PASSING_STATION_NUMBER) {
throw std::runtime_error("Station array is full");
}
this->stations[this->passingStationNum] = station;
this->duration[this->passingStationNum] = 0;
this->price[this->passingStationNum] = 0;
this->passingStationNum++;
}
/**
* @brief 在指定位置插入新站点
* @param i 插入位置的索引(0-based)
* @param station 要插入的站点ID
* @throw std::runtime_error 当索引无效或站点数量达到最大限制时抛出异常
* @note
* - 索引i可以等于passingStationNum,表示在末尾插入
* - 新插入站点的运行时间和票价初始化为0
* - 会导致i及其后续站点的信息后移
*/
void TrainScheduler::insertStation(int i, const StationID &station) {
if (i < 0 || i > this->passingStationNum) {
throw std::runtime_error("Invalid insert position");
}
if (this->passingStationNum >= MAX_PASSING_STATION_NUMBER) {
throw std::runtime_error("Station array is full");
}
// 将i及其后面的元素后移一位
for (int j = this->passingStationNum; j > i; j--) {
this->stations[j] = this->stations[j - 1];
this->duration[j] = this->duration[j - 1];
this->price[j] = this->price[j - 1];
}
this->stations[i] = station;
this->duration[i] = 0; // 新插入站点的运行时间初始化为0
this->price[i] = 0; // 新插入站点的票价初始化为0
this->passingStationNum++;
}
/**
* @brief 删除指定位置的站点
* @param i 要删除的站点索引(0-based)
* @throw std::runtime_error 当索引无效时抛出异常
* @note 删除后,后续站点的信息会前移
*/
void TrainScheduler::removeStation(int i) {
if (i < 0 || i >= this->passingStationNum) {
throw std::runtime_error("Invalid station index");
}
// 将i后面的元素前移一位
for (int j = i; j < this->passingStationNum - 1; j++) {
this->stations[j] = this->stations[j + 1];
this->duration[j] = this->duration[j + 1];
this->price[j] = this->price[j + 1];
}
this->passingStationNum--;
}
/**
* @brief 查找指定站点的索引
* @param stationID 要查找的站点ID
* @return 返回站点的索引,如果未找到则返回-1
*/
int TrainScheduler::findStation(const StationID &stationID) {
for (int i = 0; i < this->passingStationNum; i++) {
if (this->stations[i] == stationID) {
return i;
}
}
return -1; // 未找到返回-1
}
/**
* @brief 遍历并打印所有站点信息
* @note 按顺序打印每个站点的ID、到下一站的运行时间和票价
*/
void TrainScheduler::traverseStation() {
for (int i = 0; i < this->passingStationNum; i++) {
std::cout << "Station " << i << ": " << this->stations[i]
<< ", Duration: " << this->duration[i]
<< ", Price: " << this->price[i] << std::endl;
}
}
/**
* @brief 设置列车ID
* @param id 新的列车ID
*/
void TrainScheduler::setTrainID(const TrainID &id) {
this->trainID = id;
}
/**
* @brief 批量设置站点间票价
* @param price 票价数组,长度应不小于当前站点数
* @note price[i]表示从第i站到第i+1站的票价
*/
void TrainScheduler::setPrice(const int price[]) {
for (int i = 0; i < this->passingStationNum - 1; i++) {
this->price[i] = price[i];
}
}
/**
* @brief 批量设置站点间运行时间
* @param duration 运行时间数组,长度应不小于当前站点数
* @note duration[i]表示从第i站到第i+1站的运行时间
*/
void TrainScheduler::setDuration(const int duration[]) {
for (int i = 0; i < this->passingStationNum - 1; i++) {
this->duration[i] = duration[i];
}
}
/**
* @brief 设置列车座位数
* @param seatNum 新的座位数
*/
void TrainScheduler::setSeatNumber(int seatNum) {
this->seatNum = seatNum;
}
/**
* @brief 获取列车ID
* @return 返回列车ID
*/
TrainID TrainScheduler::getTrainID() const {
return this->trainID;
}
/**
* @brief 获取座位数
* @return 返回座位数
*/
int TrainScheduler::getSeatNum() const {
return this->seatNum;
}
/**
* @brief 获取当前站点数量
* @return 返回站点数量
*/
int TrainScheduler::getPassingStationNum() const {
return this->passingStationNum;
}
/**
* @brief 获取指定索引的站点ID
* @param i 站点索引(0-based)
* @return 返回站点ID
* @throw std::runtime_error 当索引无效时抛出异常
*/
StationID TrainScheduler::getStation(int i) const {
if (i < 0 || i >= this->passingStationNum) {
throw std::runtime_error("Invalid station index");
}
return this->stations[i];
}
/**
* @brief 获取指定索引的运行时间
* @param i 站点索引(0-based)
* @return 返回从第i站到第i+1站的运行时间
* @throw std::runtime_error 当索引无效时抛出异常
*/
int TrainScheduler::getDuration(int i) const {
if (i < 0 || i >= this->passingStationNum) {
throw std::runtime_error("Invalid duration index");
}
return this->duration[i];
}
/**
* @brief 获取指定索引的票价
* @param i 站点索引(0-based)
* @return 返回从第i站到第i+1站的票价
* @throw std::runtime_error 当索引无效时抛出异常
*/
int TrainScheduler::getPrice(int i) const {
if (i < 0 || i >= this->passingStationNum) {
throw std::runtime_error("Invalid price index");
}
return this->price[i];
}
/**
* @brief 批量获取所有站点ID
* @param stations 用于存储站点ID的数组,调用者需确保数组大小足够
* @note
* - 这是一个批量获取接口,不返回值而是通过参数传出数据
* - 调用者必须确保传入的数组大小不小于当前站点数量
* - 函数会将所有站点ID按顺序复制到传入的数组中
*/
void TrainScheduler::getStations(StationID *stations) const {
for (int i = 0; i < this->passingStationNum; i++) {
stations[i] = this->stations[i];
}
}
/**
* @brief 批量获取所有运行时间
* @param duration 用于存储运行时间的数组,调用者需确保数组大小足够
* @note
* - 这是一个批量获取接口,不返回值而是通过参数传出数据
* - 调用者必须确保传入的数组大小不小于当前站点数量
* - duration[i]表示从第i站到第i+1站的运行时间
*/
void TrainScheduler::getDuration(int *duration) const {
for (int i = 0; i < this->passingStationNum; i++) {
duration[i] = this->duration[i];
}
}
/**
* @brief 批量获取所有票价
* @param price 用于存储票价的数组,调用者需确保数组大小足够
* @note
* - 这是一个批量获取接口,不返回值而是通过参数传出数据
* - 调用者必须确保传入的数组大小不小于当前站点数量
* - price[i]表示从第i站到第i+1站的票价
*/
void TrainScheduler::getPrice(int *price) const {
for (int i = 0; i < this->passingStationNum; i++) {
price[i] = this->price[i];
}
}
/**
* @brief 判断两个列车调度对象是否相等
* @param rhs 要比较的另一个对象
* @return 如果两个对象的trainID相同则返回true
* @note 只比较trainID,不比较其他属性
*/
bool TrainScheduler::operator==(const TrainScheduler &rhs) const {
return this->trainID == rhs.trainID;
}
/**
* @brief 判断两个列车调度对象是否不相等
* @param rhs 要比较的另一个对象
* @return 如果两个对象的trainID不同则返回true
*/
bool TrainScheduler::operator!=(const TrainScheduler &rhs) const {
return !(*this == rhs);
}
/**
* @brief 比较两个列车调度对象的大小
* @param rhs 要比较的另一个对象
* @return 如果当前对象的trainID小于rhs的trainID则返回true
* @note 用于在有序容器中存储TrainScheduler对象
*/
bool TrainScheduler::operator<(const TrainScheduler &rhs) const {
return this->trainID < rhs.trainID;
}
/**
* @brief 输出流运算符重载
* @param os 输出流对象
* @param trainScheduler 要输出的列车调度对象
* @return 返回输出流对象
* @note 按照特定格式输出列车信息,包括:
* - 列车ID
* - 座位数
* - 所有站点信息(包括站点ID、运行时间和票价)
*/
std::ostream &operator<<(std::ostream &os, const TrainScheduler &trainScheduler) {
os << "Train " << trainScheduler.trainID << ":\n";
os << "Seat Number: " << trainScheduler.seatNum << "\n";
os << "Stations:\n";
for (int i = 0; i < trainScheduler.passingStationNum; i++) {
os << " " << i + 1 << ". " << trainScheduler.stations[i];
if (i < trainScheduler.passingStationNum - 1) {
os << " -> Duration: " << trainScheduler.duration[i]
<< ", Price: " << trainScheduler.price[i];
}
os << "\n";
}
return os;
}
}