From 37fc481970f1f82b56e9e4595cf2dda987d5e56a Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 15:07:05 +0100 Subject: [PATCH 1/7] Enum and struct for calculation in inputHandling.h --- src/inputHandling.c | 2 ++ src/inputHandling.h | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/inputHandling.c b/src/inputHandling.c index 6e8615d..8388c59 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -22,3 +22,5 @@ void deleteWhitespace(){ } } } + + diff --git a/src/inputHandling.h b/src/inputHandling.h index b73a64c..9e9a011 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -1,6 +1,19 @@ #ifndef INPUTHANDLING_H #define INPUTHANDLING_H +typedef enum{ + opAdd, opSub, opDiv, opMult, opExp, opLog +}op; + +typedef struct { +op funktionstyp; +char* formel; +double array[10]; +void* child; +double result; +}calc_op; + extern void input(); + #endif // INPUTHANDLING_H From 53f5b6af7ed38c859ded6df23bbafa81e5c8d1ca Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 15:35:23 +0100 Subject: [PATCH 2/7] Created Function to find calculation function type --- src/inputHandling.c | 16 +++++++++++++++- src/inputHandling.h | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 8388c59..9c22b86 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -4,11 +4,13 @@ char a[STRINGL]; void deleteWhitespace(); +op readFunction(char* data, int length); 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(); + op b = readFunction(a, 10); printf("Ihre Berechnung: %s \n", a); } @@ -23,4 +25,16 @@ void deleteWhitespace(){ } } - +//Einfachste Rechenoperationen lesen +op readFunction(char* data, int length){ + for(int i=0; i Date: Sat, 27 Jan 2024 16:12:05 +0100 Subject: [PATCH 3/7] Extract numbers from formula --- src/inputHandling.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 9c22b86..63cbfcc 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -1,16 +1,21 @@ #include "inputHandling.h" +#include #include +#include #define STRINGL 200 char a[STRINGL]; void deleteWhitespace(); op readFunction(char* data, int length); +void getnumbers(char* data, int length, calc_op* structure_ref); 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(); - op b = readFunction(a, 10); + calc_op temp; + temp.funktionstyp = readFunction(a, 10); + getnumbers(a,STRINGL, &temp); printf("Ihre Berechnung: %s \n", a); } @@ -38,3 +43,16 @@ op readFunction(char* data, int length){ } return opNotSupported; } + +//Zahlen auslesen (+) +void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct + char tmp[length]; + int i = 0; + memcpy(tmp, data, length); //string kopiert + char *token = strtok(tmp, "+"); //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 + i++; + token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter + } +} \ No newline at end of file From 8b8ec2a3829614a04ed205dae4e5fe80e50b5ca6 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 16:25:36 +0100 Subject: [PATCH 4/7] Extending formula extractor --- src/inputHandling.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 63cbfcc..4f081b3 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -48,8 +48,24 @@ op readFunction(char* data, int length){ void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct char tmp[length]; int i = 0; + char delimiter; + switch (structure_ref->funktionstyp) { + case opAdd: + delimiter = '+'; + break; + case opSub: + delimiter = '-'; + break; + case opDiv: + delimiter = '/'; + break; + case opMult: + delimiter = '*'; + break; + default: return; + } memcpy(tmp, data, length); //string kopiert - char *token = strtok(tmp, "+"); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt + char *token = strtok(tmp, &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 i++; From 6dd55ee91228c743d9428b5516aa15f35b4716ac Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 16:34:00 +0100 Subject: [PATCH 5/7] Added Arraylength to struct --- src/inputHandling.c | 1 + src/inputHandling.h | 1 + 2 files changed, 2 insertions(+) diff --git a/src/inputHandling.c b/src/inputHandling.c index 4f081b3..4d4c736 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -71,4 +71,5 @@ void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: s i++; token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } + structure_ref->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert } \ No newline at end of file diff --git a/src/inputHandling.h b/src/inputHandling.h index 2127896..a2d8614 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -9,6 +9,7 @@ typedef struct { op funktionstyp; char* formel; double array[10]; +int arraylength; void* child; double result; }calc_op; From 55ac78c1f15145c36f07a94e7242aea72ac35e00 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 16:49:59 +0100 Subject: [PATCH 6/7] Added function printstruct --- src/inputHandling.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/inputHandling.c b/src/inputHandling.c index 4d4c736..58446ad 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -8,6 +8,8 @@ char a[STRINGL]; void deleteWhitespace(); op readFunction(char* data, int length); void 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"); @@ -17,6 +19,7 @@ void input() { temp.funktionstyp = readFunction(a, 10); getnumbers(a,STRINGL, &temp); printf("Ihre Berechnung: %s \n", a); + printstruct(&temp); } //Leerzeichen löschen @@ -72,4 +75,19 @@ void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: s token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter } structure_ref->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert +} + +void printstruct(calc_op* formula){ + switch (formula->funktionstyp) { + case opAdd: + printf("Rechenoperation: Addition\n"); break; + case opSub: + printf("Rechenoperation: Subtraktion\n"); break; + case opMult: + printf("Rechenoperation: Multiplikation\n"); break; + case opDiv: + printf("Rechenoperation: Division\n"); break; + default: + printf("Fehler bei Auswahl der Rechenoperationen \n"); + } } \ No newline at end of file From b8e361c3ecb6c9f0f16a4841525c77da8e77a200 Mon Sep 17 00:00:00 2001 From: Sophia Weber Date: Sat, 27 Jan 2024 17:00:17 +0100 Subject: [PATCH 7/7] add more print entries add parent pointer --- src/inputHandling.c | 8 +++++++- src/inputHandling.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/inputHandling.c b/src/inputHandling.c index 58446ad..efa40c9 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -16,9 +16,9 @@ void input() { fgets(a, STRINGL, stdin); //fgets statt scanf, holt den kompletten String inkl. Whitespace deleteWhitespace(); calc_op temp; + temp.formel = a; temp.funktionstyp = readFunction(a, 10); getnumbers(a,STRINGL, &temp); - printf("Ihre Berechnung: %s \n", a); printstruct(&temp); } @@ -78,6 +78,7 @@ void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: s } void printstruct(calc_op* formula){ + printf("Berechnung: %s", formula->formel); switch (formula->funktionstyp) { case opAdd: printf("Rechenoperation: Addition\n"); break; @@ -90,4 +91,9 @@ void printstruct(calc_op* formula){ default: 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]); + } + printf("Result: %f", formula->result); } \ No newline at end of file diff --git a/src/inputHandling.h b/src/inputHandling.h index a2d8614..676b08d 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -11,6 +11,7 @@ char* formel; double array[10]; int arraylength; void* child; +void* parent; double result; }calc_op;