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
|
||||
#include "../../helpers/println.hpp"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
const int bitsPerOctet = 8;
|
||||
|
||||
|
@ -8,35 +8,42 @@ struct BinaryOctet {
|
|||
public:
|
||||
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise
|
||||
char bitsAsDigits[bitsPerOctet]{}; // bit values as chars
|
||||
BinaryOctet(int x = 0);
|
||||
BinaryOctet();
|
||||
BinaryOctet(int x);
|
||||
BinaryOctet(const BinaryOctet &) = default;
|
||||
BinaryOctet& operator=(int const &a);
|
||||
BinaryOctet &operator=(int const &a);
|
||||
const BinaryOctet operator--(int);
|
||||
};
|
||||
|
||||
BinaryOctet::BinaryOctet(int x) {
|
||||
//Alles mit 0 füllen
|
||||
BinaryOctet::BinaryOctet() {
|
||||
for (char &bitsAsDigit : bitsAsDigits) {
|
||||
bitsAsDigit = '0';
|
||||
}
|
||||
int length = static_cast<int>(std::to_string(x).length() * 4);
|
||||
while (length >= 0) {
|
||||
bitsAsDigits[length-1] = (x % 2 == 0) ? '0' : '1';
|
||||
std::cout << x << " : " << length << " : " << bitsAsDigits[length-1] << std::endl;
|
||||
x = x / 2;
|
||||
length--;
|
||||
evenParity = true;
|
||||
}
|
||||
|
||||
BinaryOctet::BinaryOctet(int x) {
|
||||
for (int i = 0; i <= bitsPerOctet - 1; i++) {
|
||||
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;
|
||||
for (char c : bitsAsDigits) {
|
||||
if (c == '1') i++;
|
||||
}
|
||||
|
||||
evenParity = i % 2 == 0;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
if(a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
|
||||
if (a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -51,7 +58,7 @@ BinaryOctet operator+(BinaryOctet a, BinaryOctet b) {
|
|||
BinaryOctet result;
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
|
@ -67,10 +74,9 @@ BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
|||
return a;
|
||||
}
|
||||
|
||||
BinaryOctet& BinaryOctet::operator=(int const &a) {
|
||||
std::cout << a << std::endl;
|
||||
std::string tmp = to_string(a);
|
||||
for(int i = 0; i < bitsPerOctet; i++) {
|
||||
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
||||
std::string tmp = std::to_string(a);
|
||||
for (int i = 0; i >= tmp.length(); i++) {
|
||||
bitsAsDigits[i] = tmp[i];
|
||||
}
|
||||
return *this;
|
||||
|
@ -98,9 +104,9 @@ std::string as_string(BinaryOctet a) {
|
|||
}
|
||||
|
||||
// for std::cout
|
||||
std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){
|
||||
os << as_string(toBePrinted);
|
||||
return os;
|
||||
std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) {
|
||||
os << as_string(toBePrinted);
|
||||
return os;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
@ -109,9 +115,8 @@ int main(int argc, char **argv) {
|
|||
BinaryOctet b = 0b00000110;
|
||||
std::cout << b << std::endl;
|
||||
std::cout << new BinaryOctet(5) << std::endl;
|
||||
println(a + b);
|
||||
std::cout << a + b << std::endl;
|
||||
//println("result = ", doCalculation(a,b));
|
||||
std::cout << "result = " << doCalculation(a,b) << std::endl;
|
||||
std::cout << "result = " << doCalculation(a, b) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,8 @@ int main() {
|
|||
std::uniform_int_distribution<int> distribution(0, 23);
|
||||
|
||||
for(int i = 0; i <= 41; i++) {
|
||||
array[i] = distribution(generator);
|
||||
int rnd = distribution(generator);
|
||||
array[i] = rnd;
|
||||
}
|
||||
|
||||
unittest_stringSimilarity();
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
int main() {
|
||||
Derived * derived = new Derived();
|
||||
|
||||
bar(derived);
|
||||
foo(derived);
|
||||
|
||||
delete derived;
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ class Base {
|
|||
|
||||
};
|
||||
|
||||
class Derived : Base {
|
||||
class Derived : public Base {
|
||||
};
|
||||
|
||||
void foo(Base* base) {
|
||||
|
|
Loading…
Reference in New Issue