|
|
@ -5,17 +5,17 @@ |
|
|
|
|
|
|
|
|
|
|
|
char data[1000]; |
|
|
|
void deleteWhitespace(char* a, int length); |
|
|
|
op readFunction(char* data, int length); |
|
|
|
char* getNumbers(char* data, int length, calc_op* structure_ref); |
|
|
|
void showStruct(calc_op* formula); |
|
|
|
void deleteWhitespace(char* formulaString, int length); |
|
|
|
op readFunction(char* formulaString, int length); |
|
|
|
char* getNumbers(char* formulaString, int length, calc_op* formulaRef); |
|
|
|
void showStruct(calc_op* formulaRef); |
|
|
|
|
|
|
|
|
|
|
|
void input(char* a, int length) { |
|
|
|
deleteWhitespace(a, length); |
|
|
|
void input(char* formulaString, int length) { |
|
|
|
deleteWhitespace(formulaString, length); |
|
|
|
calc_op temp; |
|
|
|
memcpy(data,a,length); |
|
|
|
temp.formel = a; |
|
|
|
memcpy(data, formulaString, length); |
|
|
|
temp.formel = formulaString; |
|
|
|
temp.funktionstyp = readFunction(data, 10); |
|
|
|
if (getNumbers(data, length, &temp) == NULL){ |
|
|
|
showStruct(&temp); |
|
|
@ -25,20 +25,20 @@ void input(char* a, int length) { |
|
|
|
} |
|
|
|
|
|
|
|
//Leerzeichen löschen |
|
|
|
void deleteWhitespace(char* a, int length){ |
|
|
|
void deleteWhitespace(char* formulaString, int length){ |
|
|
|
for(int i=0; i<length; i++){ |
|
|
|
if((a[i]==' ')||(a[i]=='\n')||(a[i]=='\r')){ |
|
|
|
if((formulaString[i] == ' ') || (formulaString[i] == '\n') || (formulaString[i] == '\r')){ |
|
|
|
for (int j=i; j<length; j++){ |
|
|
|
a[j]=a[j+1]; |
|
|
|
formulaString[j]=formulaString[j + 1]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Einfachste Rechenoperationen lesen |
|
|
|
op readFunction(char* data, int length){ |
|
|
|
op readFunction(char* formulaString, int length){ |
|
|
|
for(int i=0; i<length; i++){ |
|
|
|
switch (data[i]){ |
|
|
|
switch (formulaString[i]){ |
|
|
|
case '+': return opAdd; |
|
|
|
case '-': return opSub; |
|
|
|
case '/': return opDiv; |
|
|
@ -51,12 +51,12 @@ op readFunction(char* data, int length){ |
|
|
|
} |
|
|
|
|
|
|
|
//Zahlen auslesen (+) |
|
|
|
char* getNumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct |
|
|
|
char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //input sind: string, länge vom String, berechnungsstruct |
|
|
|
// char tmp[length]; |
|
|
|
char* splitPnt; |
|
|
|
int i = 0; |
|
|
|
char delimiter; |
|
|
|
switch (structure_ref->funktionstyp) { |
|
|
|
switch (formulaRef->funktionstyp) { |
|
|
|
case opAdd: |
|
|
|
delimiter = '+'; |
|
|
|
break; |
|
|
@ -71,15 +71,15 @@ char* getNumbers(char* data, int length, calc_op* structure_ref){ //input sind: |
|
|
|
break; |
|
|
|
default: return NULL; |
|
|
|
} |
|
|
|
// memcpy(tmp, data, length); //string kopiert |
|
|
|
char *token = strtok(data, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt |
|
|
|
// 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) { |
|
|
|
structure_ref->array[i] = atof(token); // String zu double konvertiert |
|
|
|
formulaRef->array[i] = atof(token); // String zu double konvertiert |
|
|
|
i++; |
|
|
|
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 |
|
|
|
formulaRef->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert |
|
|
|
op type = readFunction(splitPnt, strlen(splitPnt)+1); |
|
|
|
if (type != opNotSupported && type != opEmpty){ |
|
|
|
return splitPnt; |
|
|
@ -88,9 +88,9 @@ char* getNumbers(char* data, int length, calc_op* structure_ref){ //input sind: |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void showStruct(calc_op* formula){ |
|
|
|
printf("Berechnung: %s\n", formula->formel); |
|
|
|
switch (formula->funktionstyp) { |
|
|
|
void showStruct(calc_op* formulaRef){ |
|
|
|
printf("Berechnung: %s\n", formulaRef->formel); |
|
|
|
switch (formulaRef->funktionstyp) { |
|
|
|
case opAdd: |
|
|
|
printf("Rechenoperation: Addition\n"); break; |
|
|
|
case opSub: |
|
|
@ -103,8 +103,8 @@ void showStruct(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 i = 0; i < formulaRef->arraylength; ++i) { |
|
|
|
printf("Array[%i] = %f\n", i, formulaRef->array[i]); |
|
|
|
} |
|
|
|
printf("Result: %f", formula->result); |
|
|
|
printf("Result: %f", formulaRef->result); |
|
|
|
} |