forked from dcausey1/Project3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
173 lines (140 loc) · 5.06 KB
/
main.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
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <memory>
#include <set>
#include <algorithm>
#include <map>
class Airline {
public:
Airline(std::string name) : name(name), totalMinutesDelayed(0) {}
std::string getName() const {
return name;
}
void addMinutesDelayed(int minutes) {
totalMinutesDelayed += minutes;
}
int getTotalMinutesDelayed() const {
return totalMinutesDelayed;
}
private:
std::string name;
int totalMinutesDelayed;
};
class AirportBST {
private:
struct AirportNode {
AirportNode(std::string airportCode) : code(airportCode) {}
std::string getCode() const {
return code;
}
void addAirline(std::string airlineName) {
// Check if the airline already exists in the map
auto it = airlines.find(airlineName);
if (it == airlines.end()) {
// If the airline does not exist, create a new entry
airlines.emplace(airlineName, Airline(airlineName));
it = airlines.find(airlineName); // Update the iterator after emplacing the new entry
}
// Update the total minutes delayed for the airline
}
const std::map<std::string, Airline>& getAirlines() const {
return airlines;
}
std::shared_ptr<AirportNode> left;
std::shared_ptr<AirportNode> right;
private:
std::string code;
std::map<std::string, Airline> airlines;
};
public:
AirportBST() : root(nullptr) {}
void insertAirport(std::string airportCode) {
root = insertRecursive(root, airportCode);
}
void addAirlineToAirport(std::string airportCode, std::string airlineName) {
addAirlineToAirportRecursive(root, airportCode, airlineName);
}
void printInOrder() const {
printInOrderRecursive(root);
}
private:
std::shared_ptr<AirportNode> root;
// Private helper functions for tree operations
std::shared_ptr<AirportNode> insertRecursive(std::shared_ptr<AirportNode> currentNode, std::string airportCode) {
if (currentNode == nullptr) {
return std::make_shared<AirportNode>(airportCode);
}
if (airportCode < currentNode->getCode()) {
currentNode->left = insertRecursive(currentNode->left, airportCode);
} else if (airportCode > currentNode->getCode()) {
currentNode->right = insertRecursive(currentNode->right, airportCode);
}
return currentNode;
}
void addAirlineToAirportRecursive(std::shared_ptr<AirportNode> currentNode, std::string airportCode, std::string airlineName) {
if (currentNode != nullptr) {
if (currentNode->getCode() == airportCode) {
currentNode->addAirline(airlineName);
} else if (airportCode < currentNode->getCode()) {
addAirlineToAirportRecursive(currentNode->left, airportCode, airlineName);
} else {
addAirlineToAirportRecursive(currentNode->right, airportCode, airlineName);
}
}
}
void printInOrderRecursive(std::shared_ptr<AirportNode> currentNode) const {
if (currentNode != nullptr) {
printInOrderRecursive(currentNode->left);
std::cout << "Airport: " << currentNode->getCode() << std::endl << " Airlines: ";
for (const auto& airlinePair : currentNode->getAirlines()) {
std::cout << airlinePair.first << ", ";
}
std::cout << std::endl;
printInOrderRecursive(currentNode->right);
}
}
};
int main() {
AirportBST airportBST;
// Read data from CSV file and populate the AirportBST
std::ifstream inputFile("airlines.csv");
if (!inputFile.is_open()) {
std::cerr << "Error opening the CSV file." << std::endl;
return 1;
}
int minutesdelayed;
std::string line;
std::getline(inputFile, line); // Skip the header line
std::vector<std::string> elements;
std::vector<int> late;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
std::string airportCode, carriers;
// Read the columns from the CSV line
while (std::getline(iss, airportCode, ',')) {
while (std::getline(iss, carriers, ',')) {
if (std::all_of(carriers.begin(), carriers.end(), ::isdigit)) {
minutesdelayed = std::stoi(carriers);
late.push_back(minutesdelayed);
break;
} else {
elements.push_back(carriers);
}
}
break;
}
airportBST.insertAirport(airportCode);
for (size_t i = 0; i < elements.size(); ++i) {
airportBST.addAirlineToAirport(airportCode, elements[i]);
}
// Clear the vectors for the next iteration
elements.clear();
late.clear();
}
// Print all airports and their airlines
airportBST.printInOrder();
return 0;
}