Kajetan Johannes Hammerle 3 anni fa
parent
commit
5e72939d9c
5 ha cambiato i file con 133 aggiunte e 3 eliminazioni
  1. 9 1
      Main.cpp
  2. 7 2
      Makefile
  3. 92 0
      Reader.cpp
  4. 25 0
      Reader.h
  5. BIN
      Reader.o

+ 9 - 1
Main.cpp

@@ -1,6 +1,14 @@
 #include <iostream>
 
+#include "Reader.h"
+
 int main() {
-    std::cout << "Test\n";
+    Reader r;
+    if(r.read("romaniaroads.pl")) {
+        return 0;
+    }
+    r.print();
+    std::cout << "distance from " << r.getName(0) << " to " << r.getName(1)
+              << " is " << r[0][1] << "\n";
     return 0;
 }

+ 7 - 2
Makefile

@@ -1,5 +1,10 @@
+FLAGS = -Wall -Wextra -pedantic -Werror -O3
+
 run: build
 	./vsp
 
-build: Main.cpp 
-	g++ -o vsp Main.cpp -Wall -Wextra -pedantic -Werror -O3
+build: Main.cpp Reader.o
+	g++ -o vsp Main.cpp Reader.o ${FLAGS}
+
+Reader.o: Reader.cpp Reader.h
+	g++ -c Reader.cpp ${FLAGS}

+ 92 - 0
Reader.cpp

@@ -0,0 +1,92 @@
+#include <fstream>
+#include <iostream>
+
+#include "Reader.h"
+
+const std::string& Reader::getName(int id) {
+    return names[id];
+}
+
+const std::vector<int>& Reader::operator[](int x) const {
+    return distances[x];
+}
+
+int Reader::getOrAddId(const std::string& s) {
+    auto found = nameToId.find(s);
+    if(found != nameToId.end()) {
+        return found->second;
+    }
+    int id = ids++;
+    nameToId[s] = id;
+    for(std::vector<int>& inner : distances) {
+        inner.push_back(-1);
+    }
+    distances.push_back(std::vector<int>(ids, -1));
+    distances.back()[ids - 1] = 0;
+    names.push_back(s);
+    return id;
+}
+
+std::string Reader::trim(const std::string& s) const {
+    size_t start = 0;
+    while(start < s.length() && s[start] == ' ') {
+        start++;
+    }
+    size_t end = s.length();
+    while(end > 0 && s[end - 1] == ' ') {
+        end--;
+    }
+    return s.substr(start, end - start);
+}
+
+bool Reader::read(const char* path) {
+    std::ifstream in;
+    in.open(path);
+    if(!in.good()) {
+        std::cout << "cannot find data\n";
+        return true;
+    }
+    while(!in.eof()) {
+        std::string s;
+        std::getline(in, s);
+        if(s.find("road(", 0) != 0) {
+            continue;
+        }
+        s = s.substr(5, s.length() - 5);
+
+        size_t fromIndex = s.find(",", 0);
+        if(fromIndex == s.npos) {
+            continue;
+        }
+        size_t toIndex = s.find(",", fromIndex + 1);
+        if(toIndex == s.npos) {
+            continue;
+        }
+        size_t distanceIndex = s.find(")", toIndex + 1);
+        if(distanceIndex == s.npos) {
+            continue;
+        }
+
+        std::string from = trim(s.substr(0, fromIndex));
+        std::string to = trim(s.substr(fromIndex + 1, toIndex - fromIndex - 1));
+        std::string distanceString =
+            trim(s.substr(toIndex + 1, distanceIndex - toIndex - 1));
+
+        int fromId = getOrAddId(from);
+        int toId = getOrAddId(to);
+        int distance = std::stoi(distanceString);
+
+        distances[fromId][toId] = distance;
+        distances[toId][fromId] = distance;
+    }
+    return false;
+}
+
+void Reader::print() const {
+    for(size_t x = 0; x < distances.size(); x++) {
+        for(size_t y = 0; y < distances[x].size(); y++) {
+            printf("%3d ", distances[x][y]);
+        }
+        std::cout << "\n";
+    }
+}

+ 25 - 0
Reader.h

@@ -0,0 +1,25 @@
+#ifndef READER_H
+#define READER_H
+
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+class Reader final {
+    int ids = 0;
+    std::unordered_map<std::string, int> nameToId;
+    std::vector<std::string> names;
+    std::vector<std::vector<int>> distances;
+
+public:
+    bool read(const char* path);
+    void print() const;
+    const std::string& getName(int id);
+    const std::vector<int>& operator[](int x) const;
+
+private:
+    int getOrAddId(const std::string& s);
+    std::string trim(const std::string& s) const;
+};
+
+#endif

BIN
Reader.o