-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.cpp
80 lines (63 loc) · 2.22 KB
/
Line.cpp
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
#include "Line.h"
#include"Exceptions.h"
string Line::getLabel()
{
return label_;
}
Station* Line::getFirstStation()
{
return first_station_;
}
Station* Line::getLastStation()
{
return last_station_;
}
string Line::outputStations()
{
try {
string output;
Station* station = first_station_;
do {
output += station->outputStationCodeAndName();
if (station == last_station_) {//output last station two times
output += station->outputStationCodeAndName();
}
station = station->getNeighbour(label_);
} while (station != first_station_);
output += station->outputStationCodeAndName();//output first station second time
output.pop_back();
return output;
}
catch (SearchError& e) {
cout << "Line: " << label_ << " is not cyclic";
}
}
bool Line::BFSLineSearch(int last_code,Station* first_station,set<int>&visited_stations,set<string>& visited_lines, queue<pair<string,int>>& visit_children, map<string, pair<string, int>>& pred)
{
visited_lines.insert(label_);//this line is visited
Station* temp_station = first_station;
do {
//if station isn't visited save its lines to visit them later
if (visited_stations.find(temp_station->getCode()) == visited_stations.end()) {
temp_station->markLines(label_, visited_stations, visited_lines, visit_children, pred);
}
//if you come to last station for the first time finish search
if (temp_station->getCode() == last_code) {
return true;
}
//go to next station on this line
temp_station = temp_station->getNeighbour(label_);
}while(temp_station != first_station );//go while loop is not finished
return false;
}
Line::Line(const string& text, int& pos):first_station_(nullptr),last_station_(nullptr){
while (pos < text.length() && text[pos] != ' ')label_ += text[pos++];//read label from text
}
void Line::setFirstStation(Station* station)
{
first_station_ = station;
}
void Line::setLastStation(Station* station)
{
last_station_ = station;
}