|
|
@ -1,15 +1,25 @@ |
|
|
|
#include "inputHandling.h" |
|
|
|
#include <string.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#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); |
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
printf("Ihre Berechnung: %s \n", a); |
|
|
|
calc_op temp; |
|
|
|
temp.formel = a; |
|
|
|
temp.funktionstyp = readFunction(a, 10); |
|
|
|
getnumbers(a,STRINGL, &temp); |
|
|
|
printstruct(&temp); |
|
|
|
} |
|
|
|
|
|
|
|
//Leerzeichen löschen |
|
|
@ -22,3 +32,68 @@ void deleteWhitespace(){ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Einfachste Rechenoperationen lesen |
|
|
|
op readFunction(char* data, int length){ |
|
|
|
for(int i=0; i<length; i++){ |
|
|
|
switch (data[i]){ |
|
|
|
case '+': return opAdd; |
|
|
|
case '-': return opSub; |
|
|
|
case '/': return opDiv; |
|
|
|
case '*': return opMult; |
|
|
|
case '^': return opExp; |
|
|
|
} |
|
|
|
} |
|
|
|
return opNotSupported; |
|
|
|
} |
|
|
|
|
|
|
|
//Zahlen auslesen (+) |
|
|
|
void getnumbers(char* data, int length, calc_op* structure_ref){ //input sind: string, länge vom String, berechnungsstruct |
|
|
|
char tmp[length]; |
|
|
|
int i = 0; |
|
|
|
char delimiter; |
|
|
|
switch (structure_ref->funktionstyp) { |
|
|
|
case opAdd: |
|
|
|
delimiter = '+'; |
|
|
|
break; |
|
|
|
case opSub: |
|
|
|
delimiter = '-'; |
|
|
|
break; |
|
|
|
case opDiv: |
|
|
|
delimiter = '/'; |
|
|
|
break; |
|
|
|
case opMult: |
|
|
|
delimiter = '*'; |
|
|
|
break; |
|
|
|
default: return; |
|
|
|
} |
|
|
|
memcpy(tmp, data, length); //string kopiert |
|
|
|
char *token = strtok(tmp, &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++; |
|
|
|
token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter |
|
|
|
} |
|
|
|
structure_ref->arraylength=i; //Länge des Arrays (also zu berechnende Zahlen) gespeichert |
|
|
|
} |
|
|
|
|
|
|
|
void printstruct(calc_op* formula){ |
|
|
|
printf("Berechnung: %s", formula->formel); |
|
|
|
switch (formula->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 i = 0; i < formula->arraylength; ++i) { |
|
|
|
printf("Array[%i] = %f\n", i, formula->array[i]); |
|
|
|
} |
|
|
|
printf("Result: %f", formula->result); |
|
|
|
} |