From 709475cfc3f77732ab819ae546acd135b59df408 Mon Sep 17 00:00:00 2001 From: Matti Date: Tue, 12 Nov 2024 15:21:12 +0100 Subject: [PATCH] Finish functional Crime Analysis --- ActualMain.cpp | 4 +- Aufg4/CrimeStats.cpp | 127 ++++++++++++++++++++++++++++++++++++++++--- Aufg4/CrimeStats.h | 12 ++++ 3 files changed, 133 insertions(+), 10 deletions(-) diff --git a/ActualMain.cpp b/ActualMain.cpp index 79f53e6..0a8ab50 100644 --- a/ActualMain.cpp +++ b/ActualMain.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include "Aufg1/forLoop.h" #include "Aufg2/GuestList.h" @@ -9,4 +11,4 @@ int main() { // Aufg2Main(); // Aufg3Main(); Aufg4Main(); -} +} \ No newline at end of file diff --git a/Aufg4/CrimeStats.cpp b/Aufg4/CrimeStats.cpp index 220d5e3..f0c6919 100644 --- a/Aufg4/CrimeStats.cpp +++ b/Aufg4/CrimeStats.cpp @@ -4,17 +4,23 @@ #include #include "CrimeStats.h" + +#include +#include + #include "../Aufg2/GuestList.h" -// cdatetime, -// address, -// district, -// beat, -// grid, -// crimedescr, -// ucr_ncic_code, -// latitude, -// longitude, +/* +* # 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; @@ -55,6 +61,19 @@ std::string readFile(std::string &fileName, std::vector &allCrimes) { 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); @@ -96,6 +115,87 @@ Crime* stringToCrime(std::string &input) { 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; @@ -103,6 +203,15 @@ void Aufg4Main() { // 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); } \ No newline at end of file diff --git a/Aufg4/CrimeStats.h b/Aufg4/CrimeStats.h index 69189dc..6759770 100644 --- a/Aufg4/CrimeStats.h +++ b/Aufg4/CrimeStats.h @@ -15,6 +15,18 @@ typedef struct CRIME{ std::string longitude; } Crime; +class CRIMESTATS { + public: + std::vector crimes; + int countCrimes(); + std::string getMostCommonCrime(); + std::string getMostDangerousDistrict(); + + std::vector *getDuplicateAdresses(); +}; + +bool isInVector(std::string& str, std::vector* vec); + void split(const std::string& s, char c,std::vector& v); void Aufg4Main();