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