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.

137 lines
4.3 KiB

  1. #include "inputHandling.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. char formulaBuffer[1000];
  6. calc_op* resultCalc = NULL;
  7. calc_op* currentCalc = NULL;
  8. void processInput(char* formStr, int len) {
  9. deleteWhitespace(formStr, len);
  10. if (resultCalc != NULL){
  11. free(resultCalc);
  12. }
  13. resultCalc = malloc(sizeof(calc_op));
  14. memset(resultCalc, 0, sizeof(calc_op));
  15. resultCalc->functionsType = opResult;
  16. memcpy(formulaBuffer, formStr, len);
  17. calc_op * nextCalc = NULL;
  18. nextCalc= malloc(sizeof(calc_op));
  19. memset(nextCalc, 0, sizeof(calc_op));
  20. nextCalc->formular = formStr;
  21. nextCalc->parent = (void*) resultCalc;
  22. nextCalc->functionsType = detectFunctionOperator(formulaBuffer, 10);
  23. if (getNumbers(formulaBuffer, len, nextCalc) == NULL){
  24. resultCalc->children[0] = (void*) nextCalc;
  25. showStruct(nextCalc);
  26. } else {
  27. printf("Formular %s not supported", resultCalc->formular);
  28. }
  29. }
  30. calc_op* getNextCalc(){
  31. calc_op* newCalc = NULL;
  32. if (currentCalc != NULL){
  33. newCalc = (calc_op*) currentCalc->parent;
  34. if (newCalc == NULL) {
  35. return NULL;
  36. }
  37. newCalc->inputNumbers[0]= currentCalc->result;
  38. free(currentCalc);
  39. } else {
  40. newCalc = (calc_op*) resultCalc->children[0];
  41. }
  42. if (newCalc==NULL){
  43. return NULL;
  44. }
  45. currentCalc = newCalc; // get ext calculation
  46. showStruct(currentCalc);
  47. return currentCalc;
  48. }
  49. //Leerzeichen löschen
  50. void deleteWhitespace(char* formStr, int len){
  51. for(int stringPos=0; stringPos < len; stringPos++){
  52. if((formStr[stringPos] == ' ') || (formStr[stringPos] == '\n') || (formStr[stringPos] == '\r')){
  53. for (int j=stringPos; j < len; j++){
  54. formStr[j]=formStr[j + 1];
  55. }
  56. stringPos--;
  57. }
  58. }
  59. }
  60. //Einfachste Rechenoperationen lesen
  61. op detectFunctionOperator(char* formStr, int len){
  62. for(int stringCount=0; stringCount < len; stringCount++){
  63. switch (formStr[stringCount]){
  64. case '+': return opAdd;
  65. case '-': return opSub;
  66. case '/':case ':': return opDiv;
  67. case '*': return opMult;
  68. case '^': return opExp;
  69. case '\0': return opEmpty;
  70. }
  71. }
  72. return opNotSupported;
  73. }
  74. //Zahlen auslesen (+)
  75. char* getNumbers(char* formStr, int len, calc_op* formRef){ //processInput sind: string, länge vom String, berechnungsstruct
  76. // char tmp[len];
  77. char* splitPnt;
  78. int numPos = 0;
  79. char delimiter;
  80. switch (formRef->functionsType) {
  81. case opAdd:
  82. delimiter = '+';
  83. break;
  84. case opSub:
  85. delimiter = '-';
  86. break;
  87. case opDiv:
  88. delimiter = '/';
  89. break;
  90. case opMult:
  91. delimiter = '*';
  92. break;
  93. default: return NULL;
  94. }
  95. // memcpy(tmp, formStr, len); //string kopiert
  96. char *token = strtok(formStr, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt
  97. while (token != NULL) {
  98. formRef->inputNumbers[numPos] = atof(token); // String zu double konvertiert
  99. numPos++;
  100. splitPnt = token;
  101. token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter
  102. }
  103. formRef->arrayLength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert
  104. op type = detectFunctionOperator(splitPnt, strlen(splitPnt) + 1);
  105. if (type != opNotSupported && type != opEmpty){
  106. return splitPnt;
  107. } else {
  108. return NULL;
  109. }
  110. }
  111. void showStruct(calc_op* formRef){
  112. printf("\nBerechnung: %s\n", formRef->formular);
  113. switch (formRef->functionsType) {
  114. case opAdd:
  115. printf("Rechenoperation: Addition\n"); break;
  116. case opSub:
  117. printf("Rechenoperation: Subtraktion\n"); break;
  118. case opMult:
  119. printf("Rechenoperation: Multiplikation\n"); break;
  120. case opDiv:
  121. printf("Rechenoperation: Division\n"); break;
  122. default:
  123. printf("Fehler bei Auswahl der Rechenoperationen \n");
  124. }
  125. printf("Calculation Variables:\n");
  126. for (int arrayCount = 0; arrayCount < formRef->arrayLength; ++arrayCount) {
  127. printf("Array[%i] = %f\n", arrayCount, formRef->inputNumbers[arrayCount]);
  128. }
  129. printf("Result: %f\n", formRef->result);
  130. }