Source.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <math.h>
  7. int read_in_file(float* coefficient, FILE* file) {
  8. char** buffer = malloc(sizeof(char) * 3);
  9. for (size_t i = 0; i < 3; i++)
  10. {
  11. buffer[i] = malloc(sizeof(char) * 100);
  12. }
  13. if (fscanf(file, "%s %s %s", buffer[0], buffer[1], buffer[2]) == 3) {
  14. for (size_t i = 0; i < 3; i++)
  15. {
  16. if (!check_str(buffer[i])) {
  17. coefficient[i] = atof(buffer[i]);
  18. }
  19. else {
  20. printf("Íåêêîðåêòíûé ââîä");
  21. return 1;
  22. }
  23. }
  24. }
  25. else {
  26. printf("Íåêêîðåêòíûé ââîä. ôàéëå äîëæíî áûòü 3 êîýôôèöèåíòà");
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. int check_str(char* str) {
  32. int i = 0;
  33. int count_point = 0;
  34. if (str[0] == '.') {
  35. return 1;
  36. }
  37. if (str[0] == '-') {
  38. i++;
  39. }
  40. while (str[i] != '\0') {
  41. if ((str[i] >= 0x410 && str[i] <= 0x419) || !isdigit(str[i])) {
  42. if (str[i] == '.' && count_point <= 0) {
  43. count_point++;
  44. }
  45. else {
  46. return 1;
  47. }
  48. }
  49. i++;
  50. }
  51. return 0;
  52. }
  53. int count_coefficient(float* coefficient, float** coefficient_answer) {
  54. if (coefficient[1] == 0 && coefficient[2] == 0) {
  55. coefficient_answer[0] = 0;
  56. coefficient_answer[1] = 0;
  57. return 0;
  58. }
  59. else if (coefficient[1] == 0) {
  60. if (coefficient[2] * -1 / coefficient[0] < 0) {
  61. return 1;
  62. }
  63. else {
  64. (*coefficient_answer)[0] = sqrt(coefficient[2] * -1 / coefficient[0]);
  65. (*coefficient_answer)[1] = sqrt(coefficient[2] * -1 / coefficient[0]) * -1;
  66. return 0;
  67. }
  68. }
  69. else if (coefficient[2] == 0) {
  70. (*coefficient_answer)[0] = 0;
  71. (*coefficient_answer)[1] = coefficient[1] * -1 / coefficient[0];
  72. return 0;
  73. }
  74. else if (coefficient[0] == 0) {
  75. return 1;
  76. }
  77. else {
  78. float* temp_array = realloc((*coefficient_answer), sizeof(float) * 3);
  79. float D = pow(coefficient[1], 2) - 4 * coefficient[0] * coefficient[2];
  80. temp_array[0] = D;
  81. if (D < 0) {
  82. return 1;
  83. }
  84. else if (D == 0) {
  85. temp_array[1] = (coefficient[1] * -1) / (2 * coefficient[0]);
  86. *coefficient_answer = temp_array;
  87. return 2;
  88. }
  89. else {
  90. temp_array[1] = ((coefficient[1] * -1) + sqrt(D)) / (2 * coefficient[0]);
  91. temp_array[2] = ((coefficient[1] * -1) - sqrt(D)) / (2 * coefficient[0]);
  92. *coefficient_answer = temp_array;
  93. return 3;
  94. }
  95. }
  96. }
  97. int write_in_file(int result_code, float* coefficient_answer) {
  98. FILE* file_with_answer = fopen("answers.txt", "w");
  99. switch (result_code)
  100. {
  101. case 0:
  102. fprintf(file_with_answer, "Äàííîå óðàâíåíèå áûëî ðåøåíî áåç äèñêðèìèíàíòà\n");
  103. fprintf(file_with_answer, "Ïåðâûé êîðåíü - %.4f\nÂòîðîé êîðåíü - %.4f\n", coefficient_answer[0], coefficient_answer[1]);
  104. fclose(file_with_answer);
  105. break;
  106. case 1:
  107. fprintf(file_with_answer, "Óðàâíåíèå íå èìååò êîðíåé");
  108. fclose(file_with_answer);
  109. break;
  110. case 2:
  111. fprintf(file_with_answer, "äèñêðèìèíàíòà ðàâåí %.4f, ïîýòîìó ó óðàâíåíèÿ îäèí êîðåíü\nÊîðåíü - %.4f", coefficient_answer[0], coefficient_answer[1]);
  112. fclose(file_with_answer);
  113. break;
  114. case 3:
  115. fprintf(file_with_answer, "äèñêðèìèíàíòà ðàâåí %.4f\nÏåðâûé êîðåíü - %.4f\nÂòîðîé êîðåíü - %.4f", coefficient_answer[0], coefficient_answer[1],coefficient_answer[2]);
  116. fclose(file_with_answer);
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. int main() {
  123. system("chcp 1251>null");
  124. FILE* file = fopen("coefficient.txt", "r");
  125. float* coefficient = malloc(sizeof(float) * 3);
  126. if (read_in_file(coefficient, file)) {
  127. return 1;
  128. }
  129. fclose(file);
  130. float* coefficient_answer = malloc(sizeof(float) * 2);
  131. int code = count_coefficient(coefficient, &coefficient_answer);
  132. write_in_file(code, coefficient_answer);
  133. }