From 043e820946ce8dca4334575315af111c87f75f53 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 17:35:27 +0100 Subject: [PATCH 01/41] add detection if formula parse is finished --- src/inputHandling.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index efa40c9..e579f27 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -7,7 +7,7 @@ char a[STRINGL]; void deleteWhitespace(); op readFunction(char* data, int length); -void getnumbers(char* data, int length, calc_op* structure_ref); +char* getnumbers(char* data, int length, calc_op* structure_ref); void printstruct(calc_op* formula); @@ -16,10 +16,16 @@ void input() { fgets(a, STRINGL, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace deleteWhitespace(); calc_op temp; + char data[STRINGL]; + memcpy(data,a,STRINGL); temp.formel = a; - temp.funktionstyp = readFunction(a, 10); - getnumbers(a,STRINGL, &temp); - printstruct(&temp); + temp.funktionstyp = readFunction(data, 10); + if (getnumbers(data,STRINGL, &temp) == NULL){ + printstruct(&temp); + } else { + printf("Formular %s not supported", temp.formel); + } + } //Leerzeichen löschen @@ -48,8 +54,9 @@ op readFunction(char* data, int length){ } //Zahlen auslesen (+) -void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct - char tmp[length]; +char* getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct + // char tmp[length]; + char* splitPnt; int i = 0; char delimiter; switch (structure_ref->funktionstyp) { @@ -65,16 +72,22 @@ void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: s case opMult: delimiter = '*'; break; - default: return; + default: return NULL; } - memcpy(tmp, data, length); //string kopiert - char *token = strtok(tmp, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt + // memcpy(tmp, data, length); //string kopiert + char *token = strtok(data, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt while (token != NULL) { - structure_ref->array[i]=atof(token); // String zu double konvertiert + structure_ref->array[i] = atof(token); // String zu double konvertiert i++; + splitPnt = token; token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } structure_ref->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert + if (readFunction(splitPnt, strlen(splitPnt)!=opNotSupported)){ + return splitPnt; + } else { + return NULL; + } } void printstruct(calc_op* formula){ From 0dc484092b84ca98b66bdcc7e017d40d2cb8d049 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 17:45:48 +0100 Subject: [PATCH 02/41] fix formular parse detection bug --- src/inputHandling.c | 10 +++++----- src/inputHandling.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index e579f27..f2edfc1 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -25,13 +25,12 @@ void input() { } else { printf("Formular %s not supported", temp.formel); } - } //Leerzeichen löschen void deleteWhitespace(){ for(int i=0; iarraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert - if (readFunction(splitPnt, strlen(splitPnt)!=opNotSupported)){ - return splitPnt; - } else { + if (readFunction(splitPnt, strlen(splitPnt)+1)!=opNotSupported){ return NULL; + } else { + return splitPnt; } } diff --git a/src/inputHandling.h b/src/inputHandling.h index 676b08d..71bd8f1 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -2,7 +2,7 @@ #define INPUTHANDLING_H typedef enum{ - opAdd, opSub, opDiv, opMult, opExp, opLog, opNotSupported + opAdd, opSub, opDiv, opMult, opExp, opLog, opEmpty, opNotSupported }op; typedef struct { From f9559e41523cd573f98f4712a7ab4468bdfc411c Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 17:56:59 +0100 Subject: [PATCH 03/41] fix formular parse detection bug again --- src/inputHandling.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index f2edfc1..d4f4446 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -83,15 +83,16 @@ char* getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } structure_ref->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert - if (readFunction(splitPnt, strlen(splitPnt)+1)!=opNotSupported){ - return NULL; - } else { + op type = readFunction(splitPnt, strlen(splitPnt)+1); + if (type != opNotSupported && type != opEmpty){ return splitPnt; + } else { + return NULL; } } void printstruct(calc_op* formula){ - printf("Berechnung: %s", formula->formel); + printf("Berechnung: %s\n", formula->formel); switch (formula->funktionstyp) { case opAdd: printf("Rechenoperation: Addition\n"); break; From 5742a7079b9dabe93b8cd2bdfd4ba498065b11fb Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 18:08:47 +0100 Subject: [PATCH 04/41] change input mask to main --- src/inputHandling.c | 23 ++++++++++------------- src/inputHandling.h | 4 ++-- src/main.c | 10 +++++++++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index d4f4446..8479485 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -2,25 +2,22 @@ #include #include #include -#define STRINGL 200 -char a[STRINGL]; -void deleteWhitespace(); + +char data[1000]; +void deleteWhitespace(char* a, int length); op readFunction(char* data, int length); char* getnumbers(char* data, int length, calc_op* structure_ref); void printstruct(calc_op* formula); -void input() { - printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); - fgets(a, STRINGL, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace - deleteWhitespace(); +void input(char* a, int length) { + deleteWhitespace(a, length); calc_op temp; - char data[STRINGL]; - memcpy(data,a,STRINGL); + memcpy(data,a,length); temp.formel = a; temp.funktionstyp = readFunction(data, 10); - if (getnumbers(data,STRINGL, &temp) == NULL){ + if (getnumbers(data,length, &temp) == NULL){ printstruct(&temp); } else { printf("Formular %s not supported", temp.formel); @@ -28,10 +25,10 @@ void input() { } //Leerzeichen löschen -void deleteWhitespace(){ - for(int i=0; i +#include +#include +#define STRINGL 200 + +char a[STRINGL] = {0}; void main() { - input(); + printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); + fgets(a, STRINGL, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace + input(a, strlen(a)); } From 7fd32e4b0409faeeb741fb6ceb93087e2d615eb0 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:00:16 +0100 Subject: [PATCH 05/41] refactoring: function names improved --- src/inputHandling.c | 12 ++++++------ src/inputHandling.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 8479485..b147ae1 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -7,8 +7,8 @@ char data[1000]; void deleteWhitespace(char* a, int length); op readFunction(char* data, int length); -char* getnumbers(char* data, int length, calc_op* structure_ref); -void printstruct(calc_op* formula); +char* getNumbers(char* data, int length, calc_op* structure_ref); +void showStruct(calc_op* formula); void input(char* a, int length) { @@ -17,8 +17,8 @@ void input(char* a, int length) { memcpy(data,a,length); temp.formel = a; temp.funktionstyp = readFunction(data, 10); - if (getnumbers(data,length, &temp) == NULL){ - printstruct(&temp); + if (getNumbers(data, length, &temp) == NULL){ + showStruct(&temp); } else { printf("Formular %s not supported", temp.formel); } @@ -51,7 +51,7 @@ op readFunction(char* data, int length){ } //Zahlen auslesen (+) -char* getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct +char* getNumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct // char tmp[length]; char* splitPnt; int i = 0; @@ -88,7 +88,7 @@ char* getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: } } -void printstruct(calc_op* formula){ +void showStruct(calc_op* formula){ printf("Berechnung: %s\n", formula->formel); switch (formula->funktionstyp) { case opAdd: diff --git a/src/inputHandling.h b/src/inputHandling.h index 92ace40..9d91f38 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -16,6 +16,6 @@ double result; }calc_op; extern void input(char* a, int length); -extern void printstruct(calc_op* formula); +extern void showStruct(calc_op* formula); #endif // INPUTHANDLING_H From 92288f9a8ca2fae5dedc131ee7b669cd31a6faff Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:07:02 +0100 Subject: [PATCH 06/41] refactoring: unambiguous function variablenames --- src/inputHandling.c | 50 ++++++++++++++++++++++----------------------- src/inputHandling.h | 4 ++-- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index b147ae1..3d1ff4a 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -5,17 +5,17 @@ char data[1000]; -void deleteWhitespace(char* a, int length); -op readFunction(char* data, int length); -char* getNumbers(char* data, int length, calc_op* structure_ref); -void showStruct(calc_op* formula); +void deleteWhitespace(char* formulaString, int length); +op readFunction(char* formulaString, int length); +char* getNumbers(char* formulaString, int length, calc_op* formulaRef); +void showStruct(calc_op* formulaRef); -void input(char* a, int length) { - deleteWhitespace(a, length); +void input(char* formulaString, int length) { + deleteWhitespace(formulaString, length); calc_op temp; - memcpy(data,a,length); - temp.formel = a; + memcpy(data, formulaString, length); + temp.formel = formulaString; temp.funktionstyp = readFunction(data, 10); if (getNumbers(data, length, &temp) == NULL){ showStruct(&temp); @@ -25,20 +25,20 @@ void input(char* a, int length) { } //Leerzeichen löschen -void deleteWhitespace(char* a, int length){ +void deleteWhitespace(char* formulaString, int length){ for(int i=0; ifunktionstyp) { + switch (formulaRef->funktionstyp) { case opAdd: delimiter = '+'; break; @@ -71,15 +71,15 @@ char* getNumbers(char* data, int length, calc_op* structure_ref){ //input sind: break; default: return NULL; } - // memcpy(tmp, data, length); //string kopiert - char *token = strtok(data, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt + // memcpy(tmp, formulaString, length); //string kopiert + char *token = strtok(formulaString, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt while (token != NULL) { - structure_ref->array[i] = atof(token); // String zu double konvertiert + formulaRef->array[i] = atof(token); // String zu double konvertiert i++; splitPnt = token; token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } - structure_ref->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert + formulaRef->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert op type = readFunction(splitPnt, strlen(splitPnt)+1); if (type != opNotSupported && type != opEmpty){ return splitPnt; @@ -88,9 +88,9 @@ char* getNumbers(char* data, int length, calc_op* structure_ref){ //input sind: } } -void showStruct(calc_op* formula){ - printf("Berechnung: %s\n", formula->formel); - switch (formula->funktionstyp) { +void showStruct(calc_op* formulaRef){ + printf("Berechnung: %s\n", formulaRef->formel); + switch (formulaRef->funktionstyp) { case opAdd: printf("Rechenoperation: Addition\n"); break; case opSub: @@ -103,8 +103,8 @@ void showStruct(calc_op* formula){ printf("Fehler bei Auswahl der Rechenoperationen \n"); } printf("Calculation Variables:\n"); - for (int i = 0; i < formula->arraylength; ++i) { - printf("Array[%i] = %f\n", i, formula->array[i]); + for (int i = 0; i < formulaRef->arraylength; ++i) { + printf("Array[%i] = %f\n", i, formulaRef->array[i]); } - printf("Result: %f", formula->result); + printf("Result: %f", formulaRef->result); } \ No newline at end of file diff --git a/src/inputHandling.h b/src/inputHandling.h index 9d91f38..337096e 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -15,7 +15,7 @@ void* parent; double result; }calc_op; -extern void input(char* a, int length); -extern void showStruct(calc_op* formula); +extern void input(char* formulaString, int length); +extern void showStruct(calc_op* formulaRef); #endif // INPUTHANDLING_H From 443b8b8a9a2a335a4da7ca7e6839abd2c1d029da Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:12:25 +0100 Subject: [PATCH 07/41] refactoring: unambiguous variables --- src/inputHandling.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 3d1ff4a..6229f21 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -26,9 +26,9 @@ void input(char* formulaString, int length) { //Leerzeichen löschen void deleteWhitespace(char* formulaString, int length){ - for(int i=0; ifunktionstyp) { case opAdd: @@ -74,12 +74,12 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //input // memcpy(tmp, formulaString, length); //string kopiert char *token = strtok(formulaString, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt while (token != NULL) { - formulaRef->array[i] = atof(token); // String zu double konvertiert - i++; + formulaRef->array[numPos] = atof(token); // String zu double konvertiert + numPos++; splitPnt = token; token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } - formulaRef->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert + formulaRef->arraylength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert op type = readFunction(splitPnt, strlen(splitPnt)+1); if (type != opNotSupported && type != opEmpty){ return splitPnt; @@ -103,8 +103,8 @@ void showStruct(calc_op* formulaRef){ printf("Fehler bei Auswahl der Rechenoperationen \n"); } printf("Calculation Variables:\n"); - for (int i = 0; i < formulaRef->arraylength; ++i) { - printf("Array[%i] = %f\n", i, formulaRef->array[i]); + for (int arrayCount = 0; arrayCount < formulaRef->arraylength; ++arrayCount) { + printf("Array[%i] = %f\n", arrayCount, formulaRef->array[arrayCount]); } printf("Result: %f", formulaRef->result); } \ No newline at end of file From e72fffd3f1c4d0a23c2c1f7616909209993eaa50 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:14:26 +0100 Subject: [PATCH 08/41] refactoring: global variable changed --- src/inputHandling.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 6229f21..58e692b 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -4,7 +4,7 @@ #include -char data[1000]; +char formulaBuffer[1000]; void deleteWhitespace(char* formulaString, int length); op readFunction(char* formulaString, int length); char* getNumbers(char* formulaString, int length, calc_op* formulaRef); @@ -14,10 +14,10 @@ void showStruct(calc_op* formulaRef); void input(char* formulaString, int length) { deleteWhitespace(formulaString, length); calc_op temp; - memcpy(data, formulaString, length); + memcpy(formulaBuffer, formulaString, length); temp.formel = formulaString; - temp.funktionstyp = readFunction(data, 10); - if (getNumbers(data, length, &temp) == NULL){ + temp.funktionstyp = readFunction(formulaBuffer, 10); + if (getNumbers(formulaBuffer, length, &temp) == NULL){ showStruct(&temp); } else { printf("Formular %s not supported", temp.formel); From 3d9d2ac7bc1142884d72fd217951fc9203ef3d53 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:34:58 +0100 Subject: [PATCH 09/41] change local functions to external functions --- src/inputHandling.c | 5 ----- src/inputHandling.h | 4 ++++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 58e692b..cf4893a 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -5,11 +5,6 @@ char formulaBuffer[1000]; -void deleteWhitespace(char* formulaString, int length); -op readFunction(char* formulaString, int length); -char* getNumbers(char* formulaString, int length, calc_op* formulaRef); -void showStruct(calc_op* formulaRef); - void input(char* formulaString, int length) { deleteWhitespace(formulaString, length); diff --git a/src/inputHandling.h b/src/inputHandling.h index 337096e..4387a8e 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -17,5 +17,9 @@ double result; extern void input(char* formulaString, int length); extern void showStruct(calc_op* formulaRef); +extern void deleteWhitespace(char* formulaString, int length); +extern op readFunction(char* formulaString, int length); +extern char* getNumbers(char* formulaString, int length, calc_op* formulaRef); +extern void showStruct(calc_op* formulaRef); #endif // INPUTHANDLING_H From 2e2f1455bab3db7348c107c7a744faa259d5c2b5 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:45:23 +0100 Subject: [PATCH 10/41] Add Whitespace Test --- test/test_inputHandling.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 157270f..a6db8f6 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -3,6 +3,7 @@ #include "unity.h" #include "inputHandling.h" +char halloWelt[]="Hallo Welt"; void setUp(void) { @@ -12,9 +13,10 @@ void tearDown(void) { } -void test_inputHandling_NeedToImplement(void) +void test_inputHandling_deleteOneWhiteSpace(void) { - TEST_IGNORE_MESSAGE("Need to Implement inputHandling"); + deleteWhitespace(halloWelt, 10); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt); } #endif // TEST From 1b1b51bc047551b14d54b1c9fd087d7c7b19b0d1 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:47:13 +0100 Subject: [PATCH 11/41] Add Whitespace Test with 2 Spaces --- test/test_inputHandling.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index a6db8f6..eddc48a 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -4,6 +4,7 @@ #include "inputHandling.h" char halloWelt[]="Hallo Welt"; +char halloWelt2[]="Hallo Welt"; void setUp(void) { @@ -19,4 +20,10 @@ void test_inputHandling_deleteOneWhiteSpace(void) TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt); } +void test_inputHandling_deleteTwoWhiteSpaces(void) +{ + deleteWhitespace(halloWelt, 11); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt); +} + #endif // TEST From 00423f02e1862ec2bac44b4c0a3acfb77b958f81 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 12:57:51 +0100 Subject: [PATCH 12/41] Add Whitespace Test with many Spaces fix Whitespace function --- src/inputHandling.c | 1 + test/test_inputHandling.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index cf4893a..e7f7f61 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -26,6 +26,7 @@ void deleteWhitespace(char* formulaString, int length){ for (int j=stringPos; j < length; j++){ formulaString[j]=formulaString[j + 1]; } + stringPos--; } } } diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index eddc48a..9fb4730 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -5,6 +5,7 @@ #include "inputHandling.h" char halloWelt[]="Hallo Welt"; char halloWelt2[]="Hallo Welt"; +char halloWelt3[]="Ha llo W el t "; void setUp(void) { @@ -22,8 +23,14 @@ void test_inputHandling_deleteOneWhiteSpace(void) void test_inputHandling_deleteTwoWhiteSpaces(void) { - deleteWhitespace(halloWelt, 11); - TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt); + deleteWhitespace(halloWelt2, 11); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt2); +} + +void test_inputHandling_deleteManyWhiteSpaces(void) +{ + deleteWhitespace(halloWelt3, 16); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt3); } #endif // TEST From 98ba967d824ecf9e6ef297eddc431d0ce44b905d Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:02:33 +0100 Subject: [PATCH 13/41] Finished Whitespace Function Tests --- test/test_inputHandling.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 9fb4730..0c16125 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -6,6 +6,7 @@ char halloWelt[]="Hallo Welt"; char halloWelt2[]="Hallo Welt"; char halloWelt3[]="Ha llo W el t "; +char halloWelt4[]="Ha\n\nllo \r W el\r\r t "; void setUp(void) { @@ -33,4 +34,10 @@ void test_inputHandling_deleteManyWhiteSpaces(void) TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt3); } +void test_inputHandling_deleteAllOtherCharacter(void) +{ + deleteWhitespace(halloWelt4, 19); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt4); +} + #endif // TEST From 6245159db6e8ce452f0d3244d4f980eb38b5ce1e Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:10:18 +0100 Subject: [PATCH 14/41] refactoring: function names --- src/inputHandling.c | 10 +++++----- src/inputHandling.h | 4 ++-- src/main.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index e7f7f61..983e521 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -6,12 +6,12 @@ char formulaBuffer[1000]; -void input(char* formulaString, int length) { +void processInput(char* formulaString, int length) { deleteWhitespace(formulaString, length); calc_op temp; memcpy(formulaBuffer, formulaString, length); temp.formel = formulaString; - temp.funktionstyp = readFunction(formulaBuffer, 10); + temp.funktionstyp = detectFunctionOperator(formulaBuffer, 10); if (getNumbers(formulaBuffer, length, &temp) == NULL){ showStruct(&temp); } else { @@ -32,7 +32,7 @@ void deleteWhitespace(char* formulaString, int length){ } //Einfachste Rechenoperationen lesen -op readFunction(char* formulaString, int length){ +op detectFunctionOperator(char* formulaString, int length){ for(int stringCount=0; stringCount < length; stringCount++){ switch (formulaString[stringCount]){ case '+': return opAdd; @@ -47,7 +47,7 @@ op readFunction(char* formulaString, int length){ } //Zahlen auslesen (+) -char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //input sind: string, länge vom String, berechnungsstruct +char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //processInput sind: string, länge vom String, berechnungsstruct // char tmp[length]; char* splitPnt; int numPos = 0; @@ -76,7 +76,7 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //input token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } formulaRef->arraylength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert - op type = readFunction(splitPnt, strlen(splitPnt)+1); + op type = detectFunctionOperator(splitPnt, strlen(splitPnt) + 1); if (type != opNotSupported && type != opEmpty){ return splitPnt; } else { diff --git a/src/inputHandling.h b/src/inputHandling.h index 4387a8e..3fa35e7 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -15,10 +15,10 @@ void* parent; double result; }calc_op; -extern void input(char* formulaString, int length); +extern void processInput(char* formulaString, int length); extern void showStruct(calc_op* formulaRef); extern void deleteWhitespace(char* formulaString, int length); -extern op readFunction(char* formulaString, int length); +extern op detectFunctionOperator(char* formulaString, int length); extern char* getNumbers(char* formulaString, int length, calc_op* formulaRef); extern void showStruct(calc_op* formulaRef); diff --git a/src/main.c b/src/main.c index 23d049e..310d6ea 100644 --- a/src/main.c +++ b/src/main.c @@ -10,5 +10,5 @@ char a[STRINGL] = {0}; void main() { printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); fgets(a, STRINGL, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace - input(a, strlen(a)); + processInput(a, strlen(a)); } From 0cd447599e388c9b266c0aa2fd3cf0564736782b Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:12:45 +0100 Subject: [PATCH 15/41] refactoring: Rename global variable and define --- src/main.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 310d6ea..a496f5e 100644 --- a/src/main.c +++ b/src/main.c @@ -3,12 +3,12 @@ #include #include #include -#define STRINGL 200 +#define STRING_LENGTH 200 -char a[STRINGL] = {0}; +char inputBuffer[STRING_LENGTH] = {0}; void main() { printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); - fgets(a, STRINGL, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace - processInput(a, strlen(a)); + fgets(inputBuffer, STRING_LENGTH, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace + processInput(inputBuffer, strlen(inputBuffer)); } From 478acc16d32c9319a248962635fa1b0d893e64c5 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:22:41 +0100 Subject: [PATCH 16/41] Implement Add find test --- test/test_inputHandling.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 0c16125..ee9fa90 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -40,4 +40,11 @@ void test_inputHandling_deleteAllOtherCharacter(void) TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt4); } +void test_inputHandling_findAddFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4+5", 3); + TEST_ASSERT_TRUE(opAdd == type); +} + #endif // TEST From db8180fbe6eb635bb27a7bffd41e57f34fbcc92a Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:24:06 +0100 Subject: [PATCH 17/41] Implement Sub find test --- test/test_inputHandling.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index ee9fa90..139cd7b 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -47,4 +47,11 @@ void test_inputHandling_findAddFunctionType(void) TEST_ASSERT_TRUE(opAdd == type); } +void test_inputHandling_findSubFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4-5", 3); + TEST_ASSERT_TRUE(opSub == type); +} + #endif // TEST From bd93f6dc7fad2ce6de402ffcb1495b3a2fe168cf Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:26:29 +0100 Subject: [PATCH 18/41] Implement Multi find test --- test/test_inputHandling.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 139cd7b..e4f6be1 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -54,4 +54,11 @@ void test_inputHandling_findSubFunctionType(void) TEST_ASSERT_TRUE(opSub == type); } +void test_inputHandling_findMultiFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4*5", 3); + TEST_ASSERT_TRUE(opMult == type); +} + #endif // TEST From c22a51bf6cd91f58afde14a013e97f1347bbe1da Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:29:30 +0100 Subject: [PATCH 19/41] Implement Div find test --- src/inputHandling.c | 2 +- test/test_inputHandling.c | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 983e521..69c70ea 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -37,7 +37,7 @@ op detectFunctionOperator(char* formulaString, int length){ switch (formulaString[stringCount]){ case '+': return opAdd; case '-': return opSub; - case '/': return opDiv; + case '/':case ':': return opDiv; case '*': return opMult; case '^': return opExp; case '\0': return opEmpty; diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index e4f6be1..f68f20c 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -61,4 +61,14 @@ void test_inputHandling_findMultiFunctionType(void) TEST_ASSERT_TRUE(opMult == type); } +void test_inputHandling_findDivFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4/5", 3); + TEST_ASSERT_TRUE(opDiv == type); + type = opNotSupported; + type = detectFunctionOperator("4:5", 3); + TEST_ASSERT_TRUE(opDiv == type); +} + #endif // TEST From 7d06c4632b05b2cc075f0003f800dcddc5d7b993 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:31:15 +0100 Subject: [PATCH 20/41] Implement Exp find test --- test/test_inputHandling.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index f68f20c..7f8364f 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -71,4 +71,11 @@ void test_inputHandling_findDivFunctionType(void) TEST_ASSERT_TRUE(opDiv == type); } +void test_inputHandling_findExpFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4^5", 3); + TEST_ASSERT_TRUE(opExp == type); +} + #endif // TEST From 4d45e4f1ba339a0fc452e3ad0ea548b96324292b Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:34:35 +0100 Subject: [PATCH 21/41] Implement nothing to find test --- test/test_inputHandling.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 7f8364f..75c5725 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -78,4 +78,11 @@ void test_inputHandling_findExpFunctionType(void) TEST_ASSERT_TRUE(opExp == type); } +void test_inputHandling_findEmptyFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator(halloWelt, 10); + TEST_ASSERT_TRUE(opEmpty == type); +} + #endif // TEST From 0f23ca57c193cabbc1c3ddc2c974867642518f39 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:44:37 +0100 Subject: [PATCH 22/41] refactoring: change struct names --- src/inputHandling.c | 20 ++++++++++---------- src/inputHandling.h | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 69c70ea..06974a3 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -10,12 +10,12 @@ void processInput(char* formulaString, int length) { deleteWhitespace(formulaString, length); calc_op temp; memcpy(formulaBuffer, formulaString, length); - temp.formel = formulaString; - temp.funktionstyp = detectFunctionOperator(formulaBuffer, 10); + temp.formular = formulaString; + temp.functionsType = detectFunctionOperator(formulaBuffer, 10); if (getNumbers(formulaBuffer, length, &temp) == NULL){ showStruct(&temp); } else { - printf("Formular %s not supported", temp.formel); + printf("Formular %s not supported", temp.formular); } } @@ -52,7 +52,7 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //proces char* splitPnt; int numPos = 0; char delimiter; - switch (formulaRef->funktionstyp) { + switch (formulaRef->functionsType) { case opAdd: delimiter = '+'; break; @@ -70,12 +70,12 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //proces // memcpy(tmp, formulaString, length); //string kopiert char *token = strtok(formulaString, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt while (token != NULL) { - formulaRef->array[numPos] = atof(token); // String zu double konvertiert + formulaRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert numPos++; splitPnt = token; token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } - formulaRef->arraylength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert + formulaRef->arrayLength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert op type = detectFunctionOperator(splitPnt, strlen(splitPnt) + 1); if (type != opNotSupported && type != opEmpty){ return splitPnt; @@ -85,8 +85,8 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //proces } void showStruct(calc_op* formulaRef){ - printf("Berechnung: %s\n", formulaRef->formel); - switch (formulaRef->funktionstyp) { + printf("Berechnung: %s\n", formulaRef->formular); + switch (formulaRef->functionsType) { case opAdd: printf("Rechenoperation: Addition\n"); break; case opSub: @@ -99,8 +99,8 @@ void showStruct(calc_op* formulaRef){ printf("Fehler bei Auswahl der Rechenoperationen \n"); } printf("Calculation Variables:\n"); - for (int arrayCount = 0; arrayCount < formulaRef->arraylength; ++arrayCount) { - printf("Array[%i] = %f\n", arrayCount, formulaRef->array[arrayCount]); + for (int arrayCount = 0; arrayCount < formulaRef->arrayLength; ++arrayCount) { + printf("Array[%i] = %f\n", arrayCount, formulaRef->inputNumbers[arrayCount]); } printf("Result: %f", formulaRef->result); } \ No newline at end of file diff --git a/src/inputHandling.h b/src/inputHandling.h index 3fa35e7..9af608f 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -6,10 +6,10 @@ typedef enum{ }op; typedef struct { -op funktionstyp; -char* formel; -double array[10]; -int arraylength; +op functionsType; +char* formular; +double inputNumbers[10]; +int arrayLength; void* child; void* parent; double result; From 97163c8cce683077ea3ffd0dc29e23c0daf1a0d1 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 13:50:42 +0100 Subject: [PATCH 23/41] add no formular detected Test for getNumber Function --- test/test_inputHandling.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 75c5725..7ac418d 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -7,9 +7,11 @@ char halloWelt[]="Hallo Welt"; char halloWelt2[]="Hallo Welt"; char halloWelt3[]="Ha llo W el t "; char halloWelt4[]="Ha\n\nllo \r W el\r\r t "; +calc_op formula = {0}; void setUp(void) { + formula.functionsType = opNotSupported; } void tearDown(void) @@ -85,4 +87,10 @@ void test_inputHandling_findEmptyFunctionType(void) TEST_ASSERT_TRUE(opEmpty == type); } +void test_inputHandling_getNumbersNoFormular(void) +{ + char* pnt = NULL; + pnt = getNumbers(halloWelt, 10, &formula); + TEST_ASSERT_NULL(pnt); +} #endif // TEST From c05d58f22aaf6ee161ad625a0f774a1704277fd5 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 14:07:39 +0100 Subject: [PATCH 24/41] Implement add detected Test for getNumber Function --- project.yml | 1 + test/test_inputHandling.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/project.yml b/project.yml index cfbdf24..11236c5 100644 --- a/project.yml +++ b/project.yml @@ -47,6 +47,7 @@ :test: - *common_defines - TEST + - UNITY_INCLUDE_DOUBLE :test_preprocess: - *common_defines - TEST diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 7ac418d..ac97c3d 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -93,4 +93,17 @@ void test_inputHandling_getNumbersNoFormular(void) pnt = getNumbers(halloWelt, 10, &formula); TEST_ASSERT_NULL(pnt); } + +void test_inputHandling_getNumbersAddFormular(void) +{ + char* pnt = NULL; + char add[] = "4+5"; + formula.functionsType = detectFunctionOperator(add,3); + pnt = getNumbers(add, 3, &formula); + showStruct(&formula); + TEST_ASSERT_NULL(pnt); + TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]); + TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]); + TEST_ASSERT_EQUAL_INT(2, formula.arrayLength); +} #endif // TEST From 4744e965bb377e02f81be9323d622e9273ce2147 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 14:10:38 +0100 Subject: [PATCH 25/41] Implement sub detected Test for getNumber Function --- test/test_inputHandling.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index ac97c3d..3ba48da 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -106,4 +106,17 @@ void test_inputHandling_getNumbersAddFormular(void) TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]); TEST_ASSERT_EQUAL_INT(2, formula.arrayLength); } + +void test_inputHandling_getNumbersSubFormular(void) +{ + char* pnt = NULL; + char add[] = "4-5"; + formula.functionsType = detectFunctionOperator(add,3); + pnt = getNumbers(add, 3, &formula); + showStruct(&formula); + TEST_ASSERT_NULL(pnt); + TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]); + TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]); + TEST_ASSERT_EQUAL_INT(2, formula.arrayLength); +} #endif // TEST From 980d22cae4ee4b906db3dd0f8e29b1b4d7dcdbb0 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 14:15:03 +0100 Subject: [PATCH 26/41] Implement multi detected Test for getNumber Function --- test/test_inputHandling.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 3ba48da..0b666e9 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -99,6 +99,7 @@ void test_inputHandling_getNumbersAddFormular(void) char* pnt = NULL; char add[] = "4+5"; formula.functionsType = detectFunctionOperator(add,3); + TEST_ASSERT_TRUE(formula.functionsType == opAdd); pnt = getNumbers(add, 3, &formula); showStruct(&formula); TEST_ASSERT_NULL(pnt); @@ -110,9 +111,24 @@ void test_inputHandling_getNumbersAddFormular(void) void test_inputHandling_getNumbersSubFormular(void) { char* pnt = NULL; - char add[] = "4-5"; - formula.functionsType = detectFunctionOperator(add,3); - pnt = getNumbers(add, 3, &formula); + char sub[] = "4-5"; + formula.functionsType = detectFunctionOperator(sub,3); + TEST_ASSERT_TRUE(formula.functionsType == opSub); + pnt = getNumbers(sub, 3, &formula); + showStruct(&formula); + TEST_ASSERT_NULL(pnt); + TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]); + TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]); + TEST_ASSERT_EQUAL_INT(2, formula.arrayLength); +} + +void test_inputHandling_getNumbersMultiFormular(void) +{ + char* pnt = NULL; + char multi[] = "4*5"; + formula.functionsType = detectFunctionOperator(multi,3); + TEST_ASSERT_TRUE(formula.functionsType == opMult); + pnt = getNumbers(multi, 3, &formula); showStruct(&formula); TEST_ASSERT_NULL(pnt); TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]); From bdad9560be676db877bdbcd0350d525a5d7de134 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 14:19:51 +0100 Subject: [PATCH 27/41] Implement div detected Test for getNumber Function --- test/test_inputHandling.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 0b666e9..404d840 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -135,4 +135,18 @@ void test_inputHandling_getNumbersMultiFormular(void) TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]); TEST_ASSERT_EQUAL_INT(2, formula.arrayLength); } + +void test_inputHandling_getNumbersDivFormular(void) +{ + char* pnt = NULL; + char div[] = "4/5"; + formula.functionsType = detectFunctionOperator(div,3); + TEST_ASSERT_TRUE(formula.functionsType == opDiv); + pnt = getNumbers(div, 3, &formula); + showStruct(&formula); + TEST_ASSERT_NULL(pnt); + TEST_ASSERT_EQUAL_DOUBLE(4.0, formula.inputNumbers[0]); + TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]); + TEST_ASSERT_EQUAL_INT(2, formula.arrayLength); +} #endif // TEST From 83aec72e1edaa75faaafddeb5728d10f9298f5c8 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 17:50:47 +0100 Subject: [PATCH 28/41] Create Output Handling --- src/inputHandling.c | 4 ++-- src/main.c | 3 +++ src/outputHandling.c | 8 ++++++++ src/outputHandling.h | 8 ++++++++ test/test_outputHandling.c | 20 ++++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/outputHandling.c create mode 100644 src/outputHandling.h create mode 100644 test/test_outputHandling.c diff --git a/src/inputHandling.c b/src/inputHandling.c index 06974a3..c8191f1 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -85,7 +85,7 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //proces } void showStruct(calc_op* formulaRef){ - printf("Berechnung: %s\n", formulaRef->formular); + printf("\nBerechnung: %s\n", formulaRef->formular); switch (formulaRef->functionsType) { case opAdd: printf("Rechenoperation: Addition\n"); break; @@ -102,5 +102,5 @@ void showStruct(calc_op* formulaRef){ for (int arrayCount = 0; arrayCount < formulaRef->arrayLength; ++arrayCount) { printf("Array[%i] = %f\n", arrayCount, formulaRef->inputNumbers[arrayCount]); } - printf("Result: %f", formulaRef->result); + printf("Result: %f\n", formulaRef->result); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index a496f5e..9c7787d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include "helloWorld.h" #include "inputHandling.h" +#include "outputHandling.h" #include #include #include @@ -8,7 +9,9 @@ char inputBuffer[STRING_LENGTH] = {0}; void main() { + calc_op result; printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); fgets(inputBuffer, STRING_LENGTH, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace processInput(inputBuffer, strlen(inputBuffer)); + printResult(&result); } diff --git a/src/outputHandling.c b/src/outputHandling.c new file mode 100644 index 0000000..b38a651 --- /dev/null +++ b/src/outputHandling.c @@ -0,0 +1,8 @@ +#include +#include "outputHandling.h" +#include "inputHandling.h" + + +void printResult(calc_op* result) { + printf("Das Ergebnis ist: %f", result->result); +} \ No newline at end of file diff --git a/src/outputHandling.h b/src/outputHandling.h new file mode 100644 index 0000000..4917a54 --- /dev/null +++ b/src/outputHandling.h @@ -0,0 +1,8 @@ +#ifndef OUTPUTHANDLING_H +#define OUTPUTHANDLING_H + +#include "inputHandling.h" + +extern void printResult(calc_op* result); + +#endif // OUTPUTHANDLING_H diff --git a/test/test_outputHandling.c b/test/test_outputHandling.c new file mode 100644 index 0000000..8735cd7 --- /dev/null +++ b/test/test_outputHandling.c @@ -0,0 +1,20 @@ +#ifdef TEST + +#include "unity.h" + +#include "outputHandling.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_outputHandling_NeedToImplement(void) +{ + TEST_IGNORE_MESSAGE("Need to Implement outputHandling"); +} + +#endif // TEST From e60128b83b5efba31b9a1c663b52d01244059f97 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:03:04 +0100 Subject: [PATCH 29/41] Add hex String --- src/outputHandling.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/outputHandling.c b/src/outputHandling.c index b38a651..0a00670 100644 --- a/src/outputHandling.c +++ b/src/outputHandling.c @@ -2,7 +2,14 @@ #include "outputHandling.h" #include "inputHandling.h" +void hex(char* string, int number) { + sprintf(string, "%X", number); +} void printResult(calc_op* result) { - printf("Das Ergebnis ist: %f", result->result); + char buf[60] = {0}; + printf("Das Ergebnis ist: %f\n", result->result); + printf("Das Ergebnis in int: %i\n",(int)result->result); + hex(buf, (int)result->result); + printf("Das Ergebnis in hex: 0x%s\n", buf); ; } \ No newline at end of file From 2cd846ed3033c92f050182cb67fa4898b6d64a34 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:17:35 +0100 Subject: [PATCH 30/41] add oct String output --- src/main.c | 1 + src/outputHandling.c | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 9c7787d..10e0a7b 100644 --- a/src/main.c +++ b/src/main.c @@ -13,5 +13,6 @@ void main() { printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); fgets(inputBuffer, STRING_LENGTH, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace processInput(inputBuffer, strlen(inputBuffer)); + result.result = 65478; printResult(&result); } diff --git a/src/outputHandling.c b/src/outputHandling.c index 0a00670..bd1a3a6 100644 --- a/src/outputHandling.c +++ b/src/outputHandling.c @@ -6,10 +6,16 @@ void hex(char* string, int number) { sprintf(string, "%X", number); } +void oct(char* string, int number) { + sprintf(string, "%o", number); +} + void printResult(calc_op* result) { char buf[60] = {0}; printf("Das Ergebnis ist: %f\n", result->result); - printf("Das Ergebnis in int: %i\n",(int)result->result); + printf("Das Ergebnis in dec: %i\n",(int)result->result); hex(buf, (int)result->result); printf("Das Ergebnis in hex: 0x%s\n", buf); ; + oct(buf, (int)result->result); + printf("Das Ergebnis in oct: %s\n", buf); ; } \ No newline at end of file From 8669486934e11b99b254f66f925583edbf0ca20d Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:26:31 +0100 Subject: [PATCH 31/41] refactoring: output functions --- src/main.c | 2 +- src/outputHandling.c | 15 ++++++++------- src/outputHandling.h | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 10e0a7b..6a55347 100644 --- a/src/main.c +++ b/src/main.c @@ -14,5 +14,5 @@ void main() { fgets(inputBuffer, STRING_LENGTH, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace processInput(inputBuffer, strlen(inputBuffer)); result.result = 65478; - printResult(&result); + showResult(&result); } diff --git a/src/outputHandling.c b/src/outputHandling.c index bd1a3a6..10ce3f8 100644 --- a/src/outputHandling.c +++ b/src/outputHandling.c @@ -2,20 +2,21 @@ #include "outputHandling.h" #include "inputHandling.h" -void hex(char* string, int number) { +void buildHexString(char* string, int number) { sprintf(string, "%X", number); } -void oct(char* string, int number) { +void buildOctString(char* string, int number) { sprintf(string, "%o", number); } -void printResult(calc_op* result) { + +void showResult(calc_op* result) { char buf[60] = {0}; printf("Das Ergebnis ist: %f\n", result->result); printf("Das Ergebnis in dec: %i\n",(int)result->result); - hex(buf, (int)result->result); - printf("Das Ergebnis in hex: 0x%s\n", buf); ; - oct(buf, (int)result->result); - printf("Das Ergebnis in oct: %s\n", buf); ; + buildHexString(buf, (int) result->result); + printf("Das Ergebnis in buildHexString: 0x%s\n", buf); ; + buildOctString(buf, (int) result->result); + printf("Das Ergebnis in buildOctString: %s\n", buf); ; } \ No newline at end of file diff --git a/src/outputHandling.h b/src/outputHandling.h index 4917a54..f81b55c 100644 --- a/src/outputHandling.h +++ b/src/outputHandling.h @@ -3,6 +3,6 @@ #include "inputHandling.h" -extern void printResult(calc_op* result); +extern void showResult(calc_op* result); #endif // OUTPUTHANDLING_H From 49c95dee192ff3832e4a58f3bbe03a504edca7c2 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:29:27 +0100 Subject: [PATCH 32/41] refactoring: change buffer name --- src/outputHandling.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/outputHandling.c b/src/outputHandling.c index 10ce3f8..2f66f64 100644 --- a/src/outputHandling.c +++ b/src/outputHandling.c @@ -12,11 +12,11 @@ void buildOctString(char* string, int number) { void showResult(calc_op* result) { - char buf[60] = {0}; + char string[60] = {0}; printf("Das Ergebnis ist: %f\n", result->result); printf("Das Ergebnis in dec: %i\n",(int)result->result); - buildHexString(buf, (int) result->result); - printf("Das Ergebnis in buildHexString: 0x%s\n", buf); ; - buildOctString(buf, (int) result->result); - printf("Das Ergebnis in buildOctString: %s\n", buf); ; + buildHexString(string, (int) result->result); + printf("Das Ergebnis in buildHexString: 0x%s\n", string); ; + buildOctString(string, (int) result->result); + printf("Das Ergebnis in buildOctString: %s\n", string); ; } \ No newline at end of file From 4db7249162878fd0c0ec017db47b090bc81abdbd Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:32:43 +0100 Subject: [PATCH 33/41] refactoring: change function variable names --- src/outputHandling.c | 18 +++++++++--------- src/outputHandling.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/outputHandling.c b/src/outputHandling.c index 2f66f64..4a2fef1 100644 --- a/src/outputHandling.c +++ b/src/outputHandling.c @@ -2,21 +2,21 @@ #include "outputHandling.h" #include "inputHandling.h" -void buildHexString(char* string, int number) { - sprintf(string, "%X", number); +void buildHexString(char* string, int num) { + sprintf(string, "%X", num); } -void buildOctString(char* string, int number) { - sprintf(string, "%o", number); +void buildOctString(char* string, int num) { + sprintf(string, "%o", num); } -void showResult(calc_op* result) { +void showResult(calc_op* res) { char string[60] = {0}; - printf("Das Ergebnis ist: %f\n", result->result); - printf("Das Ergebnis in dec: %i\n",(int)result->result); - buildHexString(string, (int) result->result); + printf("Das Ergebnis ist: %f\n", res->result); + printf("Das Ergebnis in dec: %i\n",(int)res->result); + buildHexString(string, (int) res->result); printf("Das Ergebnis in buildHexString: 0x%s\n", string); ; - buildOctString(string, (int) result->result); + buildOctString(string, (int) res->result); printf("Das Ergebnis in buildOctString: %s\n", string); ; } \ No newline at end of file diff --git a/src/outputHandling.h b/src/outputHandling.h index f81b55c..4647485 100644 --- a/src/outputHandling.h +++ b/src/outputHandling.h @@ -3,6 +3,6 @@ #include "inputHandling.h" -extern void showResult(calc_op* result); +extern void showResult(calc_op* res); #endif // OUTPUTHANDLING_H From 1b22182dac0bf6c2983765cf7e8b08eb49310c10 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:36:19 +0100 Subject: [PATCH 34/41] refactoring: change function variable names in inputHandling --- src/inputHandling.c | 36 ++++++++++++++++++------------------ src/inputHandling.h | 12 ++++++------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index c8191f1..54ceecf 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -6,11 +6,11 @@ char formulaBuffer[1000]; -void processInput(char* formulaString, int length) { - deleteWhitespace(formulaString, length); +void processInput(char* formStr, int length) { + deleteWhitespace(formStr, length); calc_op temp; - memcpy(formulaBuffer, formulaString, length); - temp.formular = formulaString; + memcpy(formulaBuffer, formStr, length); + temp.formular = formStr; temp.functionsType = detectFunctionOperator(formulaBuffer, 10); if (getNumbers(formulaBuffer, length, &temp) == NULL){ showStruct(&temp); @@ -20,11 +20,11 @@ void processInput(char* formulaString, int length) { } //Leerzeichen löschen -void deleteWhitespace(char* formulaString, int length){ +void deleteWhitespace(char* formStr, int length){ for(int stringPos=0; stringPos < length; stringPos++){ - if((formulaString[stringPos] == ' ') || (formulaString[stringPos] == '\n') || (formulaString[stringPos] == '\r')){ + if((formStr[stringPos] == ' ') || (formStr[stringPos] == '\n') || (formStr[stringPos] == '\r')){ for (int j=stringPos; j < length; j++){ - formulaString[j]=formulaString[j + 1]; + formStr[j]=formStr[j + 1]; } stringPos--; } @@ -32,9 +32,9 @@ void deleteWhitespace(char* formulaString, int length){ } //Einfachste Rechenoperationen lesen -op detectFunctionOperator(char* formulaString, int length){ +op detectFunctionOperator(char* formStr, int length){ for(int stringCount=0; stringCount < length; stringCount++){ - switch (formulaString[stringCount]){ + switch (formStr[stringCount]){ case '+': return opAdd; case '-': return opSub; case '/':case ':': return opDiv; @@ -47,7 +47,7 @@ op detectFunctionOperator(char* formulaString, int length){ } //Zahlen auslesen (+) -char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //processInput sind: string, länge vom String, berechnungsstruct +char* getNumbers(char* formStr, int length, calc_op* formulaRef){ //processInput sind: string, länge vom String, berechnungsstruct // char tmp[length]; char* splitPnt; int numPos = 0; @@ -67,8 +67,8 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //proces break; default: return NULL; } - // memcpy(tmp, formulaString, length); //string kopiert - char *token = strtok(formulaString, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt + // memcpy(tmp, formStr, length); //string kopiert + char *token = strtok(formStr, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt while (token != NULL) { formulaRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert numPos++; @@ -84,9 +84,9 @@ char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //proces } } -void showStruct(calc_op* formulaRef){ - printf("\nBerechnung: %s\n", formulaRef->formular); - switch (formulaRef->functionsType) { +void showStruct(calc_op* formRef){ + printf("\nBerechnung: %s\n", formRef->formular); + switch (formRef->functionsType) { case opAdd: printf("Rechenoperation: Addition\n"); break; case opSub: @@ -99,8 +99,8 @@ void showStruct(calc_op* formulaRef){ printf("Fehler bei Auswahl der Rechenoperationen \n"); } printf("Calculation Variables:\n"); - for (int arrayCount = 0; arrayCount < formulaRef->arrayLength; ++arrayCount) { - printf("Array[%i] = %f\n", arrayCount, formulaRef->inputNumbers[arrayCount]); + for (int arrayCount = 0; arrayCount < formRef->arrayLength; ++arrayCount) { + printf("Array[%i] = %f\n", arrayCount, formRef->inputNumbers[arrayCount]); } - printf("Result: %f\n", formulaRef->result); + printf("Result: %f\n", formRef->result); } \ No newline at end of file diff --git a/src/inputHandling.h b/src/inputHandling.h index 9af608f..ca9e2fa 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -15,11 +15,11 @@ void* parent; double result; }calc_op; -extern void processInput(char* formulaString, int length); -extern void showStruct(calc_op* formulaRef); -extern void deleteWhitespace(char* formulaString, int length); -extern op detectFunctionOperator(char* formulaString, int length); -extern char* getNumbers(char* formulaString, int length, calc_op* formulaRef); -extern void showStruct(calc_op* formulaRef); +extern void processInput(char* formStr, int length); +extern void showStruct(calc_op* formRef); +extern void deleteWhitespace(char* formStr, int length); +extern op detectFunctionOperator(char* formStr, int length); +extern char* getNumbers(char* formStr, int length, calc_op* formulaRef); +extern void showStruct(calc_op* formRef); #endif // INPUTHANDLING_H From 2aa9fd94a1a6f25b736306a5e56ee2e4a1a64e84 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:39:26 +0100 Subject: [PATCH 35/41] refactoring: change function variable names in input handling --- src/inputHandling.c | 30 +++++++++++++++--------------- src/inputHandling.h | 8 ++++---- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 54ceecf..e539c60 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -6,13 +6,13 @@ char formulaBuffer[1000]; -void processInput(char* formStr, int length) { - deleteWhitespace(formStr, length); +void processInput(char* formStr, int len) { + deleteWhitespace(formStr, len); calc_op temp; - memcpy(formulaBuffer, formStr, length); + memcpy(formulaBuffer, formStr, len); temp.formular = formStr; temp.functionsType = detectFunctionOperator(formulaBuffer, 10); - if (getNumbers(formulaBuffer, length, &temp) == NULL){ + if (getNumbers(formulaBuffer, len, &temp) == NULL){ showStruct(&temp); } else { printf("Formular %s not supported", temp.formular); @@ -20,10 +20,10 @@ void processInput(char* formStr, int length) { } //Leerzeichen löschen -void deleteWhitespace(char* formStr, int length){ - for(int stringPos=0; stringPos < length; stringPos++){ +void deleteWhitespace(char* formStr, int len){ + for(int stringPos=0; stringPos < len; stringPos++){ if((formStr[stringPos] == ' ') || (formStr[stringPos] == '\n') || (formStr[stringPos] == '\r')){ - for (int j=stringPos; j < length; j++){ + for (int j=stringPos; j < len; j++){ formStr[j]=formStr[j + 1]; } stringPos--; @@ -32,8 +32,8 @@ void deleteWhitespace(char* formStr, int length){ } //Einfachste Rechenoperationen lesen -op detectFunctionOperator(char* formStr, int length){ - for(int stringCount=0; stringCount < length; stringCount++){ +op detectFunctionOperator(char* formStr, int len){ + for(int stringCount=0; stringCount < len; stringCount++){ switch (formStr[stringCount]){ case '+': return opAdd; case '-': return opSub; @@ -47,12 +47,12 @@ op detectFunctionOperator(char* formStr, int length){ } //Zahlen auslesen (+) -char* getNumbers(char* formStr, int length, calc_op* formulaRef){ //processInput sind: string, länge vom String, berechnungsstruct - // char tmp[length]; +char* getNumbers(char* formStr, int len, calc_op* formRef){ //processInput sind: string, länge vom String, berechnungsstruct + // char tmp[len]; char* splitPnt; int numPos = 0; char delimiter; - switch (formulaRef->functionsType) { + switch (formRef->functionsType) { case opAdd: delimiter = '+'; break; @@ -67,15 +67,15 @@ char* getNumbers(char* formStr, int length, calc_op* formulaRef){ //processInput break; default: return NULL; } - // memcpy(tmp, formStr, length); //string kopiert + // memcpy(tmp, formStr, len); //string kopiert char *token = strtok(formStr, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt while (token != NULL) { - formulaRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert + formRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert numPos++; splitPnt = token; token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } - formulaRef->arrayLength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert + formRef->arrayLength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert op type = detectFunctionOperator(splitPnt, strlen(splitPnt) + 1); if (type != opNotSupported && type != opEmpty){ return splitPnt; diff --git a/src/inputHandling.h b/src/inputHandling.h index ca9e2fa..e5635de 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -15,11 +15,11 @@ void* parent; double result; }calc_op; -extern void processInput(char* formStr, int length); +extern void processInput(char* formStr, int len); extern void showStruct(calc_op* formRef); -extern void deleteWhitespace(char* formStr, int length); -extern op detectFunctionOperator(char* formStr, int length); -extern char* getNumbers(char* formStr, int length, calc_op* formulaRef); +extern void deleteWhitespace(char* formStr, int len); +extern op detectFunctionOperator(char* formStr, int len); +extern char* getNumbers(char* formStr, int len, calc_op* formRef); extern void showStruct(calc_op* formRef); #endif // INPUTHANDLING_H From 999af488024eeb2b2a03322ab482b1bde3952cf9 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:46:57 +0100 Subject: [PATCH 36/41] Create Hex Output Test --- src/outputHandling.h | 2 ++ test/test_outputHandling.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/outputHandling.h b/src/outputHandling.h index 4647485..77ff505 100644 --- a/src/outputHandling.h +++ b/src/outputHandling.h @@ -3,6 +3,8 @@ #include "inputHandling.h" +extern void buildHexString(char* string, int num); +extern void buildOctString(char* string, int num); extern void showResult(calc_op* res); #endif // OUTPUTHANDLING_H diff --git a/test/test_outputHandling.c b/test/test_outputHandling.c index 8735cd7..7ea50e5 100644 --- a/test/test_outputHandling.c +++ b/test/test_outputHandling.c @@ -4,6 +4,8 @@ #include "outputHandling.h" +char string[100]= {0}; + void setUp(void) { } @@ -12,9 +14,10 @@ void tearDown(void) { } -void test_outputHandling_NeedToImplement(void) +void test_outputHandling_buildOctString(void) { - TEST_IGNORE_MESSAGE("Need to Implement outputHandling"); + buildOctString(string, 5555555); + TEST_ASSERT_EQUAL_STRING("25142543", string); } #endif // TEST From 5a9978be91259fd3f8c42365d7cea20727d15ab6 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:50:30 +0100 Subject: [PATCH 37/41] Create Oct Output Test --- src/outputHandling.c | 2 +- test/test_outputHandling.c | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/outputHandling.c b/src/outputHandling.c index 4a2fef1..450b72b 100644 --- a/src/outputHandling.c +++ b/src/outputHandling.c @@ -3,7 +3,7 @@ #include "inputHandling.h" void buildHexString(char* string, int num) { - sprintf(string, "%X", num); + sprintf(string, "0x%X", num); } void buildOctString(char* string, int num) { diff --git a/test/test_outputHandling.c b/test/test_outputHandling.c index 7ea50e5..13b77bb 100644 --- a/test/test_outputHandling.c +++ b/test/test_outputHandling.c @@ -20,4 +20,10 @@ void test_outputHandling_buildOctString(void) TEST_ASSERT_EQUAL_STRING("25142543", string); } +void test_outputHandling_buildHexString(void) +{ + buildHexString(string, 5555555); + TEST_ASSERT_EQUAL_STRING("0x54C563", string); +} + #endif // TEST From 28564e7701915bbcf08a7ef54509cfed1a5c24a1 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 18:55:58 +0100 Subject: [PATCH 38/41] change temp formular struct to pointer --- src/inputHandling.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index e539c60..867402b 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -5,17 +5,21 @@ char formulaBuffer[1000]; +calc_op* mainCalc = NULL; void processInput(char* formStr, int len) { deleteWhitespace(formStr, len); - calc_op temp; + if (mainCalc!=NULL){ + free(mainCalc); + } + mainCalc = malloc(sizeof(calc_op)); memcpy(formulaBuffer, formStr, len); - temp.formular = formStr; - temp.functionsType = detectFunctionOperator(formulaBuffer, 10); - if (getNumbers(formulaBuffer, len, &temp) == NULL){ - showStruct(&temp); + mainCalc->formular = formStr; + mainCalc->functionsType = detectFunctionOperator(formulaBuffer, 10); + if (getNumbers(formulaBuffer, len, mainCalc) == NULL){ + showStruct(mainCalc); } else { - printf("Formular %s not supported", temp.formular); + printf("Formular %s not supported", mainCalc->formular); } } From b283647e58aa1b13a9913df11605f14f0a1686d8 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 19:16:09 +0100 Subject: [PATCH 39/41] add get next calculation function --- src/inputHandling.c | 9 +++++++++ src/inputHandling.h | 5 +++-- src/main.c | 11 ++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 867402b..e9d8217 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -6,6 +6,7 @@ char formulaBuffer[1000]; calc_op* mainCalc = NULL; +calc_op* currentCalc = NULL; void processInput(char* formStr, int len) { deleteWhitespace(formStr, len); @@ -23,6 +24,14 @@ void processInput(char* formStr, int len) { } } +calc_op* getNextCalc(){ + if (currentCalc != NULL){ + free(currentCalc); + } + currentCalc = mainCalc; // get ext calculation + return currentCalc; +} + //Leerzeichen löschen void deleteWhitespace(char* formStr, int len){ for(int stringPos=0; stringPos < len; stringPos++){ diff --git a/src/inputHandling.h b/src/inputHandling.h index e5635de..9939eae 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -10,8 +10,8 @@ op functionsType; char* formular; double inputNumbers[10]; int arrayLength; -void* child; -void* parent; +void* child[10]; // all children structs which depends on this struct +void* parent; // the parent struct which this struct depends on double result; }calc_op; @@ -21,5 +21,6 @@ extern void deleteWhitespace(char* formStr, int len); extern op detectFunctionOperator(char* formStr, int len); extern char* getNumbers(char* formStr, int len, calc_op* formRef); extern void showStruct(calc_op* formRef); +extern calc_op* getNextCalc(); #endif // INPUTHANDLING_H diff --git a/src/main.c b/src/main.c index 6a55347..ac2fea4 100644 --- a/src/main.c +++ b/src/main.c @@ -9,10 +9,15 @@ char inputBuffer[STRING_LENGTH] = {0}; void main() { - calc_op result; + calc_op *result = NULL; printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); fgets(inputBuffer, STRING_LENGTH, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace processInput(inputBuffer, strlen(inputBuffer)); - result.result = 65478; - showResult(&result); + result = getNextCalc(); + if (result!=NULL){ + /* Calculation*/ + result->result = 65478; + showResult(result); + } + } From 99e7331390397b3a760454f13ca207db659490f1 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 19:17:33 +0100 Subject: [PATCH 40/41] refactoring: Change struct variable name --- src/inputHandling.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inputHandling.h b/src/inputHandling.h index 9939eae..1772592 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -10,7 +10,7 @@ op functionsType; char* formular; double inputNumbers[10]; int arrayLength; -void* child[10]; // all children structs which depends on this struct +void* children[10]; // all children structs which depends on this struct void* parent; // the parent struct which this struct depends on double result; }calc_op; From 72e91d4f583f46ccdfab5c55ae7ca2c3bd287543 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sun, 28 Jan 2024 20:26:56 +0100 Subject: [PATCH 41/41] Update next calc function --- src/inputHandling.c | 39 +++++++++++++++++++++++++++++---------- src/inputHandling.h | 2 +- src/main.c | 17 ++++++++++++----- test/test_inputHandling.c | 15 +++++++++++++++ 4 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index e9d8217..e202cd7 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -5,30 +5,49 @@ char formulaBuffer[1000]; -calc_op* mainCalc = NULL; +calc_op* resultCalc = NULL; calc_op* currentCalc = NULL; void processInput(char* formStr, int len) { deleteWhitespace(formStr, len); - if (mainCalc!=NULL){ - free(mainCalc); + if (resultCalc != NULL){ + free(resultCalc); } - mainCalc = malloc(sizeof(calc_op)); + resultCalc = malloc(sizeof(calc_op)); + memset(resultCalc, 0, sizeof(calc_op)); + resultCalc->functionsType = opResult; memcpy(formulaBuffer, formStr, len); - mainCalc->formular = formStr; - mainCalc->functionsType = detectFunctionOperator(formulaBuffer, 10); - if (getNumbers(formulaBuffer, len, mainCalc) == NULL){ - showStruct(mainCalc); + calc_op * nextCalc = NULL; + nextCalc= malloc(sizeof(calc_op)); + memset(nextCalc, 0, sizeof(calc_op)); + nextCalc->formular = formStr; + nextCalc->parent = (void*) resultCalc; + nextCalc->functionsType = detectFunctionOperator(formulaBuffer, 10); + if (getNumbers(formulaBuffer, len, nextCalc) == NULL){ + resultCalc->children[0] = (void*) nextCalc; + showStruct(nextCalc); } else { - printf("Formular %s not supported", mainCalc->formular); + printf("Formular %s not supported", resultCalc->formular); } } calc_op* getNextCalc(){ + calc_op* newCalc = NULL; if (currentCalc != NULL){ + newCalc = (calc_op*) currentCalc->parent; + if (newCalc == NULL) { + return NULL; + } + newCalc->inputNumbers[0]= currentCalc->result; free(currentCalc); + } else { + newCalc = (calc_op*) resultCalc->children[0]; + } + if (newCalc==NULL){ + return NULL; } - currentCalc = mainCalc; // get ext calculation + currentCalc = newCalc; // get ext calculation + showStruct(currentCalc); return currentCalc; } diff --git a/src/inputHandling.h b/src/inputHandling.h index 1772592..76b969f 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -2,7 +2,7 @@ #define INPUTHANDLING_H typedef enum{ - opAdd, opSub, opDiv, opMult, opExp, opLog, opEmpty, opNotSupported + opAdd, opSub, opDiv, opMult, opExp, opLog, opEmpty, opNotSupported, opResult }op; typedef struct { diff --git a/src/main.c b/src/main.c index ac2fea4..0feff4c 100644 --- a/src/main.c +++ b/src/main.c @@ -13,11 +13,18 @@ void main() { printf("Geben Sie eine Rechenoperation ein (Bsp.: 1+1):\n"); fgets(inputBuffer, STRING_LENGTH, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace processInput(inputBuffer, strlen(inputBuffer)); - result = getNextCalc(); - if (result!=NULL){ + do { + result = getNextCalc(); + if (result == NULL) { + break; + } + if (result->functionsType==opResult) { + result->result = result->inputNumbers[0]; + showResult(result); + break; + } /* Calculation*/ result->result = 65478; - showResult(result); - } - + } while (1); + result = getNextCalc(); } diff --git a/test/test_inputHandling.c b/test/test_inputHandling.c index 404d840..c8c9665 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -149,4 +149,19 @@ void test_inputHandling_getNumbersDivFormular(void) TEST_ASSERT_EQUAL_DOUBLE(5.0, formula.inputNumbers[1]); TEST_ASSERT_EQUAL_INT(2, formula.arrayLength); } + +void test_inputHandling_getNextCalc(void) +{ + calc_op* pnt = NULL; + char add[] = "4+5"; + processInput(add, 3); + pnt = getNextCalc(); + TEST_ASSERT_NOT_NULL(pnt); + TEST_ASSERT_TRUE(pnt->functionsType == opAdd); + pnt = getNextCalc(); + TEST_ASSERT_NOT_NULL(pnt); + TEST_ASSERT_TRUE(pnt->functionsType == opResult); + pnt = getNextCalc(); + TEST_ASSERT_NULL(pnt); +} #endif // TEST