From 3ef4f4e1b0a093b28fe8aef26d5e6f05684377ef Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Mon, 26 Nov 2018 22:16:00 +0100 Subject: [PATCH] 07_STD_MP: Vorbereitung aufs Testat --- src/07_STD/MP/BinaryOctet.cpp | 64 ++++++++++++++++------------------ src/07_STD/MP/BinaryOctet.h | 4 ++- src/07_STD/MP/grundgeruest.cpp | 59 +++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 41 deletions(-) diff --git a/src/07_STD/MP/BinaryOctet.cpp b/src/07_STD/MP/BinaryOctet.cpp index a4e9231..cc470f1 100644 --- a/src/07_STD/MP/BinaryOctet.cpp +++ b/src/07_STD/MP/BinaryOctet.cpp @@ -88,19 +88,44 @@ BinaryOctet BinaryOctet::operator/(BinaryOctet &a) { while(*this >= a) { *this = *this - a; - result = result + 1; + result = result + BinaryOctet(1); } return result; } -bool BinaryOctet::operator!=(BinaryOctet &a) { - if (a.evenParity !=evenParity) return false; +bool BinaryOctet::operator==(const BinaryOctet &a) { + if(a.evenParity != evenParity) return false; for (int i = 0; i < bitsPerOctet; ++i) { - if (a.bitsAsDigits[i] != bitsAsDigits[i]) return false; + if(a.bitsAsDigits[i] != bitsAsDigits[i]) return false; } return true; } +bool BinaryOctet::operator!=(BinaryOctet &a) { + if (a.evenParity != evenParity) return true; + for (int i = 0; i < bitsPerOctet; ++i) { + if (a.bitsAsDigits[i] != bitsAsDigits[i]) return true; + } + return false; +} + +bool BinaryOctet::operator<(const BinaryOctet &a) { + int power = 8; + int aI = 0; + for(int i = 0; i <= bitsPerOctet - 1; i++) { + power--; + aI = aI + (a.bitsAsDigits[i] - '0') * pow(2, power); + } + + power = 8; + int bI = 0; + for(int i = 0; i <= bitsPerOctet - 1; i++) { + power--; + bI = bI + (bitsAsDigits[i] - '0') * pow(2, power); + } + return bI < aI; +} + bool BinaryOctet::operator>=(BinaryOctet &a) { int power = 8; @@ -122,41 +147,12 @@ bool BinaryOctet::operator>=(BinaryOctet &a) { BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) { BinaryOctet result; for (; a != b; b--) { - a = a + 1; + a = a + BinaryOctet(1); a = a / b; } result = a + b; return result; } -// for println(); -std::string as_string(BinaryOctet a) { - std::string result = "("; - for (char c : a.bitsAsDigits) { - result += c; - } - result += ")"; - return result; -} -std::string as_string(BinaryOctet *a) { - std::string result = "("; - for (char c : a->bitsAsDigits) { - result += c; - } - result += ")"; - return result; -} - -// for std::cout -std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) { - os << as_string(toBePrinted); - return os; -} - - -std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) { - os << as_string(toBePrinted); - return os; -} \ No newline at end of file diff --git a/src/07_STD/MP/BinaryOctet.h b/src/07_STD/MP/BinaryOctet.h index 849952d..43d5dcf 100644 --- a/src/07_STD/MP/BinaryOctet.h +++ b/src/07_STD/MP/BinaryOctet.h @@ -7,11 +7,13 @@ struct BinaryOctet { bool evenParity; char bitsAsDigits[bitsPerOctet]{}; - BinaryOctet(int x = 0); + explicit BinaryOctet(int x = 0); BinaryOctet(const BinaryOctet&) = default; const BinaryOctet operator--(int); const BinaryOctet operator++(int); bool operator!=(BinaryOctet &a); + bool operator==(const BinaryOctet &a); + bool operator <(const BinaryOctet &a); bool operator>=(BinaryOctet &a); BinaryOctet operator-(BinaryOctet a); BinaryOctet operator+(BinaryOctet a); diff --git a/src/07_STD/MP/grundgeruest.cpp b/src/07_STD/MP/grundgeruest.cpp index 876094f..8ca4c1b 100644 --- a/src/07_STD/MP/grundgeruest.cpp +++ b/src/07_STD/MP/grundgeruest.cpp @@ -10,6 +10,39 @@ #include #include #include "grundgeruest.hpp" +#include "BinaryOctet.h" + +// for println(); +std::string as_string(BinaryOctet a) { + std::string result = "("; + for (char c : a.bitsAsDigits) { + result += c; + } + result += ")"; + return result; +} + +std::string as_string(BinaryOctet *a) { + std::string result = "("; + for (char c : a->bitsAsDigits) { + result += c; + } + result += ")"; + return result; +} + +// for std::cout +std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) { + os << as_string(toBePrinted); + return os; +} + + +std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) { + os << as_string(toBePrinted); + return os; +} + int main() { std::srand(static_cast(std::time(nullptr))); @@ -25,20 +58,34 @@ int main() { unittest_stringSimilarity(); - std::vector vector; + std::vector intVector; for (int i = 0; i <= 41; i++) { int rnd = distribution(generator); - auto found = std::find(vector.begin(), vector.end(), rnd); - if(found == vector.end()) { - vector.push_back(rnd); + auto found = std::find(intVector.begin(), intVector.end(), rnd); + if(found == intVector.end()) { + intVector.push_back(rnd); } - std::sort(vector.begin(), vector.end()); + std::sort(intVector.begin(), intVector.end()); } - for (int ai : vector) { + for (int ai : intVector) { std::cout << ai << ","; } std::cout << std::endl; + std::vector binVector; + for (int j = 0; j < 41; ++j) { + int rnd = distribution(generator); + auto binaryOctet = BinaryOctet(rnd); + auto found = std::find(binVector.begin(), binVector.end(), binaryOctet); + if(found == binVector.end()) { + binVector.push_back(binaryOctet); + } + std::sort(binVector.begin(), binVector.end()); + } + for (BinaryOctet bin : binVector) { + std::cout << bin << ","; + } + }