Browse Source

Update next calc function

remotes/origin/Input_Handling
Sophia Weber 11 months ago
parent
commit
72e91d4f58
  1. 39
      src/inputHandling.c
  2. 2
      src/inputHandling.h
  3. 17
      src/main.c
  4. 15
      test/test_inputHandling.c

39
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;
}

2
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 {

17
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();
}

15
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
Loading…
Cancel
Save