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 "Aufg1/forLoop.h"
|
||||
#include "Aufg2/GuestList.h"
|
||||
@ -9,4 +11,4 @@ int main() {
|
||||
// Aufg2Main();
|
||||
// Aufg3Main();
|
||||
Aufg4Main();
|
||||
}
|
||||
}
|
@ -4,17 +4,23 @@
|
||||
#include <fstream>
|
||||
|
||||
#include "CrimeStats.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
|
||||
#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<std::string>& v) {
|
||||
std::string::size_type i = 0;
|
||||
@ -55,6 +61,19 @@ std::string readFile(std::string &fileName, std::vector<Crime*> &allCrimes) {
|
||||
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) {
|
||||
std::vector<std::string> 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<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() {
|
||||
std::string fileName = "../Aufg4/IO-Files/SacramentocrimeJanuary2006.csv";
|
||||
std::vector<Crime*> 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);
|
||||
}
|
@ -15,6 +15,18 @@ typedef struct CRIME{
|
||||
std::string longitude;
|
||||
} 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 Aufg4Main();
|
||||
|
Loading…
x
Reference in New Issue
Block a user