add pages

This commit is contained in:
stsh
2022-02-17 14:51:10 +01:00
parent 38b7891b86
commit 903e42182c
137 changed files with 28159 additions and 6 deletions
@@ -0,0 +1,88 @@
/*****************************************************************************
M. Thaler, Jan. 2000
Datei read.java
Funktion: Unsigned int Zahl via Bytestream von stdin einlesen.
Es wird zuerst eine Zeile eingelesen und dann konvertiert.
Die eingelesene Zeile darf nur eine Zahl enthalten
(mit optionalen Leerzeichen vor/nach der Zahl).
Returns: Die konvertierte Zahl
oder -1 (PARSE_ERROR) wenn keine Zahl oder zu gross
oder -2 (READ_ERROR) wenn Fehler beim einlesen.
Korrekturen: - Maerz 2002: M. Thaler, H. Fierz, Mar. 2002
liest bis EOL oder EOF, korrekter Rueckgabewert
- Sept 2016: A. Gieriet
Refactored (sprechende Variablen-Namen,
MS Windows Support, 0 Wert erlaubt,
Leerzeichen Support,
"magic" Numbers durch Symbole ersetzt,
maxResult Parameter, etc.)
******************************************************************************/
public class read {
public int getInt(int maxResult)
throws java.io.IOException
{
// 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;
byte[] buffer = new byte[BUFFERSIZE];
int result = 0;
// read line: up to EOL or EOF (i.e. error while reading)
int bytes = 0;
int input = System.in.read();
while ((input != EOL) && (input != EOF)) { // read whole line
if (bytes < BUFFERSIZE) { // only buffer first n characters
buffer[bytes] = (byte)input;
bytes++;
} else {
result = PARSE_ERROR; // exceed buffer size, continue read line
}
input = System.in.read();
}
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;
}
}
@@ -0,0 +1,32 @@
/*****************************************************************************
M. Thaler, Jan. 2000
Datei: rectang.java
Funktion: Bestimmt, ob Dreieck rechtwinklig ist.
Returns: true wenn rechtwinklig, sonst false.
Korrekturen: - Sept 2016, A. Gieriet
Refactored (sprechende Variablen Namen, etc.)
******************************************************************************/
public class rectang {
public boolean Rectangular(int a, int b, int c) {
int aS = a*a;
int bS = b*b;
int cS = c*c;
boolean isRightAngled;
if ((a == 0) && (b == 0) && (c == 0))
isRightAngled = false;
else if ((aS + bS) == cS)
isRightAngled = true;
else if ((aS + cS) == bS)
isRightAngled = true;
else if ((bS + cS) == aS)
isRightAngled = true;
else
isRightAngled = false;
return isRightAngled;
}
}
@@ -0,0 +1,73 @@
/*****************************************************************************
M. Thaler, Jan. 2000
Datei: triangle.java
Funktion: Die drei Seiten eines Dreiecks einlesen und bestimmen ob
das Dreieck rechtwinklig ist.
Returns: Nichts.
Korrekturen: - Maerz 2002, M. Thaler, H. Fierz
Abfrage bei unkorrekter Eingabe wiederholen
- Sept 2016, A. Gieriet
Refactored (sprechende Variablen-Namen,
"magic" Numbers durch Symbole ersetzt, etc.)
******************************************************************************/
class triangle {
public static void main(String[] args)
throws java.io.IOException
{
int READ_ERROR = -2;
int MAX_NUMBER = 1000;
read ReadInt = new read();
rectang Rect = new rectang();
while (true) {
System.out.println("\nDreiecksbestimmung (CTRL-C: Abbruch)\n");
int word = 0;
int a = 0;
int b = 0;
int c = 0;
do {
System.out.print("Seite a: ");
word = ReadInt.getInt(MAX_NUMBER);
}
while ((word < 0) && (word != READ_ERROR));
if (word >= 0)
a = word;
else
break;
do {
System.out.print("Seite b: ");
word = ReadInt.getInt(MAX_NUMBER);
}
while ((word < 0) && (word != READ_ERROR));
if (word >= 0)
b = word;
else
break;
do {
System.out.print("Seite c: ");
word = ReadInt.getInt(MAX_NUMBER);
}
while ((word < 0) && (word != READ_ERROR));
if (word >= 0)
c = word;
else
break;
if (Rect.Rectangular(a, b, c) == true)
System.out.println("-> Dreieck " + a + "-" + b + "-" + c
+ " ist rechtwinklig");
else
System.out.println("-> Dreieck " + a + "-" + b + "-" + c
+ " ist nicht rechtwinklig");
System.out.println("\n");
}
System.out.println("\n\nbye bye\n");
}
}
@@ -0,0 +1,94 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
#include <stdio.h>
#include <stdlib.h>
#include "read.h"
#include "trace.h"
//#define EOF (-1) // end of file
/// end-of-line
#define EOL 10 // end of line
// abnormal return values
/// parse-error
#define PARSE_ERROR (-1)
// ASCII Codes
/// ascii code for space
#define ASCII_SPACE ' '
/// ascii code for 0
#define ASCII_DIGIT_0 '0'
/// ascii code for 9
#define ASCII_DIGIT_9 '9'
// conversion buffer
/// no buffer position sentry
#define NO_POS (-1)
/// buffer size
#define BUFFERSIZE (10)
int getInt(int maxResult)
{
TRACE("getInt(%d)", maxResult);
char buffer[BUFFERSIZE] = { 0 };
int result = 0;
// read line: up to EOL or EOF (i.e. error while reading)
int bytes = 0;
int input = getchar();
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
}
input = getchar();
}
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;
}
@@ -0,0 +1,27 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
#ifndef READ_H
#define READ_H
/// I/O error which leads to aborting.
#define READ_ERROR (-2)
/**
* @brief Reads a line from stdin and tries to parse it as number (with optional leading and trailing spaces).
* @param[in] maxResult gives the limit for the number to be read.
* @returns the succesfully read and parsed number or a negative value if in error (READ_ERROR in case of I/O error).
*/
int getInt(int maxResult);
#endif
@@ -0,0 +1,38 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
#include "rectang.h"
#include "trace.h"
int rectangular(int a, int b, int c)
{
TRACE("rectangular(%d, %d, %d)", a, b, 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;
}
@@ -0,0 +1,26 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
#ifndef RECTANG_H
#define RECTANG_H
/**
* @brief checks if the three lengths give a right-angled triangle.
* @param[in] a 1st side
* @param[in] b 2nd side
* @param[in] c 3rd side
* @returns 1 if right-angled, 0 otherwise
*/
int rectangular(int a, int b, int c);
#endif
@@ -0,0 +1,26 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
#ifndef TRACE_H
#define TRACE_H
#include <stdio.h>
/**
* @brief TRACE macro.
* @param[in] MSG "..." format string literal.
* @param[in] ... optional arguments to the format string.
*/
#define TRACE(MSG, ...) do { (void)fprintf(stderr, "TRACE: " MSG "\n", ##__VA_ARGS__); } while(0)
#endif
@@ -0,0 +1,78 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
#include <stdio.h>
#include <stdlib.h>
#include "read.h"
#include "rectang.h"
#include "trace.h"
/// max side length
#define MAX_NUMBER 1000
/**
* @brief Main entry point.
* @returns Returns EXIT_SUCCESS (=0) on success, EXIT_FAILURE (=1) on failure.
*/
int main(void)
{
TRACE("main()");
while (1) {
(void)printf("\nDreiecksbestimmung (CTRL-C: Abbruch)\n\n");
int word = 0;
int a = 0;
int b = 0;
int c = 0;
do {
(void)printf("Seite a: ");
word = getInt(MAX_NUMBER);
}
while ((word < 0) && (word != READ_ERROR));
if (word >= 0)
a = word;
else
break;
do {
(void)printf("Seite b: ");
word = getInt(MAX_NUMBER);
}
while ((word < 0) && (word != READ_ERROR));
if (word >= 0)
b = word;
else
break;
do {
(void)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))
(void)printf("-> Dreieck %d-%d-%d ist rechtwinklig\n", a, b, c);
else
(void)printf("-> Dreieck %d-%d-%d ist nicht rechtwinklig\n", a, b, c);
(void)printf("\n\n");
}
(void)printf("\n\nbye bye\n\n");
return EXIT_SUCCESS;
}
@@ -0,0 +1,12 @@
a
a
a
3
4444
4444
44444444444444
4
4
5
5
@@ -0,0 +1,12 @@
3
4
6
5
4
4
3
5
5
33
43
55
@@ -0,0 +1,12 @@
3
4
5
5
4
3
3
5
4
33
44
55
@@ -0,0 +1,205 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
#include <stdio.h>
#include <stdlib.h>
#include "CUnit/Basic.h"
#include "test_utils.h"
#ifndef TARGET // must be given by the make file --> see test target
#error missing TARGET define
#endif
/// @brief The name of the STDOUT text file.
#define OUTFILE "stdout.txt"
/// @brief The name of the STDERR text file.
#define ERRFILE "stderr.txt"
/// @brief The stimulus for the right-angled triangles
#define INFILE_RIGHT_ANGLED "stim-right-angled.input"
/// @brief The stimulus for the not right-angled triangles
#define INFILE_NOT_RIGHT_ANGLED "stim-not-right-angled.input"
/// @brief The stimulus for input errors
#define INFILE_ERROR "stim-error.input"
// setup & cleanup
static int setup(void)
{
remove_file_if_exists(OUTFILE);
remove_file_if_exists(ERRFILE);
return 0; // success
}
static int teardown(void)
{
// Do nothing.
// Especially: do not remove result files - they are removed in int setup(void) *before* running a test.
return 0; // success
}
// tests
static void test_right_angled(void)
{
// arrange
const char *out_txt[] = {
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 3-4-5 ist rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 5-4-3 ist rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 3-5-4 ist rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 33-44-55 ist rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: \n",
"\n",
"bye bye\n",
"\n",
};
// act
int exit_code = system(XSTR(TARGET) " 1>" OUTFILE " 2>" ERRFILE " <" INFILE_RIGHT_ANGLED);
// assert
CU_ASSERT_EQUAL(exit_code, 0);
assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt));
}
static void test_not_right_angled(void)
{
// arrange
const char *out_txt[] = {
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 3-4-6 ist nicht rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 5-4-4 ist nicht rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 3-5-5 ist nicht rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite b: Seite c: -> Dreieck 33-43-55 ist nicht rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: \n",
"\n",
"bye bye\n",
"\n",
};
// act
int exit_code = system(XSTR(TARGET) " 1>" OUTFILE " 2>" ERRFILE " <" INFILE_NOT_RIGHT_ANGLED);
// assert
CU_ASSERT_EQUAL(exit_code, 0);
assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt));
}
static void test_trace(void)
{
// arrange
const char *err_txt[] = {
"TRACE: main()\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: rectangular(3, 4, 6)\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: rectangular(5, 4, 4)\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: rectangular(3, 5, 5)\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: getInt(1000)\n",
"TRACE: rectangular(33, 43, 55)\n",
"TRACE: getInt(1000)\n",
};
// act
int exit_code = system(XSTR(TARGET) " 1>" OUTFILE " 2>" ERRFILE " <" INFILE_NOT_RIGHT_ANGLED);
// assert
CU_ASSERT_EQUAL(exit_code, 0);
assert_lines(ERRFILE, err_txt, sizeof(err_txt)/sizeof(*err_txt));
}
static void test_error(void)
{
// arrange
const char *out_txt[] = {
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: Seite a: Seite a: Seite a: Seite a: Seite b: Seite b: Seite b: Seite b: Seite b: Seite c: Seite c: -> Dreieck 3-4-5 ist rechtwinklig\n",
"\n",
"\n",
"\n",
"Dreiecksbestimmung (CTRL-C: Abbruch)\n",
"\n",
"Seite a: \n",
"\n",
"bye bye\n",
"\n",
};
// act
int exit_code = system(XSTR(TARGET) " 1>" OUTFILE " 2>" ERRFILE " <" INFILE_ERROR);
// assert
CU_ASSERT_EQUAL(exit_code, 0);
assert_lines(OUTFILE, out_txt, sizeof(out_txt)/sizeof(*out_txt));
}
/**
* @brief Registers and runs the tests.
*/
int main(void)
{
// setup, run, teardown
TestMainBasic("Triangle", setup, teardown
, test_right_angled
, test_not_right_angled
, test_trace
, test_error
);
}