This commit is contained in:
stsh
2022-02-17 11:45:11 +01:00
parent 674695e82a
commit 62f8e2ce05
212 changed files with 11436 additions and 0 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,16 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
// begin students to add code for task 4.1
// end students to add code
@@ -0,0 +1,16 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
// begin students to add code for task 4.1
// end students to add code
@@ -0,0 +1,16 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
// begin students to add code for task 4.1
// end students to add code
@@ -0,0 +1,16 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
// begin students to add code for task 4.1
// end students to add code
@@ -0,0 +1,16 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ Institute of Embedded Systems -
* -- | | | '_ \| __| \___ \ Zuercher Hochschule Winterthur -
* -- _| |_| | | | |____ ____) | (University of Applied Sciences) -
* -- |_____|_| |_|______|_____/ 8401 Winterthur, Switzerland -
* ----------------------------------------------------------------------------
*/
/**
* @file
* @brief Lab implementation
*/
// begin students to add code for task 4.2
// end students to add code
@@ -0,0 +1,34 @@
/* ----------------------------------------------------------------------------
* -- _____ ______ _____ -
* -- |_ _| | ____|/ ____| -
* -- | | _ __ | |__ | (___ 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)
{
// begin students to add code for task 4.1
// end students to add code
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
);
}