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.

105 lines
3.5 KiB

  1. #include "inputHandling.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. char formulaBuffer[1000];
  6. void processInput(char* formulaString, int length) {
  7. deleteWhitespace(formulaString, length);
  8. calc_op temp;
  9. memcpy(formulaBuffer, formulaString, length);
  10. temp.formel = formulaString;
  11. temp.funktionstyp = detectFunctionOperator(formulaBuffer, 10);
  12. if (getNumbers(formulaBuffer, length, &temp) == NULL){
  13. showStruct(&temp);
  14. } else {
  15. printf("Formular %s not supported", temp.formel);
  16. }
  17. }
  18. //Leerzeichen löschen
  19. void deleteWhitespace(char* formulaString, int length){
  20. for(int stringPos=0; stringPos < length; stringPos++){
  21. if((formulaString[stringPos] == ' ') || (formulaString[stringPos] == '\n') || (formulaString[stringPos] == '\r')){
  22. for (int j=stringPos; j < length; j++){
  23. formulaString[j]=formulaString[j + 1];
  24. }
  25. stringPos--;
  26. }
  27. }
  28. }
  29. //Einfachste Rechenoperationen lesen
  30. op detectFunctionOperator(char* formulaString, int length){
  31. for(int stringCount=0; stringCount < length; stringCount++){
  32. switch (formulaString[stringCount]){
  33. case '+': return opAdd;
  34. case '-': return opSub;
  35. case '/':case ':': return opDiv;
  36. case '*': return opMult;
  37. case '^': return opExp;
  38. case '\0': return opEmpty;
  39. }
  40. }
  41. return opNotSupported;
  42. }
  43. //Zahlen auslesen (+)
  44. char* getNumbers(char* formulaString, int length, calc_op* formulaRef){ //processInput sind: string, länge vom String, berechnungsstruct
  45. // char tmp[length];
  46. char* splitPnt;
  47. int numPos = 0;
  48. char delimiter;
  49. switch (formulaRef->funktionstyp) {
  50. case opAdd:
  51. delimiter = '+';
  52. break;
  53. case opSub:
  54. delimiter = '-';
  55. break;
  56. case opDiv:
  57. delimiter = '/';
  58. break;
  59. case opMult:
  60. delimiter = '*';
  61. break;
  62. default: return NULL;
  63. }
  64. // memcpy(tmp, formulaString, length); //string kopiert
  65. char *token = strtok(formulaString, &delimiter); //An der Stelle von dem ersten Plus wird ein NULL (Stringende) gesetzt
  66. while (token != NULL) {
  67. formulaRef->array[numPos] = atof(token); // String zu double konvertiert
  68. numPos++;
  69. splitPnt = token;
  70. token = strtok(NULL, "+"); //Sucht von der letzten Plus-Stelle an weiter
  71. }
  72. formulaRef->arraylength=numPos; //Länge des Arrays (also zu berechnende Zahlen) gespeichert
  73. op type = detectFunctionOperator(splitPnt, strlen(splitPnt) + 1);
  74. if (type != opNotSupported && type != opEmpty){
  75. return splitPnt;
  76. } else {
  77. return NULL;
  78. }
  79. }
  80. void showStruct(calc_op* formulaRef){
  81. printf("Berechnung: %s\n", formulaRef->formel);
  82. switch (formulaRef->funktionstyp) {
  83. case opAdd:
  84. printf("Rechenoperation: Addition\n"); break;
  85. case opSub:
  86. printf("Rechenoperation: Subtraktion\n"); break;
  87. case opMult:
  88. printf("Rechenoperation: Multiplikation\n"); break;
  89. case opDiv:
  90. printf("Rechenoperation: Division\n"); break;
  91. default:
  92. printf("Fehler bei Auswahl der Rechenoperationen \n");
  93. }
  94. printf("Calculation Variables:\n");
  95. for (int arrayCount = 0; arrayCount < formulaRef->arraylength; ++arrayCount) {
  96. printf("Array[%i] = %f\n", arrayCount, formulaRef->array[arrayCount]);
  97. }
  98. printf("Result: %f", formulaRef->result);
  99. }