04_UDEF_MP: kleinre Änderungen von denen ich nicht weiß ob die so der Aufgabenstellung nach richtig sind, da Link Aufgaben nicht gut stellen kann

This commit is contained in:
Johannes Theiner 2018-08-29 15:44:59 +02:00
parent b074da8be3
commit 28a90c24b5
2 changed files with 38 additions and 12 deletions

View File

@ -5,15 +5,18 @@
const int bitsPerOctet = 8;
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
char bitsAsDigits[bitsPerOctet]{}; // bit values as chars
BinaryOctet(int x = 0);
BinaryOctet& operator=(int const &a);
BinaryOctet(const BinaryOctet &) = default;
private:
BinaryOctet init(int x);
};
BinaryOctet::BinaryOctet(int x) {
int length = std::to_string(x).length() * 4;
BinaryOctet BinaryOctet::init(int x) {
int length = static_cast<int>(std::to_string(x).length() * 4);
while (x >= 1) {
bitsAsDigits[length] = (x % 2 == 0) ? '1' : '0';
x = x / 2;
@ -25,24 +28,47 @@ BinaryOctet::BinaryOctet(int x) {
if (c == '1') i++;
}
evenParity = i % 2 == 0;
return *this;
}
BinaryOctet::BinaryOctet(int x) {
init(x);
}
bool operator!=(BinaryOctet a, BinaryOctet b) {
if(a.evenParity != b.evenParity) return false;
for (int i = 0; i < bitsPerOctet; ++i) {
if(a.bitsAsDigits[i] != b.bitsAsDigits[i]) return false;
}
return true;
}
BinaryOctet operator--(BinaryOctet &id, int) {
return id;
}
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';
else result.bitsAsDigits[i] = '1';
}
return result;
}
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) {
}
BinaryOctet& BinaryOctet::operator=(int const &i) {
init(i);
return *this;
}
BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
BinaryOctet result;
@ -65,16 +91,17 @@ std::string as_string(BinaryOctet a) {
}
// for std::cout
/*std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){
os << "(" << ")";
std::ostream& operator<< (std::ostream& os, const BinaryOctet &toBePrinted){
os << as_string(toBePrinted);
return os;
}*/
}
int main(int argc, char **argv) {
BinaryOctet a = 0b00001111;
BinaryOctet b = 0b00000110;
println(new BinaryOctet(5));
println(new BinaryOctet(5));
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;
return 0;

View File

@ -17,8 +17,7 @@ add_executable(02_MENT_Testat 02_MENT/Testat/main_02_MENT.cpp)
add_executable(03_FLOW_MP 03_FLOW_a/MP/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
add_executable(03_FLOW_Testat 03_FLOW_a/Testat/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp)
add_executable(04_UDEF_MP_1 04_UDEF/MP/main_04_UDEF_e.cpp)
add_executable(04_UDEF_MP_2 04_UDEF/MP/main_04_UDEF_a.cpp)
add_executable(04_UDEF_MP 04_UDEF/MP/main_04_UDEF_e.cpp)
add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp)
add_executable(05_OO_MP 05_OO/MP/shapes_main.cpp helpers/AnsiConsole.cpp)