CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
commit c57c80e7e0ce5cc57c605b39e63c2a1af1dc2437
parent 0a0530170036e6bdaf8c9fbf6ed47e8a3320eaa6
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Thu, 20 Jun 2024 01:59:50 -0300

create function `getTokens`

Diffstat:
Mmain.cpp | 39++++++++++++++++++++++++++++-----------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/main.cpp b/main.cpp
@@ -3,6 +3,7 @@
 #include <string>
 #include <sstream>
 #include <cstdlib>
+#include <vector>
 
 #include "barrio.h"
 #include "parada.h"
@@ -31,6 +32,27 @@ enum {
     LINEA_6_SENTIDO
 };
 
+size_t getTokens(std::string line, std::vector<std::string> &tokens) {
+    std::string token;
+    std::stringstream lineStream;
+    size_t field;
+
+    lineStream.clear();
+    lineStream.str(line);
+
+    for(field = CALLE; std::getline(lineStream, token, CSV_DELIM); ++field) {
+        // solucion a `token` es una cadena que contiene CSV_DELIM
+        while((token[0] == '"') && (token[token.size() - 1] != '"')) {
+            std::string nextToken;
+            std::getline(lineStream, nextToken, CSV_DELIM);
+            token += CSV_DELIM + nextToken;
+        }
+        tokens.push_back(token);
+    }
+
+    return field;
+}
+
 void delSurroundingChar(std::string &str, char c) {
     str.erase(0, 1);
     str[str.size() - 1] = '\0';
@@ -39,7 +61,7 @@ void delSurroundingChar(std::string &str, char c) {
 int main (void) {
     std::ifstream inputFile;
     std::string line, token;
-    std::stringstream lineStream;
+    std::vector<std::string> tokens;
 
     int comuna, alturaPlano;
     double coordX, coordY;
@@ -57,17 +79,12 @@ int main (void) {
     coordX = coordY = 0.0f;
 
     std::getline(inputFile, line); // saltea la primer linea
-    while(std::getline(inputFile, line)) {
-        lineStream.str(line);
-
-        for(size_t field = CALLE; std::getline(lineStream, token, CSV_DELIM); ++field) {
-            // solucion a `token` es una cadena que contiene CSV_DELIM
-            while((token[0] == '"') && (token[token.size() - 1] != '"')) {
-                std::string nextToken;
-                std::getline(lineStream, nextToken, CSV_DELIM);
-                token += CSV_DELIM + nextToken;
-            }
 
+    while(std::getline(inputFile, line)) {
+        tokens.clear();
+        fields = getTokens(line, tokens);
+        for(int field = CALLE; field <= fields; ++field) {
+            token = tokens[field];
             switch(field) {
             case CALLE:
                 calle = token;