Browse Source

groesster_gemeinsammer_teiler funktion minirechner inkl. unittest

remotes/origin/minirechnerdev
fdai7754 11 months ago
parent
commit
4c84eb9be1
  1. 21
      src/minirechner.c
  2. 1
      src/minirechner.h
  3. 7
      test/test_minirechner.c

21
src/minirechner.c

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <math.h>
#include "minirechner.h" #include "minirechner.h"
//addieren //addieren
@ -31,3 +32,23 @@ float dividieren(float a, float b) {
float rest(float a, float b) { float rest(float a, float b) {
return fmod(a, 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;
}

1
src/minirechner.h

@ -6,5 +6,6 @@ float subtrahieren(float a, float b);
float multiplizieren(float a, float b); float multiplizieren(float a, float b);
float dividieren(float a, float b); float dividieren(float a, float b);
float rest(float a, float b); float rest(float a, float b);
float groesster_gemeinsammer_teiler(float a, float b);
#endif // MINIRECHNER_H #endif // MINIRECHNER_H

7
test/test_minirechner.c

@ -113,4 +113,11 @@ void test_minitaschenrechner_1_rest_7(void)
TEST_ASSERT_EQUAL(0, result); 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 #endif // TEST
Loading…
Cancel
Save