Browse Source

refactoring: unambiguous function variablenames

remotes/origin/Input_Handling
Sophia Weber 11 months ago
parent
commit
92288f9a8c
  1. 50
      src/inputHandling.c
  2. 4
      src/inputHandling.h

50
src/inputHandling.c

@ -5,17 +5,17 @@
char data[1000]; 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; calc_op temp;
memcpy(data,a,length);
temp.formel = a;
memcpy(data, formulaString, length);
temp.formel = formulaString;
temp.funktionstyp = readFunction(data, 10); temp.funktionstyp = readFunction(data, 10);
if (getNumbers(data, length, &temp) == NULL){ if (getNumbers(data, length, &temp) == NULL){
showStruct(&temp); showStruct(&temp);
@ -25,20 +25,20 @@ void input(char* a, int length) {
} }
//Leerzeichen löschen //Leerzeichen löschen
void deleteWhitespace(char* a, int length){
void deleteWhitespace(char* formulaString, int length){
for(int i=0; i<length; i++){ 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++){ for (int j=i; j<length; j++){
a[j]=a[j+1];
formulaString[j]=formulaString[j + 1];
} }
} }
} }
} }
//Einfachste Rechenoperationen lesen //Einfachste Rechenoperationen lesen
op readFunction(char* data, int length){
op readFunction(char* formulaString, int length){
for(int i=0; i<length; i++){ for(int i=0; i<length; i++){
switch (data[i]){
switch (formulaString[i]){
case '+': return opAdd; case '+': return opAdd;
case '-': return opSub; case '-': return opSub;
case '/': return opDiv; case '/': return opDiv;
@ -51,12 +51,12 @@ op readFunction(char* data, int length){
} }
//Zahlen auslesen (+) //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 tmp[length];
char* splitPnt; char* splitPnt;
int i = 0; int i = 0;
char delimiter; char delimiter;
switch (structure_ref->funktionstyp) {
switch (formulaRef->funktionstyp) {
case opAdd: case opAdd:
delimiter = '+'; delimiter = '+';
break; break;
@ -71,15 +71,15 @@ char* getNumbers(char* data, int length, calc_op* structure_ref){ //input sind:
break; break;
default: return NULL; 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) { while (token != NULL) {
structure_ref->array[i] = atof(token); // String zu double konvertiert
formulaRef->array[i] = atof(token); // String zu double konvertiert
i++; i++;
splitPnt = token; splitPnt = token;
token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter 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); op type = readFunction(splitPnt, strlen(splitPnt)+1);
if (type != opNotSupported && type != opEmpty){ if (type != opNotSupported && type != opEmpty){
return splitPnt; 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: case opAdd:
printf("Rechenoperation: Addition\n"); break; printf("Rechenoperation: Addition\n"); break;
case opSub: case opSub:
@ -103,8 +103,8 @@ void showStruct(calc_op* formula){
printf("Fehler bei Auswahl der Rechenoperationen \n"); printf("Fehler bei Auswahl der Rechenoperationen \n");
} }
printf("Calculation Variables:\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);
} }

4
src/inputHandling.h

@ -15,7 +15,7 @@ void* parent;
double result; double result;
}calc_op; }calc_op;
extern void input(char* a, int length);
extern void showStruct(calc_op* formula);
extern void input(char* formulaString, int length);
extern void showStruct(calc_op* formulaRef);
#endif // INPUTHANDLING_H #endif // INPUTHANDLING_H
Loading…
Cancel
Save