04_UDEF_MP BinaryOctet Constructor fehlerfrei
This commit is contained in:
parent
08760f4afc
commit
3b1f7b9af3
|
@ -1,6 +1,6 @@
|
||||||
// file main_04_UDEF_e.cpp
|
// file main_04_UDEF_e.cpp
|
||||||
#include "../../helpers/println.hpp"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
const int bitsPerOctet = 8;
|
const int bitsPerOctet = 8;
|
||||||
|
|
||||||
|
@ -8,35 +8,42 @@ struct BinaryOctet {
|
||||||
public:
|
public:
|
||||||
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise
|
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();
|
||||||
|
BinaryOctet(int x);
|
||||||
BinaryOctet(const BinaryOctet &) = default;
|
BinaryOctet(const BinaryOctet &) = default;
|
||||||
BinaryOctet& operator=(int const &a);
|
BinaryOctet &operator=(int const &a);
|
||||||
const BinaryOctet operator--(int);
|
const BinaryOctet operator--(int);
|
||||||
};
|
};
|
||||||
|
|
||||||
BinaryOctet::BinaryOctet(int x) {
|
BinaryOctet::BinaryOctet() {
|
||||||
//Alles mit 0 füllen
|
|
||||||
for (char &bitsAsDigit : bitsAsDigits) {
|
for (char &bitsAsDigit : bitsAsDigits) {
|
||||||
bitsAsDigit = '0';
|
bitsAsDigit = '0';
|
||||||
}
|
}
|
||||||
int length = static_cast<int>(std::to_string(x).length() * 4);
|
evenParity = true;
|
||||||
while (length >= 0) {
|
}
|
||||||
bitsAsDigits[length-1] = (x % 2 == 0) ? '0' : '1';
|
|
||||||
std::cout << x << " : " << length << " : " << bitsAsDigits[length-1] << std::endl;
|
BinaryOctet::BinaryOctet(int x) {
|
||||||
x = x / 2;
|
for (int i = 0; i <= bitsPerOctet - 1; i++) {
|
||||||
length--;
|
int tmp = ((x >> i) & 1);
|
||||||
|
bitsAsDigits[i] = (tmp == 1) ? '1' : '0';
|
||||||
}
|
}
|
||||||
|
//TODO: das sollte eigentlich nicht benötigt werden
|
||||||
|
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
|
||||||
|
|
||||||
|
//einsen zählen
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (char c : bitsAsDigits) {
|
for (char c : bitsAsDigits) {
|
||||||
if (c == '1') i++;
|
if (c == '1') i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
evenParity = i % 2 == 0;
|
evenParity = i % 2 == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
bool operator!=(BinaryOctet a, BinaryOctet b) {
|
||||||
if(a.evenParity != b.evenParity) return false;
|
if (a.evenParity != b.evenParity) return false;
|
||||||
for (int i = 0; i < bitsPerOctet; ++i) {
|
for (int i = 0; i < bitsPerOctet; ++i) {
|
||||||
if(a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
|
if (a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -51,7 +58,7 @@ BinaryOctet operator+(BinaryOctet a, BinaryOctet b) {
|
||||||
BinaryOctet result;
|
BinaryOctet result;
|
||||||
|
|
||||||
for (int i = 0; i < bitsPerOctet; ++i) {
|
for (int i = 0; i < bitsPerOctet; ++i) {
|
||||||
if(a.bitsAsDigits[i] == b.bitsAsDigits[i]) result.bitsAsDigits[i] = '0';
|
if (a.bitsAsDigits[i] == b.bitsAsDigits[i]) result.bitsAsDigits[i] = '0';
|
||||||
else result.bitsAsDigits[i] = '1';
|
else result.bitsAsDigits[i] = '1';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +74,9 @@ BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
BinaryOctet& BinaryOctet::operator=(int const &a) {
|
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
||||||
std::cout << a << std::endl;
|
std::string tmp = std::to_string(a);
|
||||||
std::string tmp = to_string(a);
|
for (int i = 0; i >= tmp.length(); i++) {
|
||||||
for(int i = 0; i < bitsPerOctet; i++) {
|
|
||||||
bitsAsDigits[i] = tmp[i];
|
bitsAsDigits[i] = tmp[i];
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -98,7 +104,7 @@ std::string as_string(BinaryOctet a) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// for std::cout
|
// for std::cout
|
||||||
std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){
|
std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) {
|
||||||
os << as_string(toBePrinted);
|
os << as_string(toBePrinted);
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
@ -109,9 +115,8 @@ int main(int argc, char **argv) {
|
||||||
BinaryOctet b = 0b00000110;
|
BinaryOctet b = 0b00000110;
|
||||||
std::cout << b << std::endl;
|
std::cout << b << std::endl;
|
||||||
std::cout << new BinaryOctet(5) << std::endl;
|
std::cout << new BinaryOctet(5) << std::endl;
|
||||||
println(a + b);
|
|
||||||
std::cout << a + b << std::endl;
|
std::cout << a + b << std::endl;
|
||||||
//println("result = ", doCalculation(a,b));
|
//println("result = ", doCalculation(a,b));
|
||||||
std::cout << "result = " << doCalculation(a,b) << std::endl;
|
std::cout << "result = " << doCalculation(a, b) << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,8 @@ int main() {
|
||||||
std::uniform_int_distribution<int> distribution(0, 23);
|
std::uniform_int_distribution<int> distribution(0, 23);
|
||||||
|
|
||||||
for(int i = 0; i <= 41; i++) {
|
for(int i = 0; i <= 41; i++) {
|
||||||
array[i] = distribution(generator);
|
int rnd = distribution(generator);
|
||||||
|
array[i] = rnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
unittest_stringSimilarity();
|
unittest_stringSimilarity();
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
int main() {
|
int main() {
|
||||||
Derived * derived = new Derived();
|
Derived * derived = new Derived();
|
||||||
|
|
||||||
bar(derived);
|
foo(derived);
|
||||||
|
|
||||||
delete derived;
|
delete derived;
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ class Base {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Derived : Base {
|
class Derived : public Base {
|
||||||
};
|
};
|
||||||
|
|
||||||
void foo(Base* base) {
|
void foo(Base* base) {
|
||||||
|
|
Loading…
Reference in New Issue