From 4c84eb9be15d19b93f8fee4500bd8b5459fd0bb3 Mon Sep 17 00:00:00 2001 From: fdai7754 Date: Sat, 3 Feb 2024 18:58:12 +0100 Subject: [PATCH] groesster_gemeinsammer_teiler funktion minirechner inkl. unittest --- src/minirechner.c | 21 +++++++++++++++++++++ src/minirechner.h | 1 + test/test_minirechner.c | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/src/minirechner.c b/src/minirechner.c index cbc13ef..c6f159d 100644 --- a/src/minirechner.c +++ b/src/minirechner.c @@ -1,4 +1,5 @@ #include +#include #include "minirechner.h" //addieren @@ -30,4 +31,24 @@ float dividieren(float a, float b) { //rest float rest(float a, float b) { return fmod(a, b); +} + +// groesster gemeinsammer Teiler +float groesster_gemeinsammer_teiler(float a, float b) { + float gr, kl; + float rest = 1; + if (a > b) { + gr = a; + kl = b; + } + else { + gr = b; + kl = a; + } + while (rest != 0) { + rest = fmod(gr, kl); + gr = kl; + kl = rest; + } + return gr; } \ No newline at end of file diff --git a/src/minirechner.h b/src/minirechner.h index a2c989f..b3717a8 100644 --- a/src/minirechner.h +++ b/src/minirechner.h @@ -6,5 +6,6 @@ float subtrahieren(float a, float b); float multiplizieren(float a, float b); float dividieren(float a, float b); float rest(float a, float b); +float groesster_gemeinsammer_teiler(float a, float b); #endif // MINIRECHNER_H diff --git a/test/test_minirechner.c b/test/test_minirechner.c index 10e0ebf..b7301f7 100644 --- a/test/test_minirechner.c +++ b/test/test_minirechner.c @@ -113,4 +113,11 @@ void test_minitaschenrechner_1_rest_7(void) TEST_ASSERT_EQUAL(0, result); } +//groesster gemeinsammer Teiler +void test_minitaschenrechner_120_groesster_gemeinsammer_teiler_30(void) +{ + float result = groesster_gemeinsammer_teiler(120, 30); + TEST_ASSERT_EQUAL(30, result); +} + #endif // TEST