Compare commits
4 Commits
b569180ebd
...
main
Author | SHA1 | Date | |
---|---|---|---|
98f67bc7dc
|
|||
9219a29f62
|
|||
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;
|
||||
}
|
181
VL03-05-01/main.cc
Normal file
181
VL03-05-01/main.cc
Normal file
@@ -0,0 +1,181 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <ctime>
|
||||
|
||||
static std::random_device rd;
|
||||
static std::mt19937 rng{rd()};
|
||||
|
||||
class ICell {
|
||||
public:
|
||||
virtual void print(bool hide) = 0;
|
||||
virtual char type() = 0;
|
||||
};
|
||||
|
||||
class BombCell : public ICell {
|
||||
public:
|
||||
virtual void print(bool hide) {
|
||||
if (hide) {
|
||||
std::cout << "X";
|
||||
} else {
|
||||
std::cout << "B";
|
||||
}
|
||||
}
|
||||
virtual char type() {
|
||||
return 'b';
|
||||
}
|
||||
};
|
||||
|
||||
class UnknownCell : public ICell {
|
||||
public:
|
||||
virtual void print(bool hide) {
|
||||
std::cout << "X";
|
||||
}
|
||||
virtual char type() {
|
||||
return 'u';
|
||||
}
|
||||
};
|
||||
|
||||
class HintCell : public ICell {
|
||||
private:
|
||||
int bombs = 0;
|
||||
|
||||
public:
|
||||
HintCell(int bombs) {
|
||||
this->bombs = bombs;
|
||||
}
|
||||
virtual void print(bool hide) {
|
||||
std::cout << this->bombs;
|
||||
}
|
||||
virtual char type() {
|
||||
return 'h';
|
||||
}
|
||||
};
|
||||
|
||||
class Minesweeper {
|
||||
private:
|
||||
int rows;
|
||||
int columns;
|
||||
int bombs;
|
||||
|
||||
std::vector<std::vector<ICell*>> cells;
|
||||
|
||||
void init() {
|
||||
this->cells = std::vector<std::vector<ICell*>>(this->rows, std::vector<ICell*>(this->columns, new UnknownCell()));
|
||||
this->placeBombs();
|
||||
this->placeHints();
|
||||
}
|
||||
|
||||
int getRandomNumber(int min, int max) {
|
||||
std::uniform_int_distribution<int> uid(min, max);
|
||||
return uid(rng);
|
||||
}
|
||||
|
||||
bool isColumnIndex(int index) {
|
||||
return (index < 0 || index >= this->cells[0].size()) ? false : true;
|
||||
}
|
||||
|
||||
bool isRowIndex(int index) {
|
||||
return (index < 0 || index >= this->cells.size()) ? false : true;
|
||||
}
|
||||
|
||||
int getNearBombs(int row, int column) {
|
||||
int bombs = 0;
|
||||
|
||||
for (int offset_row : {-1, 0, 1}) {
|
||||
for (int offset_column : {-1, 0, 1}) {
|
||||
if (this->isRowIndex(row + offset_row) && this->isColumnIndex(column + offset_column)){
|
||||
if (this->cells[row + offset_row][column + offset_column]->type() == 'b') {
|
||||
bombs++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bombs;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
Minesweeper() {
|
||||
this->rows = 16;
|
||||
this->columns = 16;
|
||||
this->bombs = 99;
|
||||
this->init();
|
||||
}
|
||||
|
||||
Minesweeper(int rows, int columns, int bombs) {
|
||||
this->rows = rows;
|
||||
this->columns = columns;
|
||||
this->bombs = bombs;
|
||||
this->init();
|
||||
}
|
||||
|
||||
~Minesweeper() {
|
||||
for (int row = 0; row < this->rows; row++) {
|
||||
for (int column = 0; column < this->columns; column++) {
|
||||
delete this->cells[row][column];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void placeBombs() {
|
||||
int placed_bombs = 0;
|
||||
|
||||
while (placed_bombs != this->bombs) {
|
||||
int column = this->getRandomNumber(0, this->columns - 1);
|
||||
int row = this->getRandomNumber(0, this->rows - 1);
|
||||
|
||||
if (this->cells[row][column]->type() != 'b') {
|
||||
this->cells[row][column] = new BombCell();
|
||||
placed_bombs++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void placeHints() {
|
||||
for (int row = 0; row < this->rows; row++) {
|
||||
for (int column = 0; column < this->columns; column++) {
|
||||
if (this->cells[row][column]->type() == 'u') {
|
||||
int near_bombs = this->getNearBombs(row, column);
|
||||
|
||||
if (near_bombs > 0) {
|
||||
this->cells[row][column] = new HintCell(near_bombs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print(bool hide) {
|
||||
for (int row = 0; row < this->cells.size(); row++) {
|
||||
for (int column = 0; column < this->cells[row].size(); column++) {
|
||||
cells[row][column]->print(hide);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << "Wie viele Zeilen soll das Spielfeld haben? ";
|
||||
int rows;
|
||||
std::cin >> rows;
|
||||
|
||||
std::cout << "Wie viele Spalten soll das Spielfeld haben? ";
|
||||
int columns;
|
||||
std::cin >> columns;
|
||||
|
||||
std::cout << "Wie viele Bomben sollen platziert werden? ";
|
||||
int bombs;
|
||||
std::cin >> bombs;
|
||||
|
||||
std::cout << "Sollen Bomben sichtbar sein? ";
|
||||
char decision;
|
||||
std::cin >> decision;
|
||||
|
||||
Minesweeper* minesweeper = new Minesweeper(rows, columns, bombs);
|
||||
minesweeper->print((decision == 'y' || decision == 'Y') ? false : true);
|
||||
|
||||
return 0;
|
||||
}
|
13
VL03-06-01/main.cc
Normal file
13
VL03-06-01/main.cc
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <csignal>
|
||||
#include <iostream>
|
||||
|
||||
void signal_handler(int signal) {
|
||||
std::cout << "Es ist ein Segmentation Fault aufgetreten!";
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::signal(SIGSEGV, signal_handler);
|
||||
int* a = nullptr;
|
||||
*a = 5;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user