diff --git a/src/c/funktionen.c b/src/c/funktionen.c index 83ed4ba..7273a45 100644 --- a/src/c/funktionen.c +++ b/src/c/funktionen.c @@ -4,6 +4,7 @@ #include #include "funktionen.h" + void welcome() { char x[15]; printf("Hallo! Wie heisst du?\n"); @@ -184,3 +185,109 @@ float cosineDegrees(float x) { float tangentDegrees(float x) { return tan(degreesToRadians(x)); } + +// Function to calculate the square of a number +float square(float x) { +return x * x; +} + +float squareRoot(float x) { +if (x >= 0) { +return sqrt(x); +} else { +printf("Error: Invalid input for square root!\n"); +return 0; +} +} + +// Function to calculate the cube of a number +float cube(float x) { +return x * x * x; +} + +// Function to calculate the cube root of a number +float cubeRoot(float x) { +return cbrt(x); +} + +// Function to calculate the absolute value of a number +float absolute(float x) { +return fabs(x); +} + +// Function to calculate the logarithm (base 10) of a number +float logarithm(float x) { +if (x > 0) { +return log10(x); +} else { +printf("Error: Invalid input for logarithm!\n"); +return 0; +} +} + +// Function to calculate the natural logarithm (base e) of a number +float naturalLogarithm(float x) { +if (x > 0) { +return log(x); +} else { +printf("Error: Invalid input for natural logarithm!\n"); +return 0; +} +} + +// Function to calculate the exponentiation of a number to the power of another number +float power(float x, float y) { +return pow(x, y); +} + +// Function to calculate the factorial of a number +int factorial(int x) { +if (x >= 0) { +int result = 1; +for (int i = 1; i <= x; i++) { +result *= i; +} +return result; +} else { +printf("Error: Invalid input for factorial!\n"); +return 0; +} +} + +// Function to calculate the floor value of a number +float floorValue(float x) { +return floor(x); +} + +// Function to calculate the ceiling value of a number +float ceilingValue(float x) { +return ceil(x); +} + +// Function to calculate the absolute difference between two numbers +float absoluteDifference(float x, float y) { + return fabs(x - y); +} + +// Function to calculate the maximum of two numbers +float maximum(float x, float y) { + return fmax(x, y); +} + +// Function to calculate the minimum of two numbers +float minimum(float x, float y) { + return fmin(x, y); +} + +// Function to calculate the average of two numbers +float average(float x, float y) { + return (x + y) / 2; +} + +// Function to calculate the remainder of division between two numbers +float remainderValue(float x, float y) { + return fmod(x, y); +} + + + diff --git a/src/c/funktionen.h b/src/c/funktionen.h index 21ce901..05c1579 100644 --- a/src/c/funktionen.h +++ b/src/c/funktionen.h @@ -1,6 +1,7 @@ #ifndef FUNKTIONEN_H #define FUNKTIONEN_H + void welcome(); // Die welcome() Funktion fragt den Namen des Benutzers ab und gibt einen entsprechenden Text aus. @@ -79,4 +80,54 @@ float cosineDegrees(float x); // Function to calculate the tangent of an angle in degrees float tangentDegrees(float x); + +// Function to calculate the square of a number +float square(float x); + +// Function to calculate the square root of a number +float squareRoot(float x); + + +// Function to calculate the cube of a number +float cube(float x); + +// Function to calculate the cube root of a number +float cubeRoot(float x); + +// Function to calculate the absolute value of a number +float absolute(float x); + +// Function to calculate the logarithm (base 10) of a number +float logarithm(float x); + +// Function to calculate the natural logarithm (base e) of a number +float naturalLogarithm(float x); + +// Function to calculate the exponentiation of a number to the power of another number +float power(float x, float y); + +// Function to calculate the factorial of a number +int factorial(int x); + +// Function to calculate the floor value of a number +float floorValue(float x); + +// Function to calculate the ceiling value of a number +float ceilingValue(float x); + +// Function to calculate the absolute difference between two numbers +float absoluteDifference(float x, float y); + +// Function to calculate the maximum of two numbers +float maximum(float x, float y); + +// Function to calculate the minimum of two numbers +float minimum(float x, float y); + +// Function to calculate the average of two numbers +float average(float x, float y); + +// Function to calculate the remainder of division between two numbers +float remainderValue(float x, float y); + #endif \ No newline at end of file diff --git a/src/test/test_funktionen.c b/src/test/test_funktionen.c index 7161783..0a65316 100644 --- a/src/test/test_funktionen.c +++ b/src/test/test_funktionen.c @@ -11,12 +11,95 @@ void tearDown(void) { } + void test_1000_plus_1(void) { /* arrange */ int actual; int expected = 1000; +void test_square(void) { + float result = square(2.5); + TEST_ASSERT_EQUAL_FLOAT(6.25, result); +} + +void test_squareRoot(void) { + float result = squareRoot(9.0); + TEST_ASSERT_EQUAL_FLOAT(3.0, result); +} + +void test_cube(void) { + float result = cube(2.0); + TEST_ASSERT_EQUAL_FLOAT(8.0, result); +} + +void test_cubeRoot(void) { + float result = cubeRoot(27.0); + TEST_ASSERT_EQUAL_FLOAT(3.0, result); +} + +void test_absolute(void) { + float result = absolute(-5.0); + TEST_ASSERT_EQUAL_FLOAT(5.0, result); +} + +void test_logarithm(void) { + float result = logarithm(100.0); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 2.0, result); +} + +void test_naturalLogarithm(void) { + float result = naturalLogarithm(100.0); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 4.60517, result); +} + +void test_power(void) { + float result = power(2.0, 3.0); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 8.0, result); +} + +void test_factorial(void) { + int result = factorial(5); + TEST_ASSERT_EQUAL_INT(120, result); +} + + +void test_floorValue(void) { + float result = floorValue(5.7); + TEST_ASSERT_EQUAL_FLOAT(5.0, result); +} + +void test_ceilingValue(void) { + float result = ceilingValue(5.2); + TEST_ASSERT_EQUAL_FLOAT(6.0, result); +} + +void test_absoluteDifference(void) { + float result = absoluteDifference(8.0, 4.5); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 3.5, result); +} + +void test_maximum(void) { + float result = maximum(5.0, 9.0); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 9.0, result); +} + +void test_minimum(void) { + float result = minimum(5.0, 9.0); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 5.0, result); +} + +void test_average(void) { + float result = average(5.0, 9.0); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 7.0, result); +} + +void test_remainderValue(void) { + float result = remainderValue(10.5, 3.0); + TEST_ASSERT_FLOAT_WITHIN(0.000001, 1.5, result); +} + + /* act */ actual = addThreeNumbers(500, 249, 251);