Source.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <conio.h>
  6. int main(int argc, char* argv[]) {
  7. FILE* input_file, * output_file;
  8. double a, b, c, discriminant, x1, x2;
  9. int num_roots;
  10. system("chcp 1251 > nul");
  11. // Îòêðûâàåì ôàéë äëÿ ÷òåíèÿ êîýôôèöèåíòîâ
  12. input_file = fopen("coefficients.txt", "r");
  13. if (!input_file) {
  14. fprintf(stderr, "Îøèáêà îòêðûòèÿ ôàéëà coefficients.txt\n");
  15. return 1;
  16. }
  17. // Ñ÷èòûâàåì êîýôôèöèåíòû
  18. if (fscanf(input_file, "%lf %lf %lf", &a, &b, &c) != 3) {
  19. fprintf(stderr, "Íåêîððåêòíûé ôîðìàò âõîäíûõ äàííûõ\n");
  20. fclose(input_file);
  21. return 1;
  22. }
  23. fclose(input_file);
  24. // Ïðîâåðÿåì êîððåêòíîñòü êîýôôèöèåíòîâ
  25. if (a == 0 && b == 0) {
  26. fprintf(stderr, "Íåâîçìîæíî ðåøèòü óðàâíåíèå, îáà êîýôôèöèåíòà a è b ðàâíû 0\n");
  27. return 1;
  28. }
  29. // Îòêðûâàåì ôàéë äëÿ çàïèñè ðåçóëüòàòîâ
  30. output_file = fopen("results.txt", "w");
  31. if (!output_file) {
  32. fprintf(stderr, "Îøèáêà îòêðûòèÿ ôàéëà results.txt\n");
  33. return 1;
  34. }
  35. // Âû÷èñëÿåì äèñêðèìèíàíò
  36. discriminant = b * b - 4 * a * c;
  37. // Îïðåäåëÿåì êîëè÷åñòâî êîðíåé
  38. if (discriminant > 0) {
  39. num_roots = 2;
  40. x1 = (-b + sqrt(discriminant)) / (2 * a);
  41. x2 = (-b - sqrt(discriminant)) / (2 * a);
  42. fprintf(output_file, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 2\nÊîðåíü 1: %.2f\nÊîðåíü 2: %.2f\n",
  43. discriminant, x1, x2);
  44. printf("Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 2\nÊîðåíü 1: %.2f\nÊîðåíü 2: %.2f\n",
  45. discriminant, x1, x2);
  46. }
  47. else if (discriminant == 0) {
  48. num_roots = 1;
  49. x1 = -b / (2 * a);
  50. fprintf(output_file, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 1\nÊîðåíü: %.2f\n",
  51. discriminant, x1);
  52. printf("Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 1\nÊîðåíü: %.2f\n",
  53. discriminant, x1);
  54. }
  55. else {
  56. num_roots = 0;
  57. fprintf(output_file, "Äèñêðèìèíàíò: %.2f\nÄåéñòâèòåëüíûõ êîðíåé íåò.\n",
  58. discriminant);
  59. printf("Äèñêðèìèíàíò: % .2f\nÄåéñòâèòåëüíûõ êîðíåé íåò.\n",
  60. discriminant);
  61. }
  62. fclose(output_file);
  63. printf("Ðåçóëüòàòû âû÷èñëåíèé ñîõðàíåíû â ôàéë results.txt\n");
  64. return 0;
  65. }