Compare commits
2 Commits
b569180ebd
...
b5adfe9750
Author | SHA1 | Date | |
---|---|---|---|
b5adfe9750 | |||
ea225bcc19 |
7585
VL03-04-01/input.csv
Normal file
7585
VL03-04-01/input.csv
Normal file
File diff suppressed because it is too large
Load Diff
149
VL03-04-01/main.cc
Normal file
149
VL03-04-01/main.cc
Normal file
@ -0,0 +1,149 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
|
||||
typedef struct CrimeReportRow {
|
||||
std::string address;
|
||||
std::string district;
|
||||
std::string crime;
|
||||
} CrimeReportRow;
|
||||
|
||||
class CrimeReport {
|
||||
private:
|
||||
std::vector<CrimeReportRow*> rows;
|
||||
|
||||
std::vector<std::string> split(const std::string& line, const std::string delimiter = ",") {
|
||||
std::vector<std::string> columns;
|
||||
std::string column;
|
||||
int last = 0;
|
||||
int next = 0;
|
||||
|
||||
while ((next = line.find(delimiter, last)) != std::string::npos) {
|
||||
columns.push_back(line.substr(last, next - last));
|
||||
last = next + 1;
|
||||
}
|
||||
|
||||
columns.push_back(line.substr(last));
|
||||
return columns;
|
||||
}
|
||||
|
||||
void read_rows(std::string filename) {
|
||||
std::ifstream file(filename);
|
||||
std::string line;
|
||||
int rownumber = 0;
|
||||
|
||||
if (file.is_open()) {
|
||||
while (std::getline(file, line)) {
|
||||
rownumber++;
|
||||
|
||||
// skip the first line of the file because it is the heading.
|
||||
if (rownumber == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<std::string> columns = split(line);
|
||||
|
||||
// skip rows with an unexpected amount of columns.
|
||||
if (columns.size() != 9) {
|
||||
std::cout << "Line " << rownumber << " is not valid!" << std::endl;
|
||||
}
|
||||
|
||||
CrimeReportRow* row = new CrimeReportRow;
|
||||
row->district = columns[2];
|
||||
row->address = columns[1];
|
||||
row->crime = columns[5];
|
||||
rows.push_back(row);
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
CrimeReport(std::string filename) {
|
||||
read_rows(filename);
|
||||
}
|
||||
|
||||
~CrimeReport() {
|
||||
for (CrimeReportRow* row : rows) {
|
||||
delete row;
|
||||
}
|
||||
|
||||
rows.clear();
|
||||
rows.shrink_to_fit();
|
||||
}
|
||||
|
||||
int get_number_of_crimes() {
|
||||
return rows.size();
|
||||
}
|
||||
|
||||
std::string get_most_frequent_crime() {
|
||||
std::map<std::string, int> crimes;
|
||||
|
||||
for (CrimeReportRow* row : rows) {
|
||||
crimes[row->crime] += 1;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> crime;
|
||||
|
||||
for (std::map<std::string, int>::iterator it = crimes.begin(); it != crimes.end(); it++) {
|
||||
if (it->second > crime.second) {
|
||||
crime = std::make_pair(it->first, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
return crime.first;
|
||||
}
|
||||
|
||||
std::string get_most_dangerous_district() {
|
||||
std::map<std::string, int> districts;
|
||||
|
||||
for (CrimeReportRow* row : rows) {
|
||||
districts[row->district] += 1;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> district;
|
||||
|
||||
for (std::map<std::string, int>::iterator it = districts.begin(); it != districts.end(); it++) {
|
||||
if (it->second > district.second) {
|
||||
district = std::make_pair(it->first, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
return district.first;
|
||||
}
|
||||
|
||||
std::vector<std::string> get_list_of_address_with_multiple_crimes() {
|
||||
std::map<std::string, int> addresses;
|
||||
|
||||
for (CrimeReportRow* row : rows) {
|
||||
addresses[row->address] += 1;
|
||||
}
|
||||
|
||||
std::vector<std::string> addresses_crimes;
|
||||
|
||||
for (std::map<std::string, int>::iterator it = addresses.begin(); it != addresses.end(); it++) {
|
||||
if (it->second > 1) {
|
||||
addresses_crimes.push_back(it->first);
|
||||
}
|
||||
}
|
||||
|
||||
return addresses_crimes;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
CrimeReport* report = new CrimeReport("input.csv");
|
||||
std::cout << "Anzahl der Verbrechen: " << report->get_number_of_crimes() << std::endl;
|
||||
std::cout << "Häufigstes Verbrechen: " << report->get_most_frequent_crime() << std::endl;
|
||||
std::cout << "Gefährlichster District: " << report->get_most_dangerous_district() << std::endl;
|
||||
|
||||
std::cout << "Adresse mit mehr als einem Verbrechen: " << std::endl;
|
||||
|
||||
for (std::string address : report->get_list_of_address_with_multiple_crimes()) {
|
||||
std::cout << "\t" << address << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
58
VL03-04-02/main.cc
Normal file
58
VL03-04-02/main.cc
Normal file
@ -0,0 +1,58 @@
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::string coder = "";
|
||||
std::string encoder = "";
|
||||
const int max_attempts = 8;
|
||||
int attempts = max_attempts;
|
||||
|
||||
std::cout << "~~ Mastermind ~~" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "Konfiguration:" << std::endl;
|
||||
std::cout << "\tFarben: 8 (1 bis 8)" << std::endl;
|
||||
std::cout << "\tCodelänge: 4" << std::endl;
|
||||
std::cout << "\tMehrfache Farben: Nein" << std::endl;
|
||||
std::cout << "\tVersuche: " << max_attempts << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "Kodierer: ";
|
||||
std::cin >> coder;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
while (encoder != coder && attempts > 0) {
|
||||
int current_attempt = (max_attempts - attempts) + 1;
|
||||
std::cout << "Versuch " << current_attempt << " von " << max_attempts << ": ";
|
||||
std::cin >> encoder;
|
||||
|
||||
std::string feedback = "";
|
||||
int black = 0;
|
||||
int white = 0;
|
||||
|
||||
for (int i = 0; i < coder.length(); i++) {
|
||||
if (encoder[i] == coder[i]) {
|
||||
black++;
|
||||
} else {
|
||||
if (coder.find(encoder[i]) != std::string::npos) {
|
||||
white++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attempts -= 1;
|
||||
|
||||
if (encoder == coder) {
|
||||
std::cout << std::endl << "Du hast gewonnen!" << std::endl;
|
||||
} else {
|
||||
std::cout << "Feedback: " << std::string(black, 'B') << std::string(white, 'W') << std::endl;
|
||||
|
||||
if (current_attempt == max_attempts) {
|
||||
std::cout << std::endl << "Du hast keine Versuche mehr!" << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user