diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 237ded7..c3f3b98 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(search-algorithms ./main.cpp + ./algorithm.cpp ) target_include_directories(search-algorithms PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/src/algorithm.cpp b/src/algorithm.cpp new file mode 100644 index 0000000..64f0fc2 --- /dev/null +++ b/src/algorithm.cpp @@ -0,0 +1,52 @@ +#include "algorithm.hpp" +#include +#include +#include +#include + +Graph ReadInGraph(std::string fileLocation) { + std::cout << fileLocation << std::endl; + std::ifstream graphFile(fileLocation); + if (!graphFile.is_open()) { + std::cerr << "Error opening file, check filename" << std::endl; + exit(1); + } + Graph newGraph; + graphFile >> newGraph.nodeCount; + newGraph.nodes.resize(newGraph.nodeCount + 1); + std::string line; + int source, destination, weight; + while (std::getline(graphFile, line)) { + std::istringstream iss(line); + iss >> source; + do { + iss >> destination >> weight; + newGraph.AddChild(source, destination, weight); + } while (!iss.eof()); + } + graphFile.close(); + return newGraph; +} + +Node::Node(int destination, int weight) { + this->destination = destination; + this->weight = weight; +} + +Graph::Graph(void) { + nodeCount = -1; +} + +void Graph::AddChild(int source, int destination, int weight) { + nodes.at(source).push_back(Node(destination, weight)); +} + +void Graph::PrintGraph(void) { + for (int i = 1; i < nodes.size(); i++) { + std::cout << "Source " << i << " "; + for (auto j : nodes.at(i)) { std::cout << "-> " << j.destination; } + std::cout << std::endl; + } + std::cout << std::endl; +} + diff --git a/src/algorithm.hpp b/src/algorithm.hpp new file mode 100644 index 0000000..e2a3f37 --- /dev/null +++ b/src/algorithm.hpp @@ -0,0 +1,23 @@ +#ifndef ALGORITHM_HPP +#define ALGORITHM_HPP + +#include +#include + +struct Node { + int destination; + int weight; + Node(int destination, int weight); +}; + +struct Graph { + int nodeCount; + std::vector> nodes; + Graph(void); + void AddChild(int source, int destination, int weight); + void PrintGraph(void); +}; + +Graph ReadInGraph(std::string fileLocation); + +#endif diff --git a/src/main.cpp b/src/main.cpp index 8077094..d2eb6e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,12 @@ +#include "algorithm.hpp" #include -int main () { - std::cout << "Hello world" << std::endl; +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "Usage: search-algorithms graphFile.txt" << std::endl; + return 1; + } + Graph newGraph = ReadInGraph(argv[1]); + newGraph.PrintGraph(); return 0; }