From 28a90c24b502abc20bec50afc3bcca93105c5104 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 29 Aug 2018 15:44:59 +0200 Subject: [PATCH] =?UTF-8?q?04=5FUDEF=5FMP:=20kleinre=20=C3=84nderungen=20v?= =?UTF-8?q?on=20denen=20ich=20nicht=20wei=C3=9F=20ob=20die=20so=20der=20Au?= =?UTF-8?q?fgabenstellung=20nach=20richtig=20sind,=20da=20Link=20Aufgaben?= =?UTF-8?q?=20nicht=20gut=20stellen=20kann?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 04_UDEF/MP/main_04_UDEF_e.cpp | 47 +++++++++++++++++++++++++++-------- CMakeLists.txt | 3 +-- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/04_UDEF/MP/main_04_UDEF_e.cpp b/04_UDEF/MP/main_04_UDEF_e.cpp index f1b92c9..7820b24 100644 --- a/04_UDEF/MP/main_04_UDEF_e.cpp +++ b/04_UDEF/MP/main_04_UDEF_e.cpp @@ -5,15 +5,18 @@ const int bitsPerOctet = 8; 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 + char bitsAsDigits[bitsPerOctet]{}; // bit values as chars BinaryOctet(int x = 0); - + BinaryOctet& operator=(int const &a); BinaryOctet(const BinaryOctet &) = default; +private: + BinaryOctet init(int x); }; -BinaryOctet::BinaryOctet(int x) { - int length = std::to_string(x).length() * 4; +BinaryOctet BinaryOctet::init(int x) { + int length = static_cast(std::to_string(x).length() * 4); while (x >= 1) { bitsAsDigits[length] = (x % 2 == 0) ? '1' : '0'; x = x / 2; @@ -25,24 +28,47 @@ BinaryOctet::BinaryOctet(int x) { if (c == '1') i++; } evenParity = i % 2 == 0; + + return *this; +} + +BinaryOctet::BinaryOctet(int x) { + init(x); } bool operator!=(BinaryOctet a, BinaryOctet b) { - + if(a.evenParity != b.evenParity) return false; + for (int i = 0; i < bitsPerOctet; ++i) { + if(a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false; + } + return true; } BinaryOctet operator--(BinaryOctet &id, int) { + return id; } 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'; + else result.bitsAsDigits[i] = '1'; + } + + return result; } BinaryOctet operator/(BinaryOctet a, BinaryOctet b) { } +BinaryOctet& BinaryOctet::operator=(int const &i) { + init(i); + return *this; +} + BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) { BinaryOctet result; @@ -65,16 +91,17 @@ std::string as_string(BinaryOctet a) { } // for std::cout -/*std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){ - os << "(" << ")"; +std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){ + os << as_string(toBePrinted); return os; -}*/ +} int main(int argc, char **argv) { BinaryOctet a = 0b00001111; BinaryOctet b = 0b00000110; - println(new BinaryOctet(5)); - println(new BinaryOctet(5)); + 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; return 0; diff --git a/CMakeLists.txt b/CMakeLists.txt index 08239cd..fd3be0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,7 @@ add_executable(02_MENT_Testat 02_MENT/Testat/main_02_MENT.cpp) add_executable(03_FLOW_MP 03_FLOW_a/MP/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp) add_executable(03_FLOW_Testat 03_FLOW_a/Testat/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp) -add_executable(04_UDEF_MP_1 04_UDEF/MP/main_04_UDEF_e.cpp) -add_executable(04_UDEF_MP_2 04_UDEF/MP/main_04_UDEF_a.cpp) +add_executable(04_UDEF_MP 04_UDEF/MP/main_04_UDEF_e.cpp) add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp) add_executable(05_OO_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp)