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,31 +8,38 @@ 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);
|
||||
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;
|
||||
for (int i = 0; i < bitsPerOctet; ++i) {
|
||||
|
@ -68,9 +75,8 @@ BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
|
|||
}
|
||||
|
||||
BinaryOctet &BinaryOctet::operator=(int const &a) {
|
||||
std::cout << a << std::endl;
|
||||
std::string tmp = to_string(a);
|
||||
for(int i = 0; i < bitsPerOctet; i++) {
|
||||
std::string tmp = std::to_string(a);
|
||||
for (int i = 0; i >= tmp.length(); i++) {
|
||||
bitsAsDigits[i] = tmp[i];
|
||||
}
|
||||
return *this;
|
||||
|
@ -109,7 +115,6 @@ 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;
|
||||
|
|
|
@ -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