07_STD_MP: Vorbereitung aufs Testat

This commit is contained in:
Johannes Theiner 2018-11-26 22:16:00 +01:00
parent c693417d2b
commit 3ef4f4e1b0
3 changed files with 86 additions and 41 deletions

View File

@ -88,19 +88,44 @@ BinaryOctet BinaryOctet::operator/(BinaryOctet &a) {
while(*this >= a) {
*this = *this - a;
result = result + 1;
result = result + BinaryOctet(1);
}
return result;
}
bool BinaryOctet::operator!=(BinaryOctet &a) {
if (a.evenParity !=evenParity) return false;
bool BinaryOctet::operator==(const BinaryOctet &a) {
if(a.evenParity != evenParity) return false;
for (int i = 0; i < bitsPerOctet; ++i) {
if (a.bitsAsDigits[i] != bitsAsDigits[i]) return false;
if(a.bitsAsDigits[i] != bitsAsDigits[i]) return false;
}
return true;
}
bool BinaryOctet::operator!=(BinaryOctet &a) {
if (a.evenParity != evenParity) return true;
for (int i = 0; i < bitsPerOctet; ++i) {
if (a.bitsAsDigits[i] != bitsAsDigits[i]) return true;
}
return false;
}
bool BinaryOctet::operator<(const BinaryOctet &a) {
int power = 8;
int aI = 0;
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 = 0; i <= bitsPerOctet - 1; i++) {
power--;
bI = bI + (bitsAsDigits[i] - '0') * pow(2, power);
}
return bI < aI;
}
bool BinaryOctet::operator>=(BinaryOctet &a) {
int power = 8;
@ -122,41 +147,12 @@ bool BinaryOctet::operator>=(BinaryOctet &a) {
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
BinaryOctet result;
for (; a != b; b--) {
a = a + 1;
a = a + BinaryOctet(1);
a = a / b;
}
result = a + b;
return result;
}
// for println();
std::string as_string(BinaryOctet a) {
std::string result = "(";
for (char c : a.bitsAsDigits) {
result += c;
}
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
std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) {
os << as_string(toBePrinted);
return os;
}
std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
os << as_string(toBePrinted);
return os;
}

View File

@ -7,11 +7,13 @@ struct BinaryOctet {
bool evenParity;
char bitsAsDigits[bitsPerOctet]{};
BinaryOctet(int x = 0);
explicit BinaryOctet(int x = 0);
BinaryOctet(const BinaryOctet&) = default;
const BinaryOctet operator--(int);
const BinaryOctet operator++(int);
bool operator!=(BinaryOctet &a);
bool operator==(const BinaryOctet &a);
bool operator <(const BinaryOctet &a);
bool operator>=(BinaryOctet &a);
BinaryOctet operator-(BinaryOctet a);
BinaryOctet operator+(BinaryOctet a);

View File

@ -10,6 +10,39 @@
#include <map>
#include <vector>
#include "grundgeruest.hpp"
#include "BinaryOctet.h"
// for println();
std::string as_string(BinaryOctet a) {
std::string result = "(";
for (char c : a.bitsAsDigits) {
result += c;
}
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
std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) {
os << as_string(toBePrinted);
return os;
}
std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
os << as_string(toBePrinted);
return os;
}
int main() {
std::srand(static_cast<unsigned int>(std::time(nullptr)));
@ -25,20 +58,34 @@ int main() {
unittest_stringSimilarity();
std::vector<int> vector;
std::vector<int> intVector;
for (int i = 0; i <= 41; i++) {
int rnd = distribution(generator);
auto found = std::find(vector.begin(), vector.end(), rnd);
if(found == vector.end()) {
vector.push_back(rnd);
auto found = std::find(intVector.begin(), intVector.end(), rnd);
if(found == intVector.end()) {
intVector.push_back(rnd);
}
std::sort(vector.begin(), vector.end());
std::sort(intVector.begin(), intVector.end());
}
for (int ai : vector) {
for (int ai : intVector) {
std::cout << ai << ",";
}
std::cout << std::endl;
std::vector<BinaryOctet> binVector;
for (int j = 0; j < 41; ++j) {
int rnd = distribution(generator);
auto binaryOctet = BinaryOctet(rnd);
auto found = std::find(binVector.begin(), binVector.end(), binaryOctet);
if(found == binVector.end()) {
binVector.push_back(binaryOctet);
}
std::sort(binVector.begin(), binVector.end());
}
for (BinaryOctet bin : binVector) {
std::cout << bin << ",";
}
}