From 7d8a786e89be279009800c8bd562d6684b96d5db Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 27 Nov 2018 11:05:26 +0100 Subject: [PATCH] 07_STD: Testat bestanden --- src/07_STD/CMakeLists.txt | 2 +- src/07_STD/Testat/BinaryOctet.cpp | 147 +++++++++++++++++++++++++++++ src/07_STD/Testat/BinaryOctet.h | 24 +++++ src/07_STD/Testat/grundgeruest.cpp | 67 ++++++++++--- 4 files changed, 228 insertions(+), 12 deletions(-) create mode 100644 src/07_STD/Testat/BinaryOctet.cpp create mode 100644 src/07_STD/Testat/BinaryOctet.h diff --git a/src/07_STD/CMakeLists.txt b/src/07_STD/CMakeLists.txt index bdde11f..3dc8a35 100644 --- a/src/07_STD/CMakeLists.txt +++ b/src/07_STD/CMakeLists.txt @@ -1,2 +1,2 @@ add_executable(07_STD_MP MP/grundgeruest.cpp MP/BinaryOctet.cpp MP/BinaryOctet.h) -add_executable(07_STD_Testat Testat/grundgeruest.cpp) \ No newline at end of file +add_executable(07_STD_Testat Testat/grundgeruest.cpp Testat/BinaryOctet.h Testat/BinaryOctet.cpp) \ No newline at end of file diff --git a/src/07_STD/Testat/BinaryOctet.cpp b/src/07_STD/Testat/BinaryOctet.cpp new file mode 100644 index 0000000..6701ab5 --- /dev/null +++ b/src/07_STD/Testat/BinaryOctet.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include "BinaryOctet.h" + + +BinaryOctet::BinaryOctet(int x) { + for (char &bitsAsDigit : bitsAsDigits) { + bitsAsDigit = '0'; + } + + int remainder; + int temp = 0; + while (x != 0) { + remainder = x % 2; + x /= 2; + bitsAsDigits[temp] = static_cast(remainder + '0'); + temp++; + } + + std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits)); + + int ones = 0; + for (char c : bitsAsDigits) { + if (c == '1') ones++; + } + + evenParity = ones % 2 == 0; +} + +const BinaryOctet BinaryOctet::operator--(int) { + return operator-(*new BinaryOctet(1)); +} + +const BinaryOctet BinaryOctet::operator++(int) { + return operator+(*new BinaryOctet(1)); +} + +BinaryOctet BinaryOctet::operator+(BinaryOctet a) { + BinaryOctet result; + bool over = false; + for (int i = bitsPerOctet - 1; i >= 0; --i) { + //Arbeitsvariablen anlegen und char in int umwandeln für einfachere Rechnung + int tmp; + int aI = a.bitsAsDigits[i] - '0'; + int bI = bitsAsDigits[i] - '0'; + + + if(over) tmp = aI + bI + 1; + else tmp = aI + bI; + over = false; + + + if(tmp >= 2) { + over = true; + if (tmp % 2 == 0) tmp = 0; + else tmp = 1; + } + + //Umwandlung in char und ins Ergebnis schreiben + result.bitsAsDigits[i] = static_cast(tmp + '0'); + } + return result; +} + +BinaryOctet BinaryOctet::operator-(BinaryOctet a) { + BinaryOctet result; + bool over = false; + for(int i = bitsPerOctet - 1; i >= 0; --i) { + int tmp; + int aI = a.bitsAsDigits[i] - '0'; + int bI = bitsAsDigits[i] - '0'; + + if(over) tmp = bI - aI - 1; + else tmp = bI - aI; + over = false; + + if(tmp < 0) { + over = true; + tmp = tmp + 1; + } + result.bitsAsDigits[i] = static_cast(tmp + '0'); + } + return result; +} + +BinaryOctet BinaryOctet::operator/(BinaryOctet &a) { + BinaryOctet result = *new BinaryOctet(0); + + while(*this >= a) { + *this = *this - a; + result = result + BinaryOctet(1); + } + return result; +} + +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; + } + 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>=(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; + 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; +} diff --git a/src/07_STD/Testat/BinaryOctet.h b/src/07_STD/Testat/BinaryOctet.h new file mode 100644 index 0000000..8b423e6 --- /dev/null +++ b/src/07_STD/Testat/BinaryOctet.h @@ -0,0 +1,24 @@ +#ifndef C_C_BINARYOCTET_H +#define C_C_BINARYOCTET_H + +const int bitsPerOctet = 8; + +struct BinaryOctet { + bool evenParity; + char bitsAsDigits[bitsPerOctet]{}; + + + BinaryOctet(int x = 0); + BinaryOctet(const BinaryOctet&) = default; + const BinaryOctet operator--(int); + const BinaryOctet operator++(int); + bool operator==(const BinaryOctet &a); + bool operator!=(BinaryOctet &a); + bool operator>=(BinaryOctet &a); + bool operator<(BinaryOctet &a); + BinaryOctet operator-(BinaryOctet a); + BinaryOctet operator+(BinaryOctet a); + BinaryOctet operator/(BinaryOctet &a); +}; + +#endif diff --git a/src/07_STD/Testat/grundgeruest.cpp b/src/07_STD/Testat/grundgeruest.cpp index 64df26a..2fe11ca 100644 --- a/src/07_STD/Testat/grundgeruest.cpp +++ b/src/07_STD/Testat/grundgeruest.cpp @@ -2,16 +2,61 @@ #include #include #include +#include +#include +#include +#include "BinaryOctet.h" -struct BinaryOctet { - bool evenParity; - char bitsAsDigits[8]; -}; - -int main() -{ - std::srand(std::time(0)); // use current time as seed for random generator - int random_variable = std::rand(); - std::cout << "Random value on [0 " << RAND_MAX << "]: " - << random_variable << '\n'; +// 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, 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))); + + std::default_random_engine generator; + std::uniform_int_distribution distribution(0, 23); + + std::vector binVector; + for (int i = 0; i <= 41; i++) { + int rnd = distribution(generator); + auto bin = rnd; + std::cout << i << ": " << bin << std::endl; + auto iterator = std::find(binVector.begin(), binVector.end(), bin); + if(iterator == binVector.end()) { + binVector.emplace_back(bin); + } + } + std::sort(binVector.begin(), binVector.end()); + for(BinaryOctet binaryOctet : binVector) { + std::cout << binaryOctet << std::endl; + } }