MP_04 operator+ added

This commit is contained in:
Johannes Theiner 2018-09-21 23:54:09 +02:00
parent 3b1f7b9af3
commit 9aa4d96928
1 changed files with 68 additions and 19 deletions

View File

@ -13,6 +13,7 @@ public:
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);
const BinaryOctet operator+(BinaryOctet a);
}; };
BinaryOctet::BinaryOctet() { BinaryOctet::BinaryOctet() {
@ -22,12 +23,13 @@ BinaryOctet::BinaryOctet() {
evenParity = true; evenParity = true;
} }
BinaryOctet::BinaryOctet(int x) { BinaryOctet::BinaryOctet(int x) {
for (int i = 0; i <= bitsPerOctet - 1; i++) { for (int i = 0; i <= bitsPerOctet - 1; i++) {
int tmp = ((x >> i) & 1); int tmp = ((x >> i) & 1);
bitsAsDigits[i] = (tmp == 1) ? '1' : '0'; bitsAsDigits[i] = (tmp == 1) ? '1' : '0';
} }
//TODO: das sollte eigentlich nicht benötigt werden //FIXME: das sollte eigentlich nicht benötigt werden
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits)); std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
//einsen zählen //einsen zählen
@ -48,31 +50,56 @@ bool operator!=(BinaryOctet a, BinaryOctet b) {
return true; return true;
} }
const BinaryOctet BinaryOctet::operator--(const int) {
//TODO: this method
const BinaryOctet BinaryOctet::operator--(const int) {
bool over = false;
for(int i = bitsPerOctet - 1; i >= 0; --i) {
int tmp = bitsAsDigits[i] - '0';
bitsAsDigits[i] = static_cast<char>(tmp + '0');
}
return *this; return *this;
} }
BinaryOctet operator+(BinaryOctet a, BinaryOctet b) {
BinaryOctet result;
for (int i = 0; i < bitsPerOctet; ++i) { const BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
if (a.bitsAsDigits[i] == b.bitsAsDigits[i]) result.bitsAsDigits[i] = '0'; BinaryOctet result;
else result.bitsAsDigits[i] = '1'; bool over = false;
for (int i = bitsPerOctet - 1; i >= 0; --i) {
//Arbeitsvariablen anlegen und char in int umwandeln für einfachere Rechnung
int tmp;
int aI = a.bitsAsDigits[i] - '0';
int bI = bitsAsDigits[i] - '0';
if(over) tmp = aI + bI + 1;
else tmp = aI + bI;
over = false;
if(tmp >= 2) {
over = true;
if (tmp % 2 == 0) tmp = 0;
else tmp = 1;
}
//Umwandlung in char und ins Ergebnis schreiben
result.bitsAsDigits[i] = static_cast<char>(tmp + '0');
} }
return result;
}
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
BinaryOctet result;
return result; return result;
} }
BinaryOctet operator+(BinaryOctet a, int b) {
}
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
return a;
}
BinaryOctet &BinaryOctet::operator=(int const &a) { BinaryOctet &BinaryOctet::operator=(int const &a) {
std::string tmp = std::to_string(a); std::string tmp = std::to_string(a);
@ -82,9 +109,9 @@ BinaryOctet &BinaryOctet::operator=(int const &a) {
return *this; return *this;
} }
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) { BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
BinaryOctet result; BinaryOctet result;
for (; a != b; b--) { for (; a != b; b--) {
a = a + 1; a = a + 1;
a = a / b; a = a / b;
@ -93,6 +120,7 @@ BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
return result; return result;
} }
// for println(); // for println();
std::string as_string(BinaryOctet a) { std::string as_string(BinaryOctet a) {
std::string result = "("; std::string result = "(";
@ -103,20 +131,41 @@ std::string as_string(BinaryOctet a) {
return result; return result;
} }
std::string as_string(BinaryOctet *a) {
std::string result = "(";
for (char c : a->bitsAsDigits) {
result += c;
}
result += ")";
return result;
}
// 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;
} }
std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
os << as_string(toBePrinted);
return os;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
BinaryOctet a = 0b00001111; BinaryOctet a = 0b00001111;
std::cout << a << std::endl; std::cout << a << std::endl;
BinaryOctet b = 0b00000110; BinaryOctet b = 0b00000110;
std::cout << b << std::endl; std::cout << b << std::endl;
std::cout << new BinaryOctet(5) << std::endl; b--;
std::cout << b << std::endl;
auto *c = new BinaryOctet(5);
std::cout << c << std::endl;
std::cout << a + b << std::endl; 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; return 0;
} }