04_UDEF_MP: Vorbereitet für Testat

This commit is contained in:
Johannes Theiner 2018-10-25 14:05:44 +02:00
parent 6ff4a41273
commit 6abaf92ecf
3 changed files with 163 additions and 54 deletions

View File

@ -1,10 +1,7 @@
#include <stdio.h> /* file main_mp2_FLOW_a.cpp */ #include <stdio.h> /* file main_mp2_FLOW_a.cpp */
#include "../../helpers/AnsiConsole.h" #include "../../helpers/AnsiConsole.h"
//TODO: verstehen
AnsiConsole console; int firstLine; int currentTick; Colors currentColor; AnsiConsole console; int firstLine; int currentTick; Colors currentColor;
#define INITPRINT(label) {firstLine=__LINE__;console.printText(2*currentTick,0,label,Colors::BLACK);} #define INITPRINT(label) {firstLine=__LINE__;console.printText(2*currentTick,0,label,Colors::WHITE);}
#define PRINT printLineNumber(__LINE__) #define PRINT printLineNumber(__LINE__)
void printLineNumber(int lineNumber);void mod1();void mod2();void mod1a(); void printLineNumber(int lineNumber);void mod1();void mod2();void mod1a();
void mod1() { void mod1() {
@ -12,7 +9,9 @@ void mod1() {
PRINT;mod1a(); PRINT;mod1a();
PRINT; PRINT;
} }
void mod1a() { void mod1a() {
INITPRINT("Modification 1a");
for(int i = 0; i < 6; i++) { PRINT; for(int i = 0; i < 6; i++) { PRINT;
}for(int i = 0; i < 7; i++) { }for(int i = 0; i < 7; i++) {
PRINT; PRINT;

View File

@ -117,8 +117,6 @@ bool BinaryOctet::operator!=(BinaryOctet &a) {
return true; return true;
} }
//TODO: this
bool BinaryOctet::operator>=(BinaryOctet &a) { bool BinaryOctet::operator>=(BinaryOctet &a) {
int power = 8; int power = 8;
@ -147,17 +145,6 @@ BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) {
return result; return result;
} }
int doCalculation(int a, int b) {
int result;
for (; a != b; b--) {
a = a + 1;
a = a / b;
}
result = a + b;
return result;
}
// for println(); // for println();
std::string as_string(BinaryOctet a) { std::string as_string(BinaryOctet a) {
std::string result = "("; std::string result = "(";
@ -192,7 +179,6 @@ std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
std::cout << "========= init ==========" << std::endl; std::cout << "========= init ==========" << std::endl;
BinaryOctet a = 0b00001111; //15 BinaryOctet a = 0b00001111; //15
std::cout << "a = " << a << std::endl; std::cout << "a = " << a << std::endl;
@ -214,14 +200,10 @@ int main(int argc, char **argv) {
std::cout << "========= operator/ ==========" << std::endl; std::cout << "========= operator/ ==========" << std::endl;
std::cout << "a / b = " << a / b << std::endl; std::cout << "a / b = " << a / b << std::endl;
std::cout << "======== operator-- ============" << std::endl; std::cout << "======== operator-- ============" << std::endl;
BinaryOctet d = 0b111111111; BinaryOctet d = 0b111111111;
d--; d--;
std::cout << "11111111-- = " << d-- << std::endl; std::cout << "11111111-- = " << d-- << std::endl;
std::cout << "============== doCalculation ================" << std::endl;
std::cout << "result = " << doCalculation(a, b) << std::endl;
//std::cout << "result = " << doCalculation(0b00001111, 0b00000110) << std::endl;
return 0; return 0;
} }

View File

@ -1,51 +1,142 @@
#include "../../helpers/println.hpp"
#include <iostream> #include <iostream>
#include <algorithm>
#include <math.h>
const int bitsPerOctet = 8; const int bitsPerOctet = 8;
struct BinaryOctet { struct BinaryOctet {
bool evenParity; // set to true if number of '1' in bitsAsDigits is even, false otherwise bool evenParity;
char bitsAsDigits[bitsPerOctet]; // bit values as chars char bitsAsDigits[bitsPerOctet]{};
BinaryOctet(int x = 0);
BinaryOctet(int x = 0);
BinaryOctet(const BinaryOctet&) = default; BinaryOctet(const BinaryOctet&) = default;
const BinaryOctet operator--(int);
const BinaryOctet operator++(int);
bool operator!=(BinaryOctet &a);
bool operator>=(BinaryOctet &a);
BinaryOctet operator-(BinaryOctet a);
BinaryOctet operator+(BinaryOctet a);
BinaryOctet operator/(BinaryOctet &a);
}; };
BinaryOctet::BinaryOctet(int x) { BinaryOctet::BinaryOctet(int x) {
int length = std::to_string(x).length() * 4; for (char &bitsAsDigit : bitsAsDigits) {
while (x >= 1) { bitsAsDigit = '0';
bitsAsDigits[length] = (x % 2 == 0) ? '1' : '0';
x = x / 2;
length--;
} }
int i = 0; int remainder;
int temp = 0;
while (x != 0) {
remainder = x % 2;
x /= 2;
bitsAsDigits[temp] = static_cast<char>(remainder + '0');
temp++;
}
std::reverse(std::begin(bitsAsDigits), std::end(bitsAsDigits));
int ones = 0;
for (char c : bitsAsDigits) { for (char c : bitsAsDigits) {
if (c == '1') i++; if (c == '1') ones++;
}
evenParity = i % 2 == 0;
} }
bool operator!=(BinaryOctet a, BinaryOctet b) { evenParity = ones % 2 == 0;
} }
BinaryOctet operator--(BinaryOctet &id, int) { const BinaryOctet BinaryOctet::operator--(int) {
return operator-(*new BinaryOctet(1));
} }
BinaryOctet operator+(BinaryOctet a, BinaryOctet b) { const BinaryOctet BinaryOctet::operator++(int) {
return operator+(*new BinaryOctet(1));
} }
BinaryOctet operator/(BinaryOctet a, BinaryOctet b) { BinaryOctet BinaryOctet::operator+(BinaryOctet a) {
BinaryOctet result;
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 BinaryOctet::operator-(BinaryOctet a) {
BinaryOctet result;
bool over = false;
for(int i = bitsPerOctet - 1; i >= 0; --i) {
int tmp;
int aI = a.bitsAsDigits[i] - '0';
int bI = bitsAsDigits[i] - '0';
if(over) tmp = bI - aI - 1;
else tmp = bI - aI;
over = false;
if(tmp < 0) {
over = true;
tmp = tmp + 1;
}
result.bitsAsDigits[i] = static_cast<char>(tmp + '0');
}
return result;
}
BinaryOctet BinaryOctet::operator/(BinaryOctet &a) {
BinaryOctet result = *new BinaryOctet(0);
while(*this >= a) {
*this = *this - a;
result = result + 1;
}
return result;
}
bool BinaryOctet::operator!=(BinaryOctet &a) {
if (a.evenParity !=evenParity) return false;
for (int i = 0; i < bitsPerOctet; ++i) {
if (a.bitsAsDigits[i] != bitsAsDigits[i]) return false;
}
return true;
}
bool BinaryOctet::operator>=(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;
} }
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;
@ -64,18 +155,55 @@ 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 << "(" << ")"; 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; std::cout << "========= init ==========" << std::endl;
BinaryOctet b = 0b00000110; BinaryOctet a = 0b00001111; //15
println(new BinaryOctet(5)); std::cout << "a = " << a << std::endl;
println(new BinaryOctet(5)); BinaryOctet b = 0b00000110;//6
//println("result = ", doCalculation(a,b)); std::cout << "b = " << b << std::endl;
//std::cout << "result = " << doCalculation(a,b) << std::endl; auto *c = new BinaryOctet(5);
std::cout << "c = " << c << std::endl;
std::cout << "======== operator+ ===========" <<std::endl;
std::cout << "a + b = " << a + b << std::endl;
std::cout << "========= operator- ==========" << std::endl;
std::cout << "a - b = " << a - b << std::endl;
std::cout << "========= operator>= ==========" << std::endl;
bool isGreater = a >= b;
std::cout << "a >= b = " << isGreater << std::endl;
std::cout << "========= operator/ ==========" << std::endl;
std::cout << "a / b = " << a / b << std::endl;
std::cout << "======== operator-- ============" << std::endl;
BinaryOctet d = 0b111111111;
d--;
std::cout << "11111111-- = " << d-- << std::endl;
return 0; return 0;
} }