From 3b1f7b9af355240528ed530eb7ae20769f740794 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 5 Sep 2018 01:03:06 +0200 Subject: [PATCH] 04_UDEF_MP BinaryOctet Constructor fehlerfrei --- 04_UDEF/MP/main_04_UDEF_e.cpp | 51 +++++++++++++++++++---------------- 07_STD/MP/grundgeruest.cpp | 3 ++- 11_PUTT/Test.cpp | 2 +- 11_PUTT/Test.h | 2 +- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/04_UDEF/MP/main_04_UDEF_e.cpp b/04_UDEF/MP/main_04_UDEF_e.cpp index ad43ea3..daef257 100644 --- a/04_UDEF/MP/main_04_UDEF_e.cpp +++ b/04_UDEF/MP/main_04_UDEF_e.cpp @@ -1,6 +1,6 @@ // file main_04_UDEF_e.cpp -#include "../../helpers/println.hpp" #include +#include const int bitsPerOctet = 8; @@ -8,35 +8,42 @@ struct BinaryOctet { public: bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise char bitsAsDigits[bitsPerOctet]{}; // bit values as chars - BinaryOctet(int x = 0); + BinaryOctet(); + BinaryOctet(int x); BinaryOctet(const BinaryOctet &) = default; - BinaryOctet& operator=(int const &a); + BinaryOctet &operator=(int const &a); const BinaryOctet operator--(int); }; -BinaryOctet::BinaryOctet(int x) { - //Alles mit 0 füllen +BinaryOctet::BinaryOctet() { for (char &bitsAsDigit : bitsAsDigits) { bitsAsDigit = '0'; } - int length = static_cast(std::to_string(x).length() * 4); - while (length >= 0) { - bitsAsDigits[length-1] = (x % 2 == 0) ? '0' : '1'; - std::cout << x << " : " << length << " : " << bitsAsDigits[length-1] << std::endl; - x = x / 2; - length--; + evenParity = true; +} + +BinaryOctet::BinaryOctet(int x) { + for (int i = 0; i <= bitsPerOctet - 1; i++) { + int tmp = ((x >> i) & 1); + bitsAsDigits[i] = (tmp == 1) ? '1' : '0'; } + //TODO: das sollte eigentlich nicht benötigt werden + std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits)); + + //einsen zählen int i = 0; for (char c : bitsAsDigits) { if (c == '1') i++; } + evenParity = i % 2 == 0; } + bool operator!=(BinaryOctet a, BinaryOctet b) { - if(a.evenParity != b.evenParity) return false; + if (a.evenParity != b.evenParity) return false; for (int i = 0; i < bitsPerOctet; ++i) { - if(a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false; + if (a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false; } return true; } @@ -51,7 +58,7 @@ BinaryOctet operator+(BinaryOctet a, BinaryOctet b) { BinaryOctet result; for (int i = 0; i < bitsPerOctet; ++i) { - if(a.bitsAsDigits[i] == b.bitsAsDigits[i]) result.bitsAsDigits[i] = '0'; + if (a.bitsAsDigits[i] == b.bitsAsDigits[i]) result.bitsAsDigits[i] = '0'; else result.bitsAsDigits[i] = '1'; } @@ -67,10 +74,9 @@ BinaryOctet operator/(BinaryOctet a, BinaryOctet b) { return a; } -BinaryOctet& BinaryOctet::operator=(int const &a) { - std::cout << a << std::endl; - std::string tmp = to_string(a); - for(int i = 0; i < bitsPerOctet; i++) { +BinaryOctet &BinaryOctet::operator=(int const &a) { + std::string tmp = std::to_string(a); + for (int i = 0; i >= tmp.length(); i++) { bitsAsDigits[i] = tmp[i]; } return *this; @@ -98,9 +104,9 @@ std::string as_string(BinaryOctet a) { } // for std::cout -std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){ - os << as_string(toBePrinted); - return os; +std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) { + os << as_string(toBePrinted); + return os; } int main(int argc, char **argv) { @@ -109,9 +115,8 @@ int main(int argc, char **argv) { BinaryOctet b = 0b00000110; std::cout << b << std::endl; std::cout << new BinaryOctet(5) << std::endl; - println(a + b); std::cout << a + b << std::endl; //println("result = ", doCalculation(a,b)); - std::cout << "result = " << doCalculation(a,b) << std::endl; + std::cout << "result = " << doCalculation(a, b) << std::endl; return 0; } diff --git a/07_STD/MP/grundgeruest.cpp b/07_STD/MP/grundgeruest.cpp index f242ce5..2b853fc 100644 --- a/07_STD/MP/grundgeruest.cpp +++ b/07_STD/MP/grundgeruest.cpp @@ -24,7 +24,8 @@ int main() { std::uniform_int_distribution distribution(0, 23); for(int i = 0; i <= 41; i++) { - array[i] = distribution(generator); + int rnd = distribution(generator); + array[i] = rnd; } unittest_stringSimilarity(); diff --git a/11_PUTT/Test.cpp b/11_PUTT/Test.cpp index b25351d..cde3791 100644 --- a/11_PUTT/Test.cpp +++ b/11_PUTT/Test.cpp @@ -7,7 +7,7 @@ int main() { Derived * derived = new Derived(); - bar(derived); + foo(derived); delete derived; diff --git a/11_PUTT/Test.h b/11_PUTT/Test.h index 5020256..4579a96 100644 --- a/11_PUTT/Test.h +++ b/11_PUTT/Test.h @@ -10,7 +10,7 @@ class Base { }; -class Derived : Base { +class Derived : public Base { }; void foo(Base* base) {