#include #include #include #include #include "CrimeStats.h" #include #include #include "../Aufg2/GuestList.h" /* * # Aufgabe 2 * Erstelle ein Objekt, welches auf die Zeilen aus * der beigefügten CSV und lese diese ein * Beantworte die folgenden Fragen: * Wie viele Verbrechen gab es in Sacramento in 2006? * Was war das häufigste Verbrechen? * Welches ist der gefährlichste Desctrict? (gemessen an der Menge der Vorfälle?) * Welche Adressen kamen mehr als einmal vor */ void split(const std::string& s, char c,std::vector& 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 &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 actualLines; split(line, '\r', actualLines); for (auto actual_line: actualLines) { allCrimes.push_back(stringToCrime(actual_line)); } } return content; } bool isInVector(std::string &str, std::vector *vec) { bool found = false; for (int i = 0; i < vec->size(); i++) { if (str == (*vec)[i]) { found = true; } } return found; } Crime* stringToCrime(std::string &input) { std::vector 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; } int CRIMESTATS::countCrimes() { return crimes.size(); } std::string CRIMESTATS::getMostCommonCrime() { std::vector crime; std::vector counter; for (auto singleCrime : crimes) { bool found = false; for (int i = 0; i < crime.size(); ++i) { if (singleCrime->crimedescription == crime[i]) { counter[i] += 1; found = true; } } if (!found) { crime.push_back(singleCrime->crimedescription); counter.push_back(1); } } auto maxElementIt = std::max_element(counter.begin(), counter.end()); int maxIndex = std::distance(counter.begin(), maxElementIt); return crime[maxIndex]; } std::string CRIMESTATS::getMostDangerousDistrict() { std::vector district; std::vector counter; for (auto singleCrime : crimes) { bool found = false; for (int i = 0; i < district.size(); ++i) { if (singleCrime->district == district[i]) { counter[i] += 1; found = true; } } if (!found) { district.push_back(singleCrime->district); counter.push_back(1); } } auto maxElementIt = std::max_element(counter.begin(), counter.end()); int maxIndex = std::distance(counter.begin(), maxElementIt); return district[maxIndex]; } std::vector *CRIMESTATS::getDuplicateAdresses() { // for crime // if crime.address in crimeAdresses // if adress not in result // result.add(crime.address) // else // crimeAdresses.add(crime.adress) // return result std::vector* result = new std::vector; std::vector breakInAdresses; for (auto singleCrime: crimes) { std::string address = singleCrime->address; if (isInVector(address, &breakInAdresses)) { if (!isInVector(address, result)) { result->push_back(address); } } else{ breakInAdresses.push_back(address); } } return result; } void Aufg4Main() { std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv"; std::vector allCrimes; std::string content = readFile(fileName, allCrimes); // std::cout << content << std::endl << "EndOfFile" << std::endl; std::cout << allCrimes.size() << std::endl; std::cout << allCrimes.at(2)->crimedescription << std::endl; CRIMESTATS crimestats; crimestats.crimes = allCrimes; std::cout << "Number of Crimes : " << crimestats.countCrimes() << std::endl; std::cout << "Most Common Crime : " << crimestats.getMostCommonCrime() << std::endl; std::cout << "Most Dangerous District : " << crimestats.getMostDangerousDistrict() << std::endl; auto dupeAddresses = crimestats.getDuplicateAdresses(); std::cout << "Multi-Crime-Addresses : " << dupeAddresses->size() << std::endl; printAllInVector(*dupeAddresses); }