#include "inputHandling.h" #include #include #include char formulaBuffer[1000]; void processInput(char* formulaString, int length) { deleteWhitespace(formulaString, length); calc_op temp; memcpy(formulaBuffer, formulaString, length); temp.formel = formulaString; temp.funktionstyp = detectFunctionOperator(formulaBuffer, 10); if (getNumbers(formulaBuffer, length, &temp) == NULL){ showStruct(&temp); } else { printf("Formular %s not supported", temp.formel); } } //Leerzeichen löschen void deleteWhitespace(char* formulaString, int length){ for(int stringPos=0; stringPos < length; stringPos++){ if((formulaString[stringPos] == ' ') || (formulaString[stringPos] == '\n') || (formulaString[stringPos] == '\r')){ for (int j=stringPos; j < length; j++){ formulaString[j]=formulaString[j + 1]; } stringPos--; } } } //Einfachste Rechenoperationen lesen op detectFunctionOperator(char* formulaString, int length){ for(int stringCount=0; stringCount < length; stringCount++){ switch (formulaString[stringCount]){ case '+': return opAdd; case '-': return opSub; case '/':case ':': return opDiv; case '*': return opMult; case '^': return opExp; case '\0': return opEmpty; } } return opNotSupported; } //Zahlen auslesen (+) 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; char delimiter; switch (formulaRef->funktionstyp) { case opAdd: delimiter = '+'; break; case opSub: delimiter = '-'; break; case opDiv: delimiter = '/'; break; case opMult: delimiter = '*'; 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 while (token != NULL) { 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=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 showStruct(calc_op* formulaRef){ printf("Berechnung: %s\n", formulaRef->formel); switch (formulaRef->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"); } printf("Calculation Variables:\n"); for (int arrayCount = 0; arrayCount < formulaRef->arraylength; ++arrayCount) { printf("Array[%i] = %f\n", arrayCount, formulaRef->array[arrayCount]); } printf("Result: %f", formulaRef->result); }