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,7 +5,7 @@
#include "../../helpers/println.hpp"
struct PascalString{
struct PascalString {
int length; // number of chars used
char characters[256]; // chars of some character string
};
@ -13,21 +13,21 @@ struct PascalString{
char cStringArea[1024];
int hexDigitToInt(char hexDigit) {
int value = -1;
int value = -1;
if(hexDigit > 47 && hexDigit < 58)
if (hexDigit > 47 && hexDigit < 58)
value = hexDigit - 48;
if(hexDigit > 96 && hexDigit < 103)
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,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;
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<char>(n + '0');
if (n >= 10 && n <= 15)
returnValue = static_cast<char>(n + 97 - 10);
return returnValue;
}
@ -65,7 +79,7 @@ char intToHexChar(int n) {
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,14 +105,13 @@ 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));
@ -112,10 +125,10 @@ int to_c_string(PascalString s) {
}
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 s2 = {4, 'f', 'f', 'f', 'f'};
@ -133,9 +146,9 @@ int main(int argc, char** argv, char** envp) {
line();
int controlRegister = 128;
controlRegister |= 64+32;
controlRegister ^=16;
controlRegister &= 128+64;
controlRegister |= 64 + 32;
controlRegister ^= 16;
controlRegister &= 128 + 64;
controlRegister <<= 1;
println(controlRegister);
@ -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)