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/src/inputHandling.c b/src/inputHandling.c index efa40c9..e202cd7 100644 --- a/src/inputHandling.c +++ b/src/inputHandling.c @@ -2,57 +2,89 @@ #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 printstruct(calc_op* formula); +char formulaBuffer[1000]; +calc_op* resultCalc = NULL; +calc_op* currentCalc = NULL; -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(); - calc_op temp; - temp.formel = a; - temp.funktionstyp = readFunction(a, 10); - getnumbers(a,STRINGL, &temp); - printstruct(&temp); +void processInput(char* formStr, int len) { + deleteWhitespace(formStr, len); + if (resultCalc != NULL){ + free(resultCalc); + } + resultCalc = malloc(sizeof(calc_op)); + memset(resultCalc, 0, sizeof(calc_op)); + resultCalc->functionsType = opResult; + memcpy(formulaBuffer, formStr, len); + 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", 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 = newCalc; // get ext calculation + showStruct(currentCalc); + return currentCalc; } //Leerzeichen löschen -void deleteWhitespace(){ - for(int i=0; ifunktionstyp) { + switch (formRef->functionsType) { case opAdd: delimiter = '+'; break; @@ -65,21 +97,28 @@ 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, formStr, len); //string kopiert + char *token = strtok(formStr, &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++; + formRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert + numPos++; + 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 + 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; + } else { + return NULL; + } } -void printstruct(calc_op* formula){ - printf("Berechnung: %s", formula->formel); - switch (formula->funktionstyp) { +void showStruct(calc_op* formRef){ + printf("\nBerechnung: %s\n", formRef->formular); + switch (formRef->functionsType) { case opAdd: printf("Rechenoperation: Addition\n"); break; case opSub: @@ -92,8 +131,8 @@ void printstruct(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 arrayCount = 0; arrayCount < formRef->arrayLength; ++arrayCount) { + printf("Array[%i] = %f\n", arrayCount, formRef->inputNumbers[arrayCount]); } - printf("Result: %f", formula->result); + printf("Result: %f\n", formRef->result); } \ No newline at end of file diff --git a/src/inputHandling.h b/src/inputHandling.h index 676b08d..76b969f 100644 --- a/src/inputHandling.h +++ b/src/inputHandling.h @@ -2,20 +2,25 @@ #define INPUTHANDLING_H typedef enum{ - opAdd, opSub, opDiv, opMult, opExp, opLog, opNotSupported + opAdd, opSub, opDiv, opMult, opExp, opLog, opEmpty, opNotSupported, opResult }op; typedef struct { -op funktionstyp; -char* formel; -double array[10]; -int arraylength; -void* child; -void* parent; +op functionsType; +char* formular; +double inputNumbers[10]; +int arrayLength; +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; -extern void input(); - +extern void processInput(char* formStr, int len); +extern void showStruct(calc_op* formRef); +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 1bca401..0feff4c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,30 @@ #include "helloWorld.h" #include "inputHandling.h" +#include "outputHandling.h" +#include +#include +#include +#define STRING_LENGTH 200 + +char inputBuffer[STRING_LENGTH] = {0}; void main() { - input(); + 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)); + do { + result = getNextCalc(); + if (result == NULL) { + break; + } + if (result->functionsType==opResult) { + result->result = result->inputNumbers[0]; + showResult(result); + break; + } + /* Calculation*/ + result->result = 65478; + } while (1); + result = getNextCalc(); } diff --git a/src/outputHandling.c b/src/outputHandling.c new file mode 100644 index 0000000..450b72b --- /dev/null +++ b/src/outputHandling.c @@ -0,0 +1,22 @@ +#include +#include "outputHandling.h" +#include "inputHandling.h" + +void buildHexString(char* string, int num) { + sprintf(string, "0x%X", num); +} + +void buildOctString(char* string, int num) { + sprintf(string, "%o", num); +} + + +void showResult(calc_op* res) { + char string[60] = {0}; + 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) 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 new file mode 100644 index 0000000..77ff505 --- /dev/null +++ b/src/outputHandling.h @@ -0,0 +1,10 @@ +#ifndef OUTPUTHANDLING_H +#define OUTPUTHANDLING_H + +#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_inputHandling.c b/test/test_inputHandling.c index 157270f..c8c9665 100644 --- a/test/test_inputHandling.c +++ b/test/test_inputHandling.c @@ -3,18 +3,165 @@ #include "unity.h" #include "inputHandling.h" +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) { } -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); } +void test_inputHandling_deleteTwoWhiteSpaces(void) +{ + deleteWhitespace(halloWelt2, 11); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt2); +} + +void test_inputHandling_deleteManyWhiteSpaces(void) +{ + deleteWhitespace(halloWelt3, 16); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt3); +} + +void test_inputHandling_deleteAllOtherCharacter(void) +{ + deleteWhitespace(halloWelt4, 19); + TEST_ASSERT_EQUAL_STRING("HalloWelt", halloWelt4); +} + +void test_inputHandling_findAddFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4+5", 3); + TEST_ASSERT_TRUE(opAdd == type); +} + +void test_inputHandling_findSubFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4-5", 3); + TEST_ASSERT_TRUE(opSub == type); +} + +void test_inputHandling_findMultiFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4*5", 3); + 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); +} + +void test_inputHandling_findExpFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator("4^5", 3); + TEST_ASSERT_TRUE(opExp == type); +} + +void test_inputHandling_findEmptyFunctionType(void) +{ + op type = opNotSupported; + type = detectFunctionOperator(halloWelt, 10); + TEST_ASSERT_TRUE(opEmpty == type); +} + +void test_inputHandling_getNumbersNoFormular(void) +{ + char* pnt = NULL; + 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); + TEST_ASSERT_TRUE(formula.functionsType == opAdd); + 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); +} + +void test_inputHandling_getNumbersSubFormular(void) +{ + char* pnt = NULL; + 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]); + 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); +} + +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 diff --git a/test/test_outputHandling.c b/test/test_outputHandling.c new file mode 100644 index 0000000..13b77bb --- /dev/null +++ b/test/test_outputHandling.c @@ -0,0 +1,29 @@ +#ifdef TEST + +#include "unity.h" + +#include "outputHandling.h" + +char string[100]= {0}; + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_outputHandling_buildOctString(void) +{ + buildOctString(string, 5555555); + TEST_ASSERT_EQUAL_STRING("25142543", string); +} + +void test_outputHandling_buildHexString(void) +{ + buildHexString(string, 5555555); + TEST_ASSERT_EQUAL_STRING("0x54C563", string); +} + +#endif // TEST