Source.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <math.h>
  8. int is_valid_double(const char* str) {
  9. char* endptr;
  10. strtod(str, &endptr);
  11. return *endptr == '\0' && endptr != str;
  12. }
  13. void write_results(const char* filename, double x1, double x2, double D, int root_count) {
  14. HANDLE hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  15. if (hFile == INVALID_HANDLE_VALUE) {
  16. MessageBox(NULL, "Îøèáêà îòêðûòèÿ ôàéëà äëÿ çàïèñè ðåçóëüòàòîâ.", "Îøèáêà", MB_OK | MB_ICONERROR);
  17. return;
  18. }
  19. char buffer[256];
  20. if (root_count == 2) {
  21. sprintf(buffer, "Êîðíè óðàâíåíèÿ: %.2f è %.2f\n", x1, x2);
  22. }
  23. else if (root_count == 1) {
  24. sprintf(buffer, "Êîðåíü óðàâíåíèÿ: %.2f\n", x1);
  25. }
  26. else {
  27. sprintf(buffer, "Íåò äåéñòâèòåëüíûõ êîðíåé.\n");
  28. }
  29. sprintf(buffer + strlen(buffer), "Äèñêðèìèíàíò: %.2f\n", D);
  30. DWORD written;
  31. WriteFile(hFile, buffer, strlen(buffer), &written, NULL);
  32. CloseHandle(hFile);
  33. }
  34. int main() {
  35. double a, b, c;
  36. char line[256];
  37. int count = 0;
  38. // Ñ÷èòûâàåì êîýôôèöèåíòû èç ôàéëà
  39. HANDLE hFile = CreateFile("test.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  40. if (hFile == INVALID_HANDLE_VALUE) {
  41. MessageBox(NULL, "Îøèáêà îòêðûòèÿ ôàéëà äëÿ ÷òåíèÿ êîýôôèöèåíòîâ.", "Îøèáêà", MB_OK | MB_ICONERROR);
  42. return 1;
  43. }
  44. DWORD bytesRead;
  45. while (ReadFile(hFile, line, sizeof(line) - 1, &bytesRead, NULL) && bytesRead > 0) {
  46. line[bytesRead] = '\0';
  47. char* token = strtok(line, " \n");
  48. while (token != NULL) {
  49. if (is_valid_double(token)) {
  50. if (count == 0) {
  51. a = strtod(token, NULL);
  52. }
  53. else if (count == 1) {
  54. b = strtod(token, NULL);
  55. }
  56. else if (count == 2) {
  57. c = strtod(token, NULL);
  58. }
  59. count++;
  60. }
  61. else {
  62. MessageBox(NULL, "Îøèáêà: Ââåäåíû íåêîððåêòíûå äàííûå.", "Îøèáêà", MB_OK | MB_ICONERROR);
  63. CloseHandle(hFile);
  64. return 1;
  65. }
  66. token = strtok(NULL, " \n");
  67. }
  68. }
  69. CloseHandle(hFile);
  70. // Ïðîâåðÿåì êîýôôèöèåíòû
  71. if (count < 3) {
  72. MessageBox(NULL, "Îøèáêà: Íå äîñòàòî÷íî êîýôôèöèåíòîâ.", "Îøèáêà", MB_OK | MB_ICONERROR);
  73. return 1;
  74. }
  75. if (a == 0) {
  76. MessageBox(NULL, "Îøèáêà: Êîýôôèöèåíò 'a' íå ìîæåò áûòü ðàâåí 0.", "Îøèáêà", MB_OK | MB_ICONERROR);
  77. return 1;
  78. }
  79. // Âû÷èñëÿåì äèñêðèìèíàíò
  80. double D = b * b - 4 * a * c;
  81. double x1, x2;
  82. int root_count = 0;
  83. if (D > 0) {
  84. x1 = (-b + sqrt(D)) / (2 * a);
  85. x2 = (-b - sqrt(D)) / (2 * a);
  86. root_count = 2;
  87. }
  88. else if (D == 0) {
  89. x1 = -b / (2 * a);
  90. root_count = 1;
  91. }
  92. // Çàïèñûâàåì ðåçóëüòàòû â ôàéë
  93. write_results("otvet.txt", x1, x2, D, root_count);
  94. MessageBox(NULL, "Ðåçóëüòàòû çàïèñàíû â ôàéë 'otvet.txt'.", "Ãîòîâî", MB_OK | MB_ICONINFORMATION);
  95. return 0;
  96. }