108 lines
2.7 KiB
C++
108 lines
2.7 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <fstream>
|
|
|
|
#include "CrimeStats.h"
|
|
#include "../Aufg2/GuestList.h"
|
|
|
|
// cdatetime,
|
|
// address,
|
|
// district,
|
|
// beat,
|
|
// grid,
|
|
// crimedescr,
|
|
// ucr_ncic_code,
|
|
// latitude,
|
|
// longitude,
|
|
|
|
void split(const std::string& s, char c,std::vector<std::string>& v) {
|
|
std::string::size_type i = 0;
|
|
std::string::size_type j = s.find(c);
|
|
|
|
while (j != std::string::npos) {
|
|
v.push_back(s.substr(i, j-i));
|
|
i = ++j;
|
|
j = s.find(c, j);
|
|
|
|
if (j == std::string::npos)
|
|
v.push_back(s.substr(i, s.length()));
|
|
}
|
|
}
|
|
|
|
std::string readFile(std::string &fileName, std::vector<Crime*> &allCrimes) {
|
|
std::string content;
|
|
std::ifstream infile;
|
|
infile.open(fileName);
|
|
|
|
if (!infile.is_open()) {
|
|
std::cout << "File does not exist" << std::endl;
|
|
return "FAILED_TO_READ_FILE";
|
|
}
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(infile, line)) {
|
|
content += line;
|
|
|
|
std::vector<std::string> actualLines;
|
|
split(line, '\r', actualLines);
|
|
|
|
for (auto actual_line: actualLines) {
|
|
allCrimes.push_back(stringToCrime(actual_line));
|
|
}
|
|
}
|
|
return content;
|
|
}
|
|
|
|
Crime* stringToCrime(std::string &input) {
|
|
std::vector<std::string> tokens;
|
|
split(input, ',', tokens);
|
|
|
|
Crime* thisCrime = new Crime;
|
|
for (int i = 0; i < tokens.size(); i++) {
|
|
switch (i%tokens.size()) {
|
|
case 0:
|
|
thisCrime->cdatetime = tokens[0];
|
|
break;
|
|
case 1:
|
|
thisCrime->address = tokens[1];
|
|
break;
|
|
case 2:
|
|
thisCrime->district = tokens[2];
|
|
break;
|
|
case 3:
|
|
thisCrime->beat = tokens[3];
|
|
break;
|
|
case 4:
|
|
thisCrime->grid = tokens[4];
|
|
break;
|
|
case 5:
|
|
thisCrime->crimedescription = tokens[5];
|
|
break;
|
|
case 6:
|
|
thisCrime->ucr_ncic_code = tokens[6];
|
|
break;
|
|
case 7:
|
|
thisCrime->latitude = tokens[7];
|
|
break;
|
|
case 8:
|
|
thisCrime->longitude = tokens[8];
|
|
break;
|
|
default:
|
|
std::cout << "I am the Default case " << tokens.size() << std::endl;
|
|
}
|
|
}
|
|
return thisCrime;
|
|
}
|
|
|
|
void Aufg4Main() {
|
|
std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv";
|
|
std::vector<Crime*> allCrimes;
|
|
std::string content = readFile(fileName, allCrimes);
|
|
|
|
// std::cout << content << std::endl << "EndOfFile" << std::endl;
|
|
|
|
std::cout << allCrimes.at(2)->crimedescription << std::endl;
|
|
|
|
} |