Finish functional Crime Analysis
This commit is contained in:
parent
01292040fe
commit
709475cfc3
@ -1,3 +1,5 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <ostream>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "Aufg1/forLoop.h"
|
#include "Aufg1/forLoop.h"
|
||||||
#include "Aufg2/GuestList.h"
|
#include "Aufg2/GuestList.h"
|
||||||
|
@ -4,17 +4,23 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include "CrimeStats.h"
|
#include "CrimeStats.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "../Aufg2/GuestList.h"
|
#include "../Aufg2/GuestList.h"
|
||||||
|
|
||||||
// cdatetime,
|
/*
|
||||||
// address,
|
* # Aufgabe 2
|
||||||
// district,
|
* Erstelle ein Objekt, welches auf die Zeilen aus
|
||||||
// beat,
|
* der beigefügten CSV und lese diese ein
|
||||||
// grid,
|
* Beantworte die folgenden Fragen:
|
||||||
// crimedescr,
|
* Wie viele Verbrechen gab es in Sacramento in 2006?
|
||||||
// ucr_ncic_code,
|
* Was war das häufigste Verbrechen?
|
||||||
// latitude,
|
* Welches ist der gefährlichste Desctrict? (gemessen an
|
||||||
// longitude,
|
der Menge der Vorfälle?)
|
||||||
|
* Welche Adressen kamen mehr als einmal vor
|
||||||
|
*/
|
||||||
|
|
||||||
void split(const std::string& s, char c,std::vector<std::string>& v) {
|
void split(const std::string& s, char c,std::vector<std::string>& v) {
|
||||||
std::string::size_type i = 0;
|
std::string::size_type i = 0;
|
||||||
@ -55,6 +61,19 @@ std::string readFile(std::string &fileName, std::vector<Crime*> &allCrimes) {
|
|||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isInVector(std::string &str, std::vector<std::string> *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) {
|
Crime* stringToCrime(std::string &input) {
|
||||||
std::vector<std::string> tokens;
|
std::vector<std::string> tokens;
|
||||||
split(input, ',', tokens);
|
split(input, ',', tokens);
|
||||||
@ -96,6 +115,87 @@ Crime* stringToCrime(std::string &input) {
|
|||||||
return thisCrime;
|
return thisCrime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CRIMESTATS::countCrimes() {
|
||||||
|
return crimes.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CRIMESTATS::getMostCommonCrime() {
|
||||||
|
std::vector<std::string> crime;
|
||||||
|
std::vector<int> 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<std::string> district;
|
||||||
|
std::vector<int> 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<std::string> *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<std::string>* result = new std::vector<std::string>;
|
||||||
|
std::vector<std::string> 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() {
|
void Aufg4Main() {
|
||||||
std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv";
|
std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv";
|
||||||
std::vector<Crime*> allCrimes;
|
std::vector<Crime*> allCrimes;
|
||||||
@ -103,6 +203,15 @@ void Aufg4Main() {
|
|||||||
|
|
||||||
// std::cout << content << std::endl << "EndOfFile" << std::endl;
|
// std::cout << content << std::endl << "EndOfFile" << std::endl;
|
||||||
|
|
||||||
|
std::cout << allCrimes.size() << std::endl;
|
||||||
std::cout << allCrimes.at(2)->crimedescription << 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);
|
||||||
}
|
}
|
@ -15,6 +15,18 @@ typedef struct CRIME{
|
|||||||
std::string longitude;
|
std::string longitude;
|
||||||
} Crime;
|
} Crime;
|
||||||
|
|
||||||
|
class CRIMESTATS {
|
||||||
|
public:
|
||||||
|
std::vector<Crime*> crimes;
|
||||||
|
int countCrimes();
|
||||||
|
std::string getMostCommonCrime();
|
||||||
|
std::string getMostDangerousDistrict();
|
||||||
|
|
||||||
|
std::vector<std::string> *getDuplicateAdresses();
|
||||||
|
};
|
||||||
|
|
||||||
|
bool isInVector(std::string& str, std::vector<std::string>* vec);
|
||||||
|
|
||||||
void split(const std::string& s, char c,std::vector<std::string>& v);
|
void split(const std::string& s, char c,std::vector<std::string>& v);
|
||||||
|
|
||||||
void Aufg4Main();
|
void Aufg4Main();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user