From 93991e767b1dc6a8c0baa9a6e1ffb1c141c85bb8 Mon Sep 17 00:00:00 2001 From: joethei Date: Sat, 20 Oct 2018 13:05:26 +0200 Subject: [PATCH] 04_UDEF_MP: operator>= funktioniert --- src/02_MENT/MP/main_02_MENT.cpp | 159 +++++++++++++++++------------- src/04_UDEF/MP/main_04_UDEF_e.cpp | 30 ++++-- src/CMakeLists.txt | 2 + 3 files changed, 114 insertions(+), 77 deletions(-) diff --git a/src/02_MENT/MP/main_02_MENT.cpp b/src/02_MENT/MP/main_02_MENT.cpp index ac8a96c..0823fed 100644 --- a/src/02_MENT/MP/main_02_MENT.cpp +++ b/src/02_MENT/MP/main_02_MENT.cpp @@ -5,29 +5,29 @@ #include "../../helpers/println.hpp" -struct PascalString{ - int length; // number of chars used - char characters[256]; // chars of some character string +struct PascalString { + int length; // number of chars used + char characters[256]; // chars of some character string }; char cStringArea[1024]; int hexDigitToInt(char hexDigit) { -int value = -1; + int value = -1; -if(hexDigit > 47 && hexDigit < 58) - value = hexDigit - 48; + if (hexDigit > 47 && hexDigit < 58) + value = hexDigit - 48; -if(hexDigit > 96 && hexDigit < 103) - value = hexDigit - 97 + 10; + if (hexDigit > 96 && hexDigit < 103) + value = hexDigit - 97 + 10; -return value; + return value; } int hexStringToInt(PascalString binaryDigits) { int returnValue = 0; - for(int i = 0; i <= binaryDigits.length; ++i) { + for (int i = 0; i <= binaryDigits.length; ++i) { returnValue += binaryDigits.characters[i]; } @@ -35,7 +35,7 @@ int hexStringToInt(PascalString binaryDigits) { } void printPascalString(PascalString s) { - for(int i = 0; i <= s.length; i++) { + for (int i = 0; i <= s.length; i++) { print(s.characters[i]); } println(""); @@ -44,7 +44,7 @@ void printPascalString(PascalString s) { PascalString intToDual(int n) { int i = std::to_string(n).length() * 4; PascalString string = {i}; - while(n >= 1) { + while (n >= 1) { string.characters[i] = (n % 2) + '0'; n = n / 2; i--; @@ -52,20 +52,34 @@ PascalString intToDual(int n) { return string; } +PascalString intToOctal(int n) { + int i = std::to_string(n).length(); + std::cout << i << " : "; + PascalString string = {i}; + while (n >= 1) { + std::cout << n; + string.characters[i] = static_cast((n / 8) + '0'); + n = n / 8; + i--; + } + std::cout << std::endl; + return string; +} + char intToHexChar(int n) { - int returnValue = 0; - if(n >= 0 && n <= 9) - returnValue = n + '0'; - if(n >= 10 && n <= 15) - returnValue = n + 97 - 10; - + char returnValue = 0; + if (n >= 0 && n <= 9) + returnValue = static_cast(n + '0'); + if (n >= 10 && n <= 15) + returnValue = static_cast(n + 97 - 10); + return returnValue; } PascalString intToHex(int n) { int i = std::to_string(n).length(); PascalString string = {i}; - while(n >= 1) { + while (n >= 1) { string.characters[i] = intToHexChar(n % 16); n = n / 16; i--; @@ -75,14 +89,14 @@ PascalString intToHex(int n) { } int getInt(char character) { - if(character == '0') return 0; + if (character == '0') return 0; else return 1; } PascalString bitwiseDualAnd(PascalString a, PascalString b) { PascalString result = {b.length}; - for(int i = 0; i <= b.length; ++i) { + for (int i = 0; i <= b.length; ++i) { result.characters[i] = a.characters[i] & b.characters[i]; } return result; @@ -91,19 +105,18 @@ PascalString bitwiseDualAnd(PascalString a, PascalString b) { PascalString bitwiseOctalAnd(PascalString a, PascalString b) { PascalString result = {b.length, '0'}; - for(int i = 0; i <= b.length; ++i) { + for (int i = 0; i <= b.length; ++i) { result.characters[i] = a.characters[i] & b.characters[i]; } return result; } - PascalString bitwiseHexAnd(PascalString a, PascalString b) { a = intToDual(hexStringToInt(a)); b = intToDual(hexStringToInt(b)); PascalString result = bitwiseDualAnd(a, b); - + return result; } @@ -112,59 +125,63 @@ int to_c_string(PascalString s) { } void line() { -println("==================================="); + println("==================================="); } -int main(int argc, char** argv, char** envp) { - - PascalString s = {3, '1', '0', '0'}; - PascalString s2 = {4, 'f', 'f', 'f', 'f'}; - println(hexStringToInt(s)); - line(); - println(hexStringToInt(s2)); - line(); - println(hexDigitToInt('d')); - line(); - println(hexDigitToInt('9')); - line(); - println(hexDigitToInt('2')); +int main(int argc, char **argv, char **envp) { - printPascalString(s2); - line(); + PascalString s = {3, '1', '0', '0'}; + PascalString s2 = {4, 'f', 'f', 'f', 'f'}; + println(hexStringToInt(s)); + line(); + println(hexStringToInt(s2)); + line(); + println(hexDigitToInt('d')); + line(); + println(hexDigitToInt('9')); + line(); + println(hexDigitToInt('2')); - int controlRegister = 128; - controlRegister |= 64+32; - controlRegister ^=16; - controlRegister &= 128+64; - controlRegister <<= 1; + printPascalString(s2); + line(); - println(controlRegister); - line(); - std::cout << "intToHexChar = " << intToHexChar(6) << std::endl; - line(); - println(intToHexChar(10)); - line(); - printPascalString(intToDual(4106)); - line(); - printPascalString(intToHex(965)); - line(); - printPascalString(intToHex(1956)); + int controlRegister = 128; + controlRegister |= 64 + 32; + controlRegister ^= 16; + controlRegister &= 128 + 64; + controlRegister <<= 1; - line(); - printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {3, '1', '1', '1'})); - line(); - printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {3, '1', '1', '1'})); - line(); - printPascalString(bitwiseDualAnd({3, '0', '1', '0'}, {3, '1', '0', '1'})); - line(); - printPascalString(bitwiseDualAnd({3, '1', '0', '0'}, {3, '1', '1', '1'})); - line(); - printPascalString(bitwiseOctalAnd({2, '6', '3'}, {2, '1', '2'})); - - - line(); - printPascalString(bitwiseHexAnd({2, 'A', '1'}, {2, 'C', '2'})); + println(controlRegister); + line(); + std::cout << "intToHexChar = " << intToHexChar(6) << std::endl; + line(); + println(intToHexChar(10)); + line(); + printPascalString(intToDual(4106)); + line(); + printPascalString(intToHex(965)); + line(); + printPascalString(intToHex(1956)); - return 0; + line(); + std::cout << "intToOctal = "; + printPascalString(intToOctal(32)); + + line(); + printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {3, '1', '1', '1'})); + line(); + printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {3, '1', '1', '1'})); + line(); + printPascalString(bitwiseDualAnd({3, '0', '1', '0'}, {3, '1', '0', '1'})); + line(); + printPascalString(bitwiseDualAnd({3, '1', '0', '0'}, {3, '1', '1', '1'})); + line(); + printPascalString(bitwiseOctalAnd({2, '6', '3'}, {2, '1', '2'})); + + + line(); + printPascalString(bitwiseHexAnd({2, 'A', '1'}, {2, 'C', '2'})); + + return 0; } diff --git a/src/04_UDEF/MP/main_04_UDEF_e.cpp b/src/04_UDEF/MP/main_04_UDEF_e.cpp index ff430f5..3dc4a47 100644 --- a/src/04_UDEF/MP/main_04_UDEF_e.cpp +++ b/src/04_UDEF/MP/main_04_UDEF_e.cpp @@ -1,5 +1,6 @@ #include #include +#include const int bitsPerOctet = 8; @@ -104,7 +105,6 @@ BinaryOctet BinaryOctet::operator/(BinaryOctet &a) { while(*this >= a) { *this = *this - a; result = result + 1; - std::cout << result.bitsAsDigits << std::endl; } return result; } @@ -120,16 +120,19 @@ bool BinaryOctet::operator!=(BinaryOctet &a) { //TODO: this bool BinaryOctet::operator>=(BinaryOctet &a) { - if(!operator!=(a)) return true; + int power = 8; int aI = 0; - for(int i = bitsPerOctet - 1; i >= 0; --i) { - aI =+ (a.bitsAsDigits[i] - '0') * 2 ^ i; + 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 = bitsPerOctet - 1; i >= 0; --i) { - bI += (bitsAsDigits[i] - '0') * 2 ^ i; + for(int i = 0; i <= bitsPerOctet - 1; i++) { + power--; + bI = bI + (bitsAsDigits[i] - '0') * pow(2, power); } return bI >= aI; } @@ -144,6 +147,16 @@ BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) { return result; } +int doCalculation(int a, int b) { + int result; + for (; a != b; b--) { + a = a + 1; + a = a / b; + } + result = a + b; + return result; +} + // for println(); std::string as_string(BinaryOctet a) { @@ -194,6 +207,10 @@ int main(int argc, char **argv) { std::cout << "========= operator- ==========" << std::endl; std::cout << "a - b = " << a - b << std::endl; + std::cout << "========= operator>= ==========" << std::endl; + bool isGreater = a >= b; + std::cout << "a >= b = " << isGreater << std::endl; + std::cout << "========= operator/ ==========" << std::endl; std::cout << "a / b = " << a / b << std::endl; @@ -205,5 +222,6 @@ int main(int argc, char **argv) { std::cout << "============== doCalculation ================" << std::endl; std::cout << "result = " << doCalculation(a, b) << std::endl; + //std::cout << "result = " << doCalculation(0b00001111, 0b00000110) << std::endl; return 0; } \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8cb46e6..eaf329a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,7 +9,9 @@ add_executable(03_FLOW_MP 03_FLOW/MP/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp add_executable(03_FLOW_Testat 03_FLOW/Testat/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp) add_executable(04_UDEF_MP 04_UDEF/MP/main_04_UDEF_e.cpp) +target_link_libraries(04_UDEF_MP m) add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp) +target_link_libraries(04_UDEF_Testat m) add_executable(05_OO_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp) add_executable(05_OO_Testat 05_OO/Testat/shapes_main.cpp helpers/AnsiConsole.cpp)