You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

138 lines
4.3 KiB

#include "inputHandling.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char formulaBuffer[1000];
calc_op* resultCalc = NULL;
calc_op* currentCalc = NULL;
void processInput(char* formStr, int len) {
deleteWhitespace(formStr, len);
if (resultCalc != NULL){
free(resultCalc);
}
resultCalc = malloc(sizeof(calc_op));
memset(resultCalc, 0, sizeof(calc_op));
resultCalc->functionsType = opResult;
memcpy(formulaBuffer, formStr, len);
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", 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 = newCalc; // get ext calculation
showStruct(currentCalc);
return currentCalc;
}
//Leerzeichen löschen
void deleteWhitespace(char* formStr, int len){
for(int stringPos=0; stringPos < len; stringPos++){
if((formStr[stringPos] == ' ') || (formStr[stringPos] == '\n') || (formStr[stringPos] == '\r')){
for (int j=stringPos; j < len; j++){
formStr[j]=formStr[j + 1];
}
stringPos--;
}
}
}
//Einfachste Rechenoperationen lesen
op detectFunctionOperator(char* formStr, int len){
for(int stringCount=0; stringCount < len; stringCount++){
switch (formStr[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* formStr, int len, calc_op* formRef){ //processInput sind: string, länge vom String, berechnungsstruct
// char tmp[len];
char* splitPnt;
int numPos = 0;
char delimiter;
switch (formRef->functionsType) {
case opAdd:
delimiter = '+';
break;
case opSub:
delimiter = '-';
break;
case opDiv:
delimiter = '/';
break;
case opMult:
delimiter = '*';
break;
default: return NULL;
}
// memcpy(tmp, formStr, len); //string kopiert
char *token = strtok(formStr, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt
while (token != NULL) {
formRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert
numPos++;
splitPnt = token;
token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter
}
formRef->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* formRef){
printf("\nBerechnung: %s\n", formRef->formular);
switch (formRef->functionsType) {
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 < formRef->arrayLength; ++arrayCount) {
printf("Array[%i] = %f\n", arrayCount, formRef->inputNumbers[arrayCount]);
}
printf("Result: %f\n", formRef->result);
}