diff --git a/02_MENT/Testat/main_02_MENT.cpp b/02_MENT/Testat/main_02_MENT.cpp index 7187d34..5ca37c4 100644 --- a/02_MENT/Testat/main_02_MENT.cpp +++ b/02_MENT/Testat/main_02_MENT.cpp @@ -19,6 +19,7 @@ void printPascalString(PascalString s) { } PascalString bitwiseDualOr(PascalString a, PascalString b) { + PascalString shortString = a.length <= b.length ? a : b; PascalString longString = a.length <= b.length ? b : a; diff --git a/03_FLOW_a/MP/build.sh b/03_FLOW/MP/build.sh similarity index 100% rename from 03_FLOW_a/MP/build.sh rename to 03_FLOW/MP/build.sh diff --git a/03_FLOW_a/MP/main_mp2_FLOW_a.cpp b/03_FLOW/MP/main_mp2_FLOW_a.cpp similarity index 100% rename from 03_FLOW_a/MP/main_mp2_FLOW_a.cpp rename to 03_FLOW/MP/main_mp2_FLOW_a.cpp diff --git a/03_FLOW_a/Testat/build.sh b/03_FLOW/Testat/build.sh old mode 100755 new mode 100644 similarity index 100% rename from 03_FLOW_a/Testat/build.sh rename to 03_FLOW/Testat/build.sh diff --git a/03_FLOW_a/Testat/main_mp2_FLOW_a.cpp b/03_FLOW/Testat/main_mp2_FLOW_a.cpp similarity index 100% rename from 03_FLOW_a/Testat/main_mp2_FLOW_a.cpp rename to 03_FLOW/Testat/main_mp2_FLOW_a.cpp diff --git a/03_FLOW_b/build.sh b/03_FLOW_b/build.sh deleted file mode 100644 index 844f053..0000000 --- a/03_FLOW_b/build.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -clang++ -std=c++14 -o controlFlowIntr main_ISR.cpp diff --git a/03_FLOW_b/controlFlowIntr b/03_FLOW_b/controlFlowIntr deleted file mode 100644 index dcccacc..0000000 Binary files a/03_FLOW_b/controlFlowIntr and /dev/null differ diff --git a/03_FLOW_b/main_ISR.cpp b/03_FLOW_b/main_ISR.cpp deleted file mode 100644 index 8f87b34..0000000 --- a/03_FLOW_b/main_ISR.cpp +++ /dev/null @@ -1,27 +0,0 @@ -// file main_ISR.cpp -#include -#include -#include - -volatile int j; - -//extern "C" { -void ISR(int iarg){ - std::cout << "ISR j=" << j << "\n"; -} -//} - -void install_ISR(void){ - signal(SIGUSR1, ISR); -} - -int main(int argc, const char * argv[]) { - std::cout << "process ID = " << getpid() << "\n"; - install_ISR(); - for(int i=0; i<300*1000*1000; i++){ - i= i +10; i-=10; j=i; - } - std::cout << "done.\n"; - return 0; -} - diff --git a/03_FLOW_b/run.sh b/03_FLOW_b/run.sh deleted file mode 100644 index b4b5f7d..0000000 --- a/03_FLOW_b/run.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash -./controlFlowIntr & -echo $! -sleep 1 -kill -SIGUSR1 $! -sleep 1 -kill -SIGUSR1 $! -kill -SIGUSR1 $! - diff --git a/03_FLOW_c/AP2-dc.cpp b/03_FLOW_c/AP2-dc.cpp deleted file mode 100644 index 5a545b3..0000000 --- a/03_FLOW_c/AP2-dc.cpp +++ /dev/null @@ -1,239 +0,0 @@ -// based on http://www.stroustrup.com/dc.c -// by Bjarne Stroustrup -// The desk calculator -// pp 107-117, sec 6.1, A Desk calculator -// No guarantees offered. Constructive comments to bs@research.att.com - - -/* - expr_list: - expression PRINT // PRINT is \n - expression PRINT expr_list - - expression: - expression + term - expression - term - term - - term: - term / power - term * power - power - - power: - power # primary // char '^' produces problems with terminal input - primary - - - primary: - NUMBER - ( expression ) - - -while statt for -erweitern um o,i oder HexaDezimal mit &, |, x, - */ - - -#include -#include -#include - -using namespace std; - -int no_of_errors; // note: default initialized to 0 -bool doPrintCalculations = true; - - -unsigned int error(const char* s) -{ - no_of_errors++; - cerr << "error: " << s << '\n'; - return 1; -} - -enum Token_value { - NUMBER, // '0' ... '9' - PLUS='+', MINUS='-', MUL='*', DIV='/', - PRINT='\n', POWER='#', LP='(', RP=')' -}; - -Token_value global_token = PRINT; -unsigned int number_value; - -unsigned int asciToInt(string string_value){ - unsigned int result = 0; - for(size_t i=0; i> number_value; - string string_value; - string_value = ch; - while (cin.get(ch) && isdigit(ch)) - string_value += ch; // string_value.push_back(ch); // to work around library bug - cin.putback(ch); - number_value = asciToInt(string_value); - return global_token=NUMBER; - } - default: - error("bad token"); - return global_token=PRINT; - } -} - - -unsigned int expression(); // cannot do without (indirect recursion) - - -unsigned int primary() // handle primaries -{ - Token_value current_token = get_token(); - - switch (current_token) { - case NUMBER:{ - unsigned int v = number_value; - get_token(); // proceed global_token to next token (i.e. operator or '\n') - return v; - } - case LP:{ - unsigned int e = expression(); - if (global_token != RP) return error(") expected"); - get_token(); // eat ')' in order to proceed global_token to next token - return e; - } - default: - return error("primary expected"); - } -} - -unsigned int power() // 2 ^ 3 -{ - unsigned int left = primary(); - unsigned int right = 0; - - for (;;) - switch (global_token) { - case POWER:{ - right = primary(); - if (doPrintCalculations) printf("%u # %u\n", left, right); - unsigned int base = left; - left = 1; - for (int i=0; i "; - cout << expression() << '\n'; - } - return no_of_errors; -} diff --git a/03_FLOW_c/build.sh b/03_FLOW_c/build.sh deleted file mode 100644 index 3674a68..0000000 --- a/03_FLOW_c/build.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -clang++ -std=c++14 -o dc.out AP2-dc.cpp diff --git a/04_UDEF/MP/main_04_UDEF_e.cpp b/04_UDEF/MP/main_04_UDEF_e.cpp index cc84326..a9afe15 100644 --- a/04_UDEF/MP/main_04_UDEF_e.cpp +++ b/04_UDEF/MP/main_04_UDEF_e.cpp @@ -1,30 +1,20 @@ -// file main_04_UDEF_e.cpp #include #include 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 - BinaryOctet(); - BinaryOctet(int x); - BinaryOctet(const BinaryOctet &) = default; - BinaryOctet &operator=(int const &a); - const BinaryOctet operator--(int); - const BinaryOctet operator-(BinaryOctet a); - const BinaryOctet operator+(BinaryOctet a); + bool evenParity; + char bitsAsDigits[bitsPerOctet]{}; + BinaryOctet(int x = 0); + BinaryOctet(const BinaryOctet&) = default; + BinaryOctet &operator=(int &a); + BinaryOctet operator--(int); + BinaryOctet operator-(BinaryOctet &a); + BinaryOctet operator+(BinaryOctet a); + BinaryOctet operator/(BinaryOctet &a); }; -BinaryOctet::BinaryOctet() { - for (char &bitsAsDigit : bitsAsDigits) { - bitsAsDigit = '0'; - } - evenParity = true; -} - - BinaryOctet::BinaryOctet(int x) { for (char &bitsAsDigit : bitsAsDigits) { bitsAsDigit = '0'; @@ -43,41 +33,43 @@ BinaryOctet::BinaryOctet(int x) { } evenParity = i % 2 == 0; + //std::cout << evenParity << std::endl; } - -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; +BinaryOctet &BinaryOctet::operator=(int &a) { + std::string tmp = std::to_string(a); + for (int i = 0; i >= tmp.length(); i++) { + bitsAsDigits[i] = tmp[i]; } - return true; + return *this; } - -//TODO: this method -const BinaryOctet BinaryOctet::operator--(const int) { - return operator-(1); +BinaryOctet BinaryOctet::operator--(int) { + return operator-(*new BinaryOctet(1)); } -//FIXME: returns wrong result -//using complement method here -const BinaryOctet BinaryOctet::operator-(BinaryOctet a) { +BinaryOctet BinaryOctet::operator-(BinaryOctet &a) { BinaryOctet result; - //reverse a - for(int i = 0; i <= bitsPerOctet; i++) { - a.bitsAsDigits[i] = a.bitsAsDigits[i] == '0' ? '1' : '0'; + 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(tmp + '0'); } - a + 1; - - result = *this + a; - std::cout << result.evenParity << std::endl; - return result; } - -const BinaryOctet BinaryOctet::operator+(BinaryOctet a) { +BinaryOctet BinaryOctet::operator+(BinaryOctet a) { BinaryOctet result; bool over = false; for (int i = bitsPerOctet - 1; i >= 0; --i) { @@ -104,23 +96,22 @@ const BinaryOctet BinaryOctet::operator+(BinaryOctet a) { return result; } - -BinaryOctet operator/(BinaryOctet a, BinaryOctet b) { +BinaryOctet BinaryOctet::operator/(BinaryOctet &a) { BinaryOctet result; + + return result; } - -BinaryOctet &BinaryOctet::operator=(int const &a) { - std::string tmp = std::to_string(a); - for (int i = 0; i >= tmp.length(); i++) { - bitsAsDigits[i] = tmp[i]; +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 *this; + return true; } - BinaryOctet doCalculation(BinaryOctet a, BinaryOctet b) { BinaryOctet result; for (; a != b; b--) { @@ -152,7 +143,6 @@ std::string as_string(BinaryOctet *a) { return result; } - // for std::cout std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) { os << as_string(toBePrinted); @@ -163,7 +153,6 @@ std::ostream &operator<<(std::ostream &os, const BinaryOctet &toBePrinted) { std::ostream &operator<<(std::ostream &os, BinaryOctet *toBePrinted) { os << as_string(toBePrinted); return os; - } @@ -192,4 +181,4 @@ int main(int argc, char **argv) { std::cout << "============== doCalculation ================" << std::endl; std::cout << "result = " << doCalculation(a, b) << std::endl; return 0; -} +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7969eb2..bc68644 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,8 @@ add_executable(01_ENV_Testat 01_ENV/Testat/main.c 01_ENV/Testat/func1.c) add_executable(02_MENT_MP 02_MENT/MP/main_02_MENT.cpp) 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(03_FLOW_MP 03_FLOW/MP/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp) +add_executable(03_FLOW_Testat 03_FLOW/Testat/main_mp2_FLOW_a.cpp helpers/AnsiConsole.cpp) add_executable(04_UDEF_MP 04_UDEF/MP/main_04_UDEF_e.cpp) add_executable(04_UDEF_Testat 04_UDEF/Testat/Testat.cpp)