-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++Code.cpp
More file actions
191 lines (166 loc) · 4.73 KB
/
C++Code.cpp
File metadata and controls
191 lines (166 loc) · 4.73 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
//read in a text file of interconnected cities.
//get user input for from/to
//give list of potential cities if input is partial
//find and display shortest route to get from start to finish
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <list>
using namespace std;
//pull city names from file
int processFile(string cityName[], string f);
//pull city from/to info from file
void createVector(vector<int> arr[], string cityName[], int size, string fileName);
//get user input, will return list of potential inputs if no match
int getUserInput(string seek, string cityName[], int size);
//Dijkstras algorithm implementation
void bfs (vector<int> alists[], int size, int start, int target,string arr[]);
int main(){
string fileName;
int startCity;
int endCity;
string temp;
bool running = true;
cout << "Enter filename:";
cin >> fileName;
string cityNames[150];
vector<int> connections[150];
int size = processFile(cityNames,fileName);
createVector(connections, cityNames, size,fileName);
while(running){
cout << "Enter starting city: ";
cin >> fileName;
startCity = getUserInput(fileName,cityNames,size);
cout << "\nEnter destination city: ";
cin >> fileName;
endCity = getUserInput(fileName,cityNames,size);
bfs(connections,size,startCity,endCity,cityNames);
cout << "\b\b\b\b";
cout << "\nSearch again? Y or N";
cin >> fileName;
if(fileName == "N")
running = false;
}
}
bool checkArr(string cityName[], string city, int size){
for(int i = 0;i<size;i++){
if(cityName[i] == city){
return false;
}
}
cityName[size] = city;
return true;
}
//open file first time
//parse file into strings and push them into array
//return # of cities in array
int processFile(string cityName[], string f){
ifstream inFile(f);
string currCity;
int size = 0;
while(getline(inFile,currCity)){
currCity = currCity.substr(7,currCity.length());
if(checkArr(cityName,currCity,size))
size++;
}
return size;
}
//find index of City in array
int getIndex(string cityName[],string seekCity, int size){
for(int i = 0;i<size;i++){
if(cityName[i] == seekCity){
return i;
}
}
return -1;
}
//open file again, for each city starting with the word "from:" find it in array cityName
//then push_back of vector at that index with the index of the connected city
//until next "from:" encountered
void createVector(vector<int> cityConnection[], string cityName[], int size, string fileName){
ifstream inFile(fileName);
string currCity;
int currIndex;
int pushIndex;
size_t checkFrom;
//make sure at start of file
inFile.clear();
inFile.seekg(0,inFile.beg);
while(getline(inFile,currCity)){
//check if line starts with "From:"
checkFrom = currCity.find("From:");
if(checkFrom != string::npos){
//from was found in string, means new city/new index needed;
currCity = currCity.substr(7,currCity.length());
currIndex = getIndex(cityName, currCity,size);
}
//no from found, get city index, push that into vector at current index
else if(checkFrom == string::npos){
currCity = currCity.substr(7,currCity.length());
pushIndex = getIndex(cityName,currCity,size);
cityConnection[currIndex].push_back(pushIndex);
}
}
}
//check arr of strings for exact match, if not then assemble list of potentials and have them pick one, then get index of that city and return it
int getUserInput(string seek,string cityName[],int size){
int x = getIndex(cityName,seek,size);
cout << x;
if(x != -1){
return x;
}
else{
//get an array of potential inputs
string potentials[100];
int y = 0;
for(int i = 0;i<size;i++){
if(cityName[i].find(seek) != string::npos){
potentials[y] = cityName[i];
y++;
}
}
cout << "\nNo match, potential matches found: " << y;
cout << "\nPlease choose number you meant.";
for(int i = 0;i<y;i++){
cout << i+1 << ". " << potentials[i] << "\n";
}
cin >> y;
int x = getIndex(cityName,potentials[y-1],size);
return x;
}
}
void printPath(int parents[], int size, int startv, int endv, string arr[]) {
if (endv != startv) {
printPath(parents, size, startv, parents[endv], arr);
}
cout << arr[endv] << " -> ";
}
void bfs (vector<int> alists[], int size, int start, int target, string arr[]) {
int * parents = new int[size];
for (int i = 0; i< size; i++) parents[i] = -1;
parents[start] = start;
queue<int> q;
q.push(start);
bool found = false;
while (!q.empty() && !found) {
int v = q.front();
q.pop();
if (v == target)
found = true;
else for (int i = 0; i < alists[v].size(); i++) {
int w = alists[v][i];
if (parents[w] == -1) {
parents[w] = v;
q.push(w);
}
}
}
if (found)
printPath(parents,10,start,target,arr);
else
cout << "Not found";
cout << endl;
delete [] parents;
}