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

@ -52,12 +52,26 @@ 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<char>((n / 8) + '0');
n = n / 8;
i--;
}
std::cout << std::endl;
return string;
}
char intToHexChar(int n) {
int returnValue = 0;
char returnValue = 0;
if (n >= 0 && n <= 9)
returnValue = n + '0';
returnValue = static_cast<char>(n + '0');
if (n >= 10 && n <= 15)
returnValue = n + 97 - 10;
returnValue = static_cast<char>(n + 97 - 10);
return returnValue;
}
@ -98,7 +112,6 @@ PascalString bitwiseOctalAnd(PascalString a, PascalString b) {
}
PascalString bitwiseHexAnd(PascalString a, PascalString b) {
a = intToDual(hexStringToInt(a));
b = intToDual(hexStringToInt(b));
@ -150,6 +163,10 @@ int main(int argc, char** argv, char** envp) {
line();
printPascalString(intToHex(1956));
line();
std::cout << "intToOctal = ";
printPascalString(intToOctal(32));
line();
printPascalString(bitwiseDualAnd({3, '1', '1', '0'}, {3, '1', '1', '1'}));
line();

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <algorithm>
#include <math.h>
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;
}

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(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)