04_UDEF_MP: operator>= funktioniert

This commit is contained in:
Johannes Theiner 2018-10-20 13:05:26 +02:00
parent 7faef81469
commit 93991e767b
3 changed files with 114 additions and 77 deletions

View File

@ -5,29 +5,29 @@
#include "../../helpers/println.hpp" #include "../../helpers/println.hpp"
struct PascalString{ struct PascalString {
int length; // number of chars used int length; // number of chars used
char characters[256]; // chars of some character string char characters[256]; // chars of some character string
}; };
char cStringArea[1024]; char cStringArea[1024];
int hexDigitToInt(char hexDigit) { int hexDigitToInt(char hexDigit) {
int value = -1; int value = -1;
if(hexDigit > 47 && hexDigit < 58) if (hexDigit > 47 && hexDigit < 58)
value = hexDigit - 48; value = hexDigit - 48;
if(hexDigit > 96 && hexDigit < 103) if (hexDigit > 96 && hexDigit < 103)
value = hexDigit - 97 + 10; value = hexDigit - 97 + 10;
return value; return value;
} }
int hexStringToInt(PascalString binaryDigits) { int hexStringToInt(PascalString binaryDigits) {
int returnValue = 0; int returnValue = 0;
for(int i = 0; i <= binaryDigits.length; ++i) { for (int i = 0; i <= binaryDigits.length; ++i) {
returnValue += binaryDigits.characters[i]; returnValue += binaryDigits.characters[i];
} }
@ -35,7 +35,7 @@ int hexStringToInt(PascalString binaryDigits) {
} }
void printPascalString(PascalString s) { void printPascalString(PascalString s) {
for(int i = 0; i <= s.length; i++) { for (int i = 0; i <= s.length; i++) {
print(s.characters[i]); print(s.characters[i]);
} }
println(""); println("");
@ -44,7 +44,7 @@ void printPascalString(PascalString s) {
PascalString intToDual(int n) { PascalString intToDual(int n) {
int i = std::to_string(n).length() * 4; int i = std::to_string(n).length() * 4;
PascalString string = {i}; PascalString string = {i};
while(n >= 1) { while (n >= 1) {
string.characters[i] = (n % 2) + '0'; string.characters[i] = (n % 2) + '0';
n = n / 2; n = n / 2;
i--; i--;
@ -52,12 +52,26 @@ PascalString intToDual(int n) {
return string; 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<char>((n / 8) + '0');
n = n / 8;
i--;
}
std::cout << std::endl;
return string;
}
char intToHexChar(int n) { char intToHexChar(int n) {
int returnValue = 0; char returnValue = 0;
if(n >= 0 && n <= 9) if (n >= 0 && n <= 9)
returnValue = n + '0'; returnValue = static_cast<char>(n + '0');
if(n >= 10 && n <= 15) if (n >= 10 && n <= 15)
returnValue = n + 97 - 10; returnValue = static_cast<char>(n + 97 - 10);
return returnValue; return returnValue;
} }
@ -65,7 +79,7 @@ char intToHexChar(int n) {
PascalString intToHex(int n) { PascalString intToHex(int n) {
int i = std::to_string(n).length(); int i = std::to_string(n).length();
PascalString string = {i}; PascalString string = {i};
while(n >= 1) { while (n >= 1) {
string.characters[i] = intToHexChar(n % 16); string.characters[i] = intToHexChar(n % 16);
n = n / 16; n = n / 16;
i--; i--;
@ -75,14 +89,14 @@ PascalString intToHex(int n) {
} }
int getInt(char character) { int getInt(char character) {
if(character == '0') return 0; if (character == '0') return 0;
else return 1; else return 1;
} }
PascalString bitwiseDualAnd(PascalString a, PascalString b) { PascalString bitwiseDualAnd(PascalString a, PascalString b) {
PascalString result = {b.length}; 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]; result.characters[i] = a.characters[i] & b.characters[i];
} }
return result; return result;
@ -91,14 +105,13 @@ PascalString bitwiseDualAnd(PascalString a, PascalString b) {
PascalString bitwiseOctalAnd(PascalString a, PascalString b) { PascalString bitwiseOctalAnd(PascalString a, PascalString b) {
PascalString result = {b.length, '0'}; 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]; result.characters[i] = a.characters[i] & b.characters[i];
} }
return result; return result;
} }
PascalString bitwiseHexAnd(PascalString a, PascalString b) { PascalString bitwiseHexAnd(PascalString a, PascalString b) {
a = intToDual(hexStringToInt(a)); a = intToDual(hexStringToInt(a));
b = intToDual(hexStringToInt(b)); b = intToDual(hexStringToInt(b));
@ -112,59 +125,63 @@ int to_c_string(PascalString s) {
} }
void line() { void line() {
println("==================================="); println("===================================");
} }
int main(int argc, char** argv, char** envp) { int main(int argc, char **argv, char **envp) {
PascalString s = {3, '1', '0', '0'}; PascalString s = {3, '1', '0', '0'};
PascalString s2 = {4, 'f', 'f', 'f', 'f'}; PascalString s2 = {4, 'f', 'f', 'f', 'f'};
println(hexStringToInt(s)); println(hexStringToInt(s));
line(); line();
println(hexStringToInt(s2)); println(hexStringToInt(s2));
line(); line();
println(hexDigitToInt('d')); println(hexDigitToInt('d'));
line(); line();
println(hexDigitToInt('9')); println(hexDigitToInt('9'));
line(); line();
println(hexDigitToInt('2')); println(hexDigitToInt('2'));
printPascalString(s2); printPascalString(s2);
line(); line();
int controlRegister = 128; int controlRegister = 128;
controlRegister |= 64+32; controlRegister |= 64 + 32;
controlRegister ^=16; controlRegister ^= 16;
controlRegister &= 128+64; controlRegister &= 128 + 64;
controlRegister <<= 1; controlRegister <<= 1;
println(controlRegister); println(controlRegister);
line(); line();
std::cout << "intToHexChar = " << intToHexChar(6) << std::endl; std::cout << "intToHexChar = " << intToHexChar(6) << std::endl;
line(); line();
println(intToHexChar(10)); println(intToHexChar(10));
line(); line();
printPascalString(intToDual(4106)); printPascalString(intToDual(4106));
line(); line();
printPascalString(intToHex(965)); printPascalString(intToHex(965));
line(); line();
printPascalString(intToHex(1956)); printPascalString(intToHex(1956));
line(); line();
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {3, '1', '1', '1'})); std::cout << "intToOctal = ";
line(); printPascalString(intToOctal(32));
printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {3, '1', '1', '1'}));
line(); line();
printPascalString(bitwiseDualAnd({3, '0', '1', '0'}, {3, '1', '0', '1'})); printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {3, '1', '1', '1'}));
line(); line();
printPascalString(bitwiseDualAnd({3, '1', '0', '0'}, {3, '1', '1', '1'})); printPascalString(bitwiseDualAnd({3, '1', '1', '1'}, {3, '1', '1', '1'}));
line(); line();
printPascalString(bitwiseOctalAnd({2, '6', '3'}, {2, '1', '2'})); 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(); line();
printPascalString(bitwiseHexAnd({2, 'A', '1'}, {2, 'C', '2'})); printPascalString(bitwiseHexAnd({2, 'A', '1'}, {2, 'C', '2'}));
return 0; return 0;
} }

View File

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <math.h>
const int bitsPerOctet = 8; const int bitsPerOctet = 8;
@ -104,7 +105,6 @@ BinaryOctet BinaryOctet::operator/(BinaryOctet &a) {
while(*this >= a) { while(*this >= a) {
*this = *this - a; *this = *this - a;
result = result + 1; result = result + 1;
std::cout << result.bitsAsDigits << std::endl;
} }
return result; return result;
} }
@ -120,16 +120,19 @@ bool BinaryOctet::operator!=(BinaryOctet &a) {
//TODO: this //TODO: this
bool BinaryOctet::operator>=(BinaryOctet &a) { bool BinaryOctet::operator>=(BinaryOctet &a) {
if(!operator!=(a)) return true;
int power = 8;
int aI = 0; int aI = 0;
for(int i = bitsPerOctet - 1; i >= 0; --i) { for(int i = 0; i <= bitsPerOctet - 1; i++) {
aI =+ (a.bitsAsDigits[i] - '0') * 2 ^ i; power--;
aI = aI + (a.bitsAsDigits[i] - '0') * pow(2, power);
} }
power = 8;
int bI = 0; int bI = 0;
for(int i = bitsPerOctet - 1; i >= 0; --i) { for(int i = 0; i <= bitsPerOctet - 1; i++) {
bI += (bitsAsDigits[i] - '0') * 2 ^ i; power--;
bI = bI + (bitsAsDigits[i] - '0') * pow(2, power);
} }
return bI >= aI; return bI >= aI;
} }
@ -144,6 +147,16 @@ BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
return result; 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(); // for println();
std::string as_string(BinaryOctet a) { std::string as_string(BinaryOctet a) {
@ -194,6 +207,10 @@ int main(int argc, char **argv) {
std::cout << "========= operator- ==========" << std::endl; std::cout << "========= operator- ==========" << std::endl;
std::cout << "a - b = " << a - b << 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 << "========= operator/ ==========" << std::endl;
std::cout << "a / b = " << a / b << 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 << "============== doCalculation ================" << std::endl;
std::cout << "result = " << doCalculation(a, b) << std::endl; std::cout << "result = " << doCalculation(a, b) << std::endl;
//std::cout << "result = " << doCalculation(0b00001111, 0b00000110) << std::endl;
return 0; return 0;
} }

View File

@ -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(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) 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) 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_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp)
add_executable(05_OO_Testat 05_OO/Testat/shapes_main.cpp helpers/AnsiConsole.cpp) add_executable(05_OO_Testat 05_OO/Testat/shapes_main.cpp helpers/AnsiConsole.cpp)