From cb3cb443cd00ebd74bb8516a7713fba53c3991bd Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Sun, 30 Sep 2018 17:34:48 +0200 Subject: [PATCH] 04_UDEF_MP BinaryOctet funktionierende Subtraktion --- 02_MENT/Testat/main_02_MENT.cpp | 1 + {03_FLOW_a => 03_FLOW}/MP/build.sh | 0 {03_FLOW_a => 03_FLOW}/MP/main_mp2_FLOW_a.cpp | 0 {03_FLOW_a => 03_FLOW}/Testat/build.sh | 0 .../Testat/main_mp2_FLOW_a.cpp | 0 03_FLOW_b/build.sh | 2 - 03_FLOW_b/controlFlowIntr | Bin 15156 -> 0 bytes 03_FLOW_b/main_ISR.cpp | 27 -- 03_FLOW_b/run.sh | 9 - 03_FLOW_c/AP2-dc.cpp | 239 ------------------ 03_FLOW_c/build.sh | 2 - 04_UDEF/MP/main_04_UDEF_e.cpp | 97 ++++--- CMakeLists.txt | 4 +- 13 files changed, 46 insertions(+), 335 deletions(-) rename {03_FLOW_a => 03_FLOW}/MP/build.sh (100%) rename {03_FLOW_a => 03_FLOW}/MP/main_mp2_FLOW_a.cpp (100%) rename {03_FLOW_a => 03_FLOW}/Testat/build.sh (100%) mode change 100755 => 100644 rename {03_FLOW_a => 03_FLOW}/Testat/main_mp2_FLOW_a.cpp (100%) delete mode 100644 03_FLOW_b/build.sh delete mode 100644 03_FLOW_b/controlFlowIntr delete mode 100644 03_FLOW_b/main_ISR.cpp delete mode 100644 03_FLOW_b/run.sh delete mode 100644 03_FLOW_c/AP2-dc.cpp delete mode 100644 03_FLOW_c/build.sh 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 dcccacc3d2934304b7f492c5fb90ffb415bea7ed..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15156 zcmeHOeQX@Zb)SVD@c$vPkVx4fD zL@u9Arh5&)T3^?Ctq*D>5Lbq3-Hua8e>R!!agymn0}!m%H)Qk;8YRTlIx6*ZoQ$9Y z9jA5Y{+$&$F>RL&P_ls~aKIvPoL~%>``U5RDJOTNzk49%Wb)a!)n|%}K9(;9_9uo; zIXF)5K*g}TN?(0U+jqMuL0qlx{bg-zUn>4i_H8oyMok&wN_}kaubDg&^y@%>|3JFL zs`lSxOxssE7b^RKbEYyZ&HLSLdmgh|+xDB>hN^x5X4r1#8||vpbvM=(5kXXkygJZ6 z7BC?LJPSjbw<*;H9&Mr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*aAefTL%Q<6J$voF|nd)317Nb1!=rJ4}UG zdHGt}TbQeNsrS6AyjNAS;#MfRmYw7pi(gaKvU@C5Xbu!hjOAyEC4S`wv&1d;&AZ!L zRw!8BRFJoPJ6T~LF|4HxWaX1iY2zsE7^SU(XAkZXR~>_v>`LX@RyvqiJp4F~)t$TF zv8>J=PRC;j&JQyrw{CZwOd;>^Y!gplf5e{qRKZOra6)_cy7xQ#8>syOXz$$a^jB0+ zeQer+HudKFtmS4j;^>r#_sA}fLnADDyXtAwgXZYdT{z? -#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)