From 8e83e65b2c0d227ee592e1b6dcc4428b4308dfe0 Mon Sep 17 00:00:00 2001 From: Enrico Schellenberger Date: Tue, 6 Feb 2024 16:29:15 +0100 Subject: [PATCH] added logic to ConClock and Unittest for the function --- src/main/c/ConvertMode.c | 58 ++---- src/main/c/main_taschenrechner.c | 319 ++++++++++++++++++++++++++++++- src/main/c/taschenrechner.h | 3 + src/test/c/test_taschenrechner.c | 7 +- 4 files changed, 339 insertions(+), 48 deletions(-) diff --git a/src/main/c/ConvertMode.c b/src/main/c/ConvertMode.c index acf9158..5ce125c 100644 --- a/src/main/c/ConvertMode.c +++ b/src/main/c/ConvertMode.c @@ -16,7 +16,7 @@ char Fluid[] = { 'ml', 'l' , 'gallon'}; char Temp[] = { 'celsius', 'fahrenheit' }; char Speed[] = { 'km/h','mp/h' }; char Data[] = { 'B', 'KB', 'MB', 'GB', 'TB', 'PT' }; -char Time[] = { 'ms', 's', 'min', 'h', 'd', 'w', 'mon', 'y' }; +char Time[] = { 'ms', 's', 'min', 'h', 'd', 'w', 'mon', 'y'}; char currency[] = { 'E', 'D', 'R' }; @@ -189,66 +189,32 @@ double getValue(int choice) { printf("\nThe convertet result is %dlf %dł", result, Distance[endingUnit]); break; - case 13: - printf("\nEnter what the Unit is starting with (0 (24h), 1 (12h)): "); + case 13://char Time[] = { 'ms', 's', 'min', 'h', 'd', 'w', 'mon', 'y' }; + printf("\nEnter what the Unit is starting with (0 ms, 1 s, 2 min, 3 h, 4 d, 5 w, 6 mon, 7 y): "); scanf("%d", &startingUnit); - printf("\nEnter what the value should it be changed to (0 (24h), 1 (12h)): "); + printf("\nEnter what the value should it be changed to (0 ms, 1 s, 2 min, 3 h, 4 d, 5 w, 6 mon, 7 y): "); scanf("%d", &endingUnit); - result = ConArea(value, startingUnit, endingUnit); + result = ConTime(value, startingUnit, endingUnit); printf("\nThe convertet result is %dlf %d", result, Distance[endingUnit]); break; - } - } -} -/* -double ConSpeed(double speed, int startingUnit, int endingUnit) { - switch (startingUnit) - { - case 0: //kmh to x - switch (endingUnit) - { - case 0: //kmh to kmh - return speed; - break; - - case 1: //kmh to mph - return speed * 0.621371; - break; - default: - break; - } + case 14: + printf("\nEnter what the Unit is starting with (0 (24h), 1 (12h)): "); + scanf("%d", &startingUnit); - case 1: //mph to x - switch (endingUnit) - { - case 0: //mph to kmh - return speed * 1.60934; - break; + printf("\nEnter what the value should it be changed to (0 (24h), 1 (12h)): "); + scanf("%d", &endingUnit); - case 1: //mph to mph - return speed; - break; + result = ConClock(value, startingUnit, endingUnit); - default: + printf("\nThe convertet result is %dlf %d", result, Distance[endingUnit]); break; } - - default: - break; } } -*/ - - - -double ConClock() { - -} - void unitConverterMode() { diff --git a/src/main/c/main_taschenrechner.c b/src/main/c/main_taschenrechner.c index cbd4e1b..414021b 100644 --- a/src/main/c/main_taschenrechner.c +++ b/src/main/c/main_taschenrechner.c @@ -948,7 +948,7 @@ double ConVolume(double volum, int startingUnit, int endingUnit) { break; } } -double ConTime(double time, int startingUnit, int endingUnit) { +double ConClock(double time, int startingUnit, int endingUnit) { switch (startingUnit) { case 0: //24 to x @@ -992,6 +992,323 @@ double ConTime(double time, int startingUnit, int endingUnit) { } } +double ConTime(double time, int startingUnit, int endingUnit) { + switch (startingUnit) + {//'ms', 's', 'min', 'h', 'd', 'w', 'mon', 'y' + case 0: //ms to x + switch (endingUnit) + { + case 0: //ms + return time; + break; + + case 1: //s + return time / 1000; + break; + + case 2: //min + return time / (1000 * 60); + break; + + case 3: //h + return time / (1000 * 60 * 60); + break; + + case 4: //d + return time / (1000 * 60 * 60 * 24); + break; + + case 5: //w + return time / (1000 * 60 * 60 * 24 * 7); + break; + + case 6: //mon + return time / (1000 * 60 * 60 * 24 * 7 * 30); + break; + + case 7: //y + return time / (1000 * 60 * 60 * 24 * 7 * 30 * 12); + break; + + default: + break; + } + + case 1: //s to x + switch (endingUnit) + { + case 0: //ms + return time * 1000; + break; + + case 1: //s + return time; + break; + + case 2: //min + return time / 60; + break; + + case 3: //h + return time / (60 * 60); + break; + + case 4: //d + return time / (60 * 60 * 24); + break; + + case 5: //w + return time / (60 * 60 * 24 * 7); + break; + + case 6: //mon + return time / (60 * 60 * 24 * 7 * 30); + break; + + case 7: //y + return time / (60 * 60 * 24 * 7 * 30 * 12); + break; + + default: + break; + } + + case 2: //min to x + switch (endingUnit) + { + case 0: //ms + return time * 1000; + break; + + case 1: //s + return time * 1000 * 60; + break; + + case 2: //min + return time; + break; + + case 3: //h + return time / 60; + break; + + case 4: //d + return time / (60 * 24); + break; + + case 5: //w + return time / (60 * 24 * 7); + break; + + case 6: //mon + return time / (60 * 24 * 7 * 4); + break; + + case 7: //y + return time / 60 * 24 * 7 * 4 * 12); + break; + + default: + break; + } + + case 3: //h to x + switch (endingUnit) + { + case 0: //ms + return time * 1000 * 60 * 60; + break; + + case 1: //s + return time * 60 * 60; + break; + + case 2: //min + return time * 60; + break; + + case 3: //h + return time; + break; + + case 4: //d + return time / 24; + break; + + case 5: //w + return time / (24 * 7); + break; + + case 6: //mon + return time / (24 * 7 * 4); + break; + + case 7: //y + return time / (24 * 7 * 4 * 12); + break; + + default: + break; + } + + case 4: //d to x + switch (endingUnit) + { + case 0: //ms + return time * 1000 * 60 * 60 * 24; + break; + + case 1: //s + return time * 60 * 60 * 24; + break; + + case 2: //min + return time * 60 * 24; + break; + + case 3: //h + return time * 24; + break; + + case 4: //d + return time; + break; + + case 5: //w + return time / 7; + break; + + case 6: //mon + return time / (7 * 4); + break; + + case 7: //y + return time / (7 * 4 * 12); + break; + + default: + break; + } + + case 5: //w to x + switch (endingUnit) + { + case 0: //ms + return time * 1000 * 60 * 60 * 24 * 7; + break; + + case 1: //s + return time * 60 * 60 * 24 * 7; + break; + + case 2: //min + return time * 60 * 24 * 7; + break; + + case 3: //h + return time * 24 * 7; + break; + + case 4: //d + return time * 7; + break; + + case 5: //w + return time; + break; + + case 6: //mon + return time / 4; + break; + + case 7: //y + return time / (4 * 12); + break; + + default: + break; + } + + case 6: //mon to x + switch (endingUnit) + { + case 0: //ms + return time * 1000 * 60 * 60 * 24 * 7 * 4; + break; + + case 1: //s + return time * 60 * 60 * 24 * 7 * 4; + break; + + case 2: //min + return time * 60 * 24 * 7 * 4; + break; + + case 3: //h + return time * 24 * 7 * 4; + break; + + case 4: //d + return time * 7 * 4; + break; + + case 5: //w + return time * 4; + break; + + case 6: //mon + return time; + break; + + case 7: //y + return time / 12; + break; + + default: + break; + } + + case 7: //y to x + switch (endingUnit) + { + case 0: //ms + return time * 1000 * 60 * 60 * 24 * 7 * 4 * 12; + break; + + case 1: //s + return time * 60 * 60 * 24 * 7 * 4 * 12; + break; + + case 2: //min + return time * 60 * 24 * 7 * 4 * 12; + break; + + case 3: //h + return time * 24 * 7 * 4 * 12; + break; + + case 4: //d + return time * 7 * 4 * 12; + break; + + case 5: //w + return time * 4 * 12; + break; + + case 6: //mon + return time * 12); + break; + + case 7: //y + return time; + break; + //(1000 * 60 * 60 * 24 * 7 * 4 * 12) + default: + break; + } + } +} + //.. // graphMode diff --git a/src/main/c/taschenrechner.h b/src/main/c/taschenrechner.h index f5a8302..78ef6a1 100644 --- a/src/main/c/taschenrechner.h +++ b/src/main/c/taschenrechner.h @@ -81,6 +81,9 @@ double ConArea(double area, int startingUnit, int endingUnit); double ConVolume(double volum, int startingUnit, int endingUnit); +double ConClock(double time, int startingUnit, int endingUnit); + double ConTime(double time, int startingUnit, int endingUnit); + #endif // TASCHENRECHNER_H diff --git a/src/test/c/test_taschenrechner.c b/src/test/c/test_taschenrechner.c index 60b744d..cf6c739 100644 --- a/src/test/c/test_taschenrechner.c +++ b/src/test/c/test_taschenrechner.c @@ -98,11 +98,16 @@ void test_ConVolume(void) {//5 mm to cm TEST_ASSERT_EQUAL(0.005), result); } -void test_ConTime(void) {//5:00 to 5AM +void test_ConClock(void) {//5:00 to 5AM double result = ConData(5, 0, 1); TEST_ASSERT_EQUAL(5), result); } +void test_ConTime(void) {//5 ms to s + double result = ConData(5, 0, 1); + TEST_ASSERT_EQUAL(0.005), result); +} + #endif // TEST