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.

121 lines
3.0 KiB

  1. #include "operationHandler.h"
  2. #include <stdbool.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. // checking integer input as operation id
  8. bool checkOperationInput(int input) {
  9. switch (input) {
  10. case 4: case 3: case 2: case 1:
  11. return true;
  12. }
  13. return false;
  14. }
  15. // full input process
  16. int* evaluateInput(char* input, int operation) {
  17. // check if formatting is correct
  18. if(!containsTwoNumbers(input)) return NULL;
  19. int firstNumber = extractFirstNumber(input);
  20. // origin string "input" has been edited
  21. int secondNumber = atoi(input);
  22. int* result = malloc(sizeof(int));
  23. switch (operation) {
  24. case 1:
  25. *result = firstNumber + secondNumber;
  26. break;
  27. case 2:
  28. *result = firstNumber - secondNumber;
  29. break;
  30. case 3:
  31. *result = firstNumber * secondNumber;
  32. break;
  33. case 4:
  34. *result = firstNumber / secondNumber;
  35. break;
  36. }
  37. return result;
  38. }
  39. // formatting check of string
  40. bool containsTwoNumbers(const char* str) {
  41. int numbersCount = 0;
  42. bool hasSpace = false;
  43. bool isInNumber = false;
  44. for (int i = 0; str[i] != '\0'; i++) {
  45. if (isdigit(str[i])) {
  46. if (!isInNumber) {
  47. isInNumber = true;
  48. numbersCount++;
  49. }
  50. } else if (str[i] == ' ' && isInNumber) {
  51. hasSpace = true;
  52. isInNumber = false;
  53. } else if (str[i] == ' ' && !isInNumber) {
  54. hasSpace = false;
  55. } else {
  56. hasSpace = false;
  57. isInNumber = false;
  58. }
  59. }
  60. return (numbersCount == 2 && hasSpace);
  61. }
  62. // extracting of first number and removing from string
  63. int extractFirstNumber(char* str) {
  64. int number = 0;
  65. bool isInNumber = false;
  66. int endIndex = 0;
  67. for (int i = 0; str[i] != '\0'; i++) {
  68. if (isdigit(str[i])) {
  69. if (!isInNumber) {
  70. isInNumber = true;
  71. number = str[i] - '0';
  72. } else {
  73. number = number * 10 + (str[i] - '0');
  74. }
  75. } else if (str[i] == ' ' && isInNumber) {
  76. endIndex = i;
  77. break;
  78. } else {
  79. isInNumber = false;
  80. }
  81. }
  82. int index = 0;
  83. char temp[strlen(str)-endIndex+1];
  84. for(int i = endIndex+1; i < strlen(str); i++) {
  85. temp[index++] = str[i];
  86. }
  87. temp[index] = '\0';
  88. strcpy(str, temp);
  89. return number;
  90. }
  91. void outputInteger(int output) {
  92. outputTemplate();
  93. printf("Result: %d", output);
  94. }
  95. void outputLong(long output) {
  96. outputTemplate();
  97. printf("Result: %ld", output);
  98. }
  99. void outputDouble(double output) {
  100. outputTemplate();
  101. printf("Result: %lf", output);
  102. }
  103. void outputFloat(float output) {
  104. outputTemplate();
  105. printf("Result: %f", output);
  106. }
  107. void outputTemplate() {
  108. printf("###############################\n-> Calculator\n#################################");
  109. }