diff --git a/P04_Modularisieren_von_C_Code/work/modularize/bin/triangle b/P04_Modularisieren_von_C_Code/work/modularize/bin/triangle new file mode 100755 index 0000000..fddacc5 Binary files /dev/null and b/P04_Modularisieren_von_C_Code/work/modularize/bin/triangle differ diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/read.c b/P04_Modularisieren_von_C_Code/work/modularize/src/read.c index 16187fe..17f07a6 100644 --- a/P04_Modularisieren_von_C_Code/work/modularize/src/read.c +++ b/P04_Modularisieren_von_C_Code/work/modularize/src/read.c @@ -12,5 +12,79 @@ * @brief Lab implementation */ // begin students to add code for task 4.1 +#define EOL 10 +#define PARSE_ERROR -1 +#define READ_ERROR -2 +#define ASCII_SPACE 32 +#define ASCII_DIGIT_0 48 +#define ASCII_DIGIT_9 57 +#define NO_POS -1 +#define BUFFERSIZE 10 +#define EOF -1 +int getInt(int maxResult) { + // end of input + //int EOF = -1; // end of file + //int EOL = 10; // end of line + // abnormal return values + //int PARSE_ERROR = -1; + //int READ_ERROR = -2; + // ASCII Codes + //int ASCII_SPACE = 32; // ' ' + //int ASCII_DIGIT_0 = 48; // '0' + //int ASCII_DIGIT_9 = 57; // '9' + + // conversion buffer + //int NO_POS = -1; + //int BUFFERSIZE = 10; + char buffer[BUFFERSIZE] = {0}; + + int result = 0; + + // read line: up to EOL or EOF (i.e. error while reading) + int bytes = 0; + int input; + scanf("%d", &input); + while ((input != EOL) && (input != EOF)) { // read whole line + if (bytes < BUFFERSIZE) { // only buffer first n characters + buffer[bytes] = (char)input; + bytes++; + } else { + result = PARSE_ERROR; // exceed buffer size, continue read line + } + scanf("%d", &input); + } + if (input == EOF) { + result = READ_ERROR; + } + // check for numbers: skip leading and trailing spaces + // (i.e. this includes all control chars below the space ASCII code) + int pos = 0; + while((pos < bytes) && (buffer[pos] <= ASCII_SPACE)) pos++; // skip SP + int posOfFirstDigit = pos; + int posOfLastDigit = NO_POS; + while ((pos < bytes) + && (buffer[pos] >= ASCII_DIGIT_0) + && (buffer[pos] <= ASCII_DIGIT_9)) + { + posOfLastDigit = pos; + pos++; + } + while((pos < bytes) && (buffer[pos] <= ASCII_SPACE)) pos++; // skip SP + // produce return value + if (result != 0) { + // previously detected read or parse error given + } else if ((pos != bytes) || (posOfLastDigit == NO_POS)) { + result = PARSE_ERROR; + } else { // convert number + for(int i = posOfFirstDigit; i <= posOfLastDigit; i++) { + result = result * 10 + (buffer[i] - ASCII_DIGIT_0); + if (result > maxResult) { + result = PARSE_ERROR; + break; + } + } + } + return result; +} // end students to add code diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/read.d b/P04_Modularisieren_von_C_Code/work/modularize/src/read.d new file mode 100644 index 0000000..bc19a4f --- /dev/null +++ b/P04_Modularisieren_von_C_Code/work/modularize/src/read.d @@ -0,0 +1 @@ +src/read.o: src/read.c /usr/include/stdc-predef.h diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/read.o b/P04_Modularisieren_von_C_Code/work/modularize/src/read.o new file mode 100644 index 0000000..ec055bf Binary files /dev/null and b/P04_Modularisieren_von_C_Code/work/modularize/src/read.o differ diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.c b/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.c index 16187fe..63234fd 100644 --- a/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.c +++ b/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.c @@ -12,5 +12,24 @@ * @brief Lab implementation */ // begin students to add code for task 4.1 +int Rectangular(int a, int b, int c) { + int aS = a*a; + int bS = b*b; + int cS = c*c; + + int isRightAngled; + if ((a == 0) && (b == 0) && (c == 0)) + isRightAngled = 0; + else if ((aS + bS) == cS) + isRightAngled = 1; + else if ((aS + cS) == bS) + isRightAngled = 1; + else if ((bS + cS) == aS) + isRightAngled = 1; + else + isRightAngled = 0; + + return isRightAngled; +} // end students to add code diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.d b/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.d new file mode 100644 index 0000000..501f89c --- /dev/null +++ b/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.d @@ -0,0 +1 @@ +src/rectang.o: src/rectang.c /usr/include/stdc-predef.h diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.o b/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.o new file mode 100644 index 0000000..79f4bce Binary files /dev/null and b/P04_Modularisieren_von_C_Code/work/modularize/src/rectang.o differ diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/triangle b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle new file mode 100755 index 0000000..0b90df3 Binary files /dev/null and b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle differ diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.c b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.c index b6a414b..d052da4 100644 --- a/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.c +++ b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.c @@ -15,9 +15,11 @@ #include #include "read.h" #include "rectang.h" - +int getInt(int maxResult); +int Rectangular(int a, int b, int c); /// max side length #define MAX_NUMBER 1000 +#define READ_ERROR -2 /** @@ -27,7 +29,53 @@ int main(void) { // begin students to add code for task 4.1 - + + while (1) { + printf("\nDreiecksbestimmung (CTRL-C: Abbruch)\n"); + + int word = 0; + int a = 0; + int b = 0; + int c = 0; + + do { + printf("Seite a: "); + word = getInt(MAX_NUMBER); + } + while ((word < 0) && (word != READ_ERROR)); + if (word >= 0) + a = word; + else + break; + + do { + printf("Seite b: "); + word = getInt(MAX_NUMBER); + } + while ((word < 0) && (word != READ_ERROR)); + if (word >= 0) + b = word; + else + break; + + do { + printf("Seite c: "); + word = getInt(MAX_NUMBER); + } + while ((word < 0) && (word != READ_ERROR)); + if (word >= 0) + c = word; + else + break; + + if (Rectangular(a, b, c) == 1) + printf("-> Dreieck %d-%d-%d ist rechtwinklig", a, b, c); + else + printf("-> Dreieck %d-%d-%d ist nicht rechtwinklig", a, b, c); + printf("\n"); + } + printf("\n\nbye bye\n"); + // end students to add code return EXIT_SUCCESS; } diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.d b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.d new file mode 100644 index 0000000..48a0788 --- /dev/null +++ b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.d @@ -0,0 +1,23 @@ +src/triangle.o: src/triangle.c /usr/include/stdc-predef.h \ + /usr/include/stdio.h \ + /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \ + /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \ + /usr/include/x86_64-linux-gnu/bits/wordsize.h \ + /usr/include/x86_64-linux-gnu/bits/long-double.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \ + /usr/include/x86_64-linux-gnu/bits/types.h \ + /usr/include/x86_64-linux-gnu/bits/typesizes.h \ + /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \ + /usr/include/x86_64-linux-gnu/bits/types/FILE.h \ + /usr/include/x86_64-linux-gnu/bits/libio.h \ + /usr/include/x86_64-linux-gnu/bits/_G_config.h \ + /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \ + /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \ + /usr/include/x86_64-linux-gnu/bits/sys_errlist.h /usr/include/stdlib.h \ + /usr/include/x86_64-linux-gnu/bits/floatn.h \ + /usr/include/x86_64-linux-gnu/bits/floatn-common.h \ + /usr/include/x86_64-linux-gnu/bits/stdlib-float.h src/read.h \ + src/rectang.h diff --git a/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.o b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.o new file mode 100644 index 0000000..cd8bc91 Binary files /dev/null and b/P04_Modularisieren_von_C_Code/work/modularize/src/triangle.o differ diff --git a/P04_Modularisieren_von_C_Code/work/modularize/tests/tests.d b/P04_Modularisieren_von_C_Code/work/modularize/tests/tests.d new file mode 100644 index 0000000..2abec9a --- /dev/null +++ b/P04_Modularisieren_von_C_Code/work/modularize/tests/tests.d @@ -0,0 +1,40 @@ +tests/tests.o: tests/tests.c /usr/include/stdc-predef.h \ + /usr/include/stdio.h \ + /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \ + /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \ + /usr/include/x86_64-linux-gnu/bits/wordsize.h \ + /usr/include/x86_64-linux-gnu/bits/long-double.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs.h \ + /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stddef.h \ + /usr/include/x86_64-linux-gnu/bits/types.h \ + /usr/include/x86_64-linux-gnu/bits/typesizes.h \ + /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \ + /usr/include/x86_64-linux-gnu/bits/types/FILE.h \ + /usr/include/x86_64-linux-gnu/bits/libio.h \ + /usr/include/x86_64-linux-gnu/bits/_G_config.h \ + /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \ + /usr/lib/gcc/x86_64-linux-gnu/7/include/stdarg.h \ + /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \ + /usr/include/x86_64-linux-gnu/bits/sys_errlist.h /usr/include/stdlib.h \ + /usr/include/x86_64-linux-gnu/bits/floatn.h \ + /usr/include/x86_64-linux-gnu/bits/floatn-common.h \ + /usr/include/x86_64-linux-gnu/bits/stdlib-float.h \ + /usr/include/CUnit/Basic.h /usr/include/CUnit/CUnit.h \ + /usr/include/string.h /usr/include/math.h \ + /usr/include/x86_64-linux-gnu/bits/math-vector.h \ + /usr/include/x86_64-linux-gnu/bits/libm-simd-decl-stubs.h \ + /usr/include/x86_64-linux-gnu/bits/flt-eval-method.h \ + /usr/include/x86_64-linux-gnu/bits/fp-logb.h \ + /usr/include/x86_64-linux-gnu/bits/fp-fast.h \ + /usr/include/x86_64-linux-gnu/bits/mathcalls-helper-functions.h \ + /usr/include/x86_64-linux-gnu/bits/mathcalls.h \ + /usr/include/CUnit/CUError.h /usr/include/errno.h \ + /usr/include/x86_64-linux-gnu/bits/errno.h /usr/include/linux/errno.h \ + /usr/include/x86_64-linux-gnu/asm/errno.h \ + /usr/include/asm-generic/errno.h /usr/include/asm-generic/errno-base.h \ + /usr/include/CUnit/TestDB.h /usr/include/setjmp.h \ + /usr/include/x86_64-linux-gnu/bits/setjmp.h \ + /usr/include/x86_64-linux-gnu/bits/types/__sigset_t.h \ + /usr/include/CUnit/TestRun.h \ + /home/vagrant/snp/testlib/include/test_utils.h diff --git a/P04_Modularisieren_von_C_Code/work/modularize/tests/tests.o b/P04_Modularisieren_von_C_Code/work/modularize/tests/tests.o new file mode 100644 index 0000000..185a1f6 Binary files /dev/null and b/P04_Modularisieren_von_C_Code/work/modularize/tests/tests.o differ