From e10c5365159aab8b38d4cf4099ff68f9c31e7330 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 00:18:01 +0100 Subject: [PATCH 01/19] added fakultaet.c --- src/fakultaet/fakultaet.c | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/fakultaet/fakultaet.c diff --git a/src/fakultaet/fakultaet.c b/src/fakultaet/fakultaet.c new file mode 100644 index 0000000..63a600f --- /dev/null +++ b/src/fakultaet/fakultaet.c @@ -0,0 +1,6 @@ +#include + +int fakultaet(int n) { + + +} \ No newline at end of file From 55065b6b998339d795910a84d6f58b3e1c081c0b Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 00:59:27 +0100 Subject: [PATCH 02/19] added fakultaet.c function --- src/fakultaet/fakultaet.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/fakultaet/fakultaet.c b/src/fakultaet/fakultaet.c index 63a600f..93273f6 100644 --- a/src/fakultaet/fakultaet.c +++ b/src/fakultaet/fakultaet.c @@ -1,6 +1,13 @@ #include int fakultaet(int n) { + if (n == 0 || n == 1) + return 1; + int result = 1; + for (int i = 2; i <= n; i++) { + result *= i; + } + return result; } \ No newline at end of file From 0d50a1a4e4b8e568c4146f4e76d12154f213c250 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 01:22:08 +0100 Subject: [PATCH 03/19] added fakultaet.h --- src/fakultaet/fakultaet.h | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/fakultaet/fakultaet.h diff --git a/src/fakultaet/fakultaet.h b/src/fakultaet/fakultaet.h new file mode 100644 index 0000000..c2d4a62 --- /dev/null +++ b/src/fakultaet/fakultaet.h @@ -0,0 +1,6 @@ +#ifndef FAKULTAET_H +#define FAKULTAET_H + +int fakultaet(int n); + +#endif \ No newline at end of file From 69ed4f665158fb376c62e3544814d807561744e0 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 01:31:07 +0100 Subject: [PATCH 04/19] added termination condition in function --- src/fakultaet/fakultaet.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/fakultaet/fakultaet.c b/src/fakultaet/fakultaet.c index 93273f6..d10c0a0 100644 --- a/src/fakultaet/fakultaet.c +++ b/src/fakultaet/fakultaet.c @@ -1,6 +1,11 @@ #include int fakultaet(int n) { + if (n < 0) { + printf("Fakultaet kann nicht für negative Zahlen berechnet werden!\n"); + return -1; + } + if (n == 0 || n == 1) return 1; From 70e496ccfacdf567ffb4e5b88bfd77b9a87b3583 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 01:38:30 +0100 Subject: [PATCH 05/19] deleted terminal condition --- src/fakultaet/fakultaet.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/fakultaet/fakultaet.c b/src/fakultaet/fakultaet.c index d10c0a0..93273f6 100644 --- a/src/fakultaet/fakultaet.c +++ b/src/fakultaet/fakultaet.c @@ -1,11 +1,6 @@ #include int fakultaet(int n) { - if (n < 0) { - printf("Fakultaet kann nicht für negative Zahlen berechnet werden!\n"); - return -1; - } - if (n == 0 || n == 1) return 1; From a613d8c63224cc96cba430d17beff1a5fcc57482 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 01:42:03 +0100 Subject: [PATCH 06/19] added main.c --- src/fakultaet/main.c | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/fakultaet/main.c diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c new file mode 100644 index 0000000..09c56a3 --- /dev/null +++ b/src/fakultaet/main.c @@ -0,0 +1,5 @@ +int main() { + + + return 0; +} \ No newline at end of file From 0fffae7de8528047539e308cd08a95adba2c3bfa Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 01:54:01 +0100 Subject: [PATCH 07/19] included header files --- src/fakultaet/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c index 09c56a3..eae0702 100644 --- a/src/fakultaet/main.c +++ b/src/fakultaet/main.c @@ -1,3 +1,7 @@ +#include "fakultaet.h" +#include "../userinput.h" +#include + int main() { From 353a06181e5515f590d56df86e0592fd792dc787 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 02:18:44 +0100 Subject: [PATCH 08/19] added function to get user input --- src/fakultaet/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c index eae0702..f11f8d4 100644 --- a/src/fakultaet/main.c +++ b/src/fakultaet/main.c @@ -3,7 +3,10 @@ #include int main() { + int minInput = 0; + printf("Enter a number to calculate its factorial: \n"); + int number = usergetd("Enter an even number: "), minInput, NULL); return 0; } \ No newline at end of file From 2717c18f56f9405a1f21ed95584eb4f2b988a603 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 03:03:23 +0100 Subject: [PATCH 09/19] print the factorial --- src/fakultaet/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c index f11f8d4..69c4628 100644 --- a/src/fakultaet/main.c +++ b/src/fakultaet/main.c @@ -8,5 +8,9 @@ int main() { printf("Enter a number to calculate its factorial: \n"); int number = usergetd("Enter an even number: "), minInput, NULL); + int result = fakultaet(num); + + printf("The factorial of %d is: %d\n"); + return 0; } \ No newline at end of file From 0b4e7b9f6cc4a59f6242ff3d4ed7358678f829a8 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 03:51:53 +0100 Subject: [PATCH 10/19] added test_fakultaet.c --- test/fakultaet/test_fakultaet.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/fakultaet/test_fakultaet.c diff --git a/test/fakultaet/test_fakultaet.c b/test/fakultaet/test_fakultaet.c new file mode 100644 index 0000000..e69de29 From b0ce1fe90ce3524a27404131295c9d066d567ecb Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 04:49:22 +0100 Subject: [PATCH 11/19] added test for input 0 --- test/fakultaet/test_fakultaet.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/fakultaet/test_fakultaet.c b/test/fakultaet/test_fakultaet.c index e69de29..49b1d16 100644 --- a/test/fakultaet/test_fakultaet.c +++ b/test/fakultaet/test_fakultaet.c @@ -0,0 +1,9 @@ +#include "unity.h" +#include "fakultaet.h" + +void setUp(){} +void tearDown(){} + +void test_fakultaetOf0() { + TEST_ASSERT_EQUAL_INT(1, fakultaet(0)); +} \ No newline at end of file From 5ba6ed51d6e029f51c9c4a8d5946f1934076f58b Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 04:52:28 +0100 Subject: [PATCH 12/19] added test for input 1 --- test/fakultaet/test_fakultaet.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/fakultaet/test_fakultaet.c b/test/fakultaet/test_fakultaet.c index 49b1d16..bbec4a5 100644 --- a/test/fakultaet/test_fakultaet.c +++ b/test/fakultaet/test_fakultaet.c @@ -6,4 +6,8 @@ void tearDown(){} void test_fakultaetOf0() { TEST_ASSERT_EQUAL_INT(1, fakultaet(0)); +} + +void test_fakultaetOf1() { + TEST_ASSERT_EQUAL_INT(1, fakultaet(1)); } \ No newline at end of file From 8151f931af0035fe55ef5d6fed5c359f3232f6ac Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 04:56:54 +0100 Subject: [PATCH 13/19] added test for input 5 --- test/fakultaet/test_fakultaet.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/fakultaet/test_fakultaet.c b/test/fakultaet/test_fakultaet.c index bbec4a5..03eb75c 100644 --- a/test/fakultaet/test_fakultaet.c +++ b/test/fakultaet/test_fakultaet.c @@ -10,4 +10,8 @@ void test_fakultaetOf0() { void test_fakultaetOf1() { TEST_ASSERT_EQUAL_INT(1, fakultaet(1)); +} + +void test_fakultaetOf5() { + TEST_ASSERT_EQUAL_INT(120, fakultaet(5)); } \ No newline at end of file From b87f8bb2c0214b2faad852c911bde2119e2f5be0 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 05:09:59 +0100 Subject: [PATCH 14/19] refactoring: added test comments --- test/fakultaet/test_fakultaet.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/fakultaet/test_fakultaet.c b/test/fakultaet/test_fakultaet.c index 03eb75c..4414b23 100644 --- a/test/fakultaet/test_fakultaet.c +++ b/test/fakultaet/test_fakultaet.c @@ -4,14 +4,17 @@ void setUp(){} void tearDown(){} +// Test for the input zero void test_fakultaetOf0() { TEST_ASSERT_EQUAL_INT(1, fakultaet(0)); } +// Test for the input one void test_fakultaetOf1() { TEST_ASSERT_EQUAL_INT(1, fakultaet(1)); } +// Test for the input five void test_fakultaetOf5() { TEST_ASSERT_EQUAL_INT(120, fakultaet(5)); } \ No newline at end of file From 33def303b403c06e98917302152d42aba2a4ee83 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 05:39:38 +0100 Subject: [PATCH 15/19] refactoring: added function comments --- src/fakultaet/fakultaet.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fakultaet/fakultaet.c b/src/fakultaet/fakultaet.c index 93273f6..b05b947 100644 --- a/src/fakultaet/fakultaet.c +++ b/src/fakultaet/fakultaet.c @@ -1,9 +1,11 @@ #include int fakultaet(int n) { + // Checking if the input is either zero or one if (n == 0 || n == 1) return 1; + // The result multiplies with i and i increases every turn until the loop ends int result = 1; for (int i = 2; i <= n; i++) { result *= i; From b658883ba0d58096d88db53708b34084ef44d408 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 05:52:01 +0100 Subject: [PATCH 16/19] corrected main --- src/fakultaet/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c index 69c4628..112c8a7 100644 --- a/src/fakultaet/main.c +++ b/src/fakultaet/main.c @@ -8,9 +8,9 @@ int main() { printf("Enter a number to calculate its factorial: \n"); int number = usergetd("Enter an even number: "), minInput, NULL); - int result = fakultaet(num); + int result = fakultaet(number); - printf("The factorial of %d is: %d\n"); + printf("The factorial of %d is: %d\n", number, result); return 0; } \ No newline at end of file From 670431de6dac15df2c97b46d96df6e5a589e23d6 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 06:12:07 +0100 Subject: [PATCH 17/19] refactoring: added comments to main --- src/fakultaet/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c index 112c8a7..5de7643 100644 --- a/src/fakultaet/main.c +++ b/src/fakultaet/main.c @@ -3,12 +3,14 @@ #include int main() { + // minInput is set to 0 so there won't be any negative numbers int minInput = 0; printf("Enter a number to calculate its factorial: \n"); int number = usergetd("Enter an even number: "), minInput, NULL); int result = fakultaet(number); + // result now has the factorial of the user input printf("The factorial of %d is: %d\n", number, result); From 4ce382161958281194c3497f2b6eac5465743a6d Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 09:09:55 +0100 Subject: [PATCH 18/19] refactoring: changed print and maxInput --- src/fakultaet/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c index 5de7643..4132634 100644 --- a/src/fakultaet/main.c +++ b/src/fakultaet/main.c @@ -5,9 +5,9 @@ int main() { // minInput is set to 0 so there won't be any negative numbers int minInput = 0; + int maxInput = 12; - printf("Enter a number to calculate its factorial: \n"); - int number = usergetd("Enter an even number: "), minInput, NULL); + int number = usergetd("Enter a positive number below 13: "), minInput, maxInput); int result = fakultaet(number); // result now has the factorial of the user input From 55b7448ec19f592afa57920c9b9035e17f514531 Mon Sep 17 00:00:00 2001 From: Hendrik Sitzmann Date: Thu, 8 Feb 2024 11:01:08 +0100 Subject: [PATCH 19/19] refactoring: added comment --- src/fakultaet/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fakultaet/main.c b/src/fakultaet/main.c index 4132634..d0ca0fe 100644 --- a/src/fakultaet/main.c +++ b/src/fakultaet/main.c @@ -5,6 +5,7 @@ int main() { // minInput is set to 0 so there won't be any negative numbers int minInput = 0; + // maxInput is set to 12 because 13 would be too big for int int maxInput = 12; int number = usergetd("Enter a positive number below 13: "), minInput, maxInput);