Source.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <locale.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. void writeToFile(HANDLE file, const char* data) {
  9. DWORD bytesWritten;
  10. WriteFile(file, data, strlen(data), &bytesWritten, NULL);
  11. }
  12. void readFromFile(HANDLE file, char* buffer, DWORD size) {
  13. DWORD bytesRead;
  14. ReadFile(file, buffer, size - 1, &bytesRead, NULL);
  15. buffer[bytesRead] = '\0'; // Çàâåðøåíèå ñòðîêè
  16. }
  17. int main() {
  18. setlocale(LC_ALL, "Russian");
  19. HANDLE file1 = CreateFile(L"coefOUT.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  20. HANDLE file = CreateFile(L"coef.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  21. if (file == INVALID_HANDLE_VALUE) {
  22. fprintf(stderr, "Îøèáêà îòêðûòèÿ ôàéëà coef.txt: %lu\n", GetLastError());
  23. return EXIT_FAILURE;
  24. }
  25. if (file1 == INVALID_HANDLE_VALUE) {
  26. fprintf(stderr, "Îøèáêà îòêðûòèÿ ôàéëà coefOUT.txt: %lu\n", GetLastError());
  27. CloseHandle(file);
  28. return EXIT_FAILURE;
  29. }
  30. char line[256];
  31. double coef[3];
  32. int coefCount = 0; // Ñ÷åò÷èê êîýôôèöèåíòîâ
  33. while (1) {
  34. readFromFile(file, line, sizeof(line)); // ×òåíèå ñòðîêè èç ôàéëà
  35. // Ïðîâåðêà íà êîíåö ôàéëà
  36. if (strlen(line) == 0) {
  37. break; // Åñëè ñòðîêà ïóñòàÿ, âûõîäèì èç öèêëà
  38. }
  39. char* token = strtok(line, " \n");
  40. // Îáðàáîòêà êîýôôèöèåíòîâ
  41. while (token != NULL) {
  42. coef[coefCount] = atof(token);
  43. coefCount++;
  44. // Åñëè ïðî÷èòàíî ðîâíî 3 êîýôôèöèåíòà, âû÷èñëÿåì êîðíè
  45. if (coefCount == 3) {
  46. double x1, x;
  47. double a = coef[0];
  48. double b = coef[1];
  49. double c = coef[2];
  50. char output[256];
  51. snprintf(output, sizeof(output), "Êîýôôèöèåíòû: %f %f %f\n", a, b, c);
  52. writeToFile(file1, output);
  53. double D = b * b - 4 * a * c;
  54. if (a == 0 && b == 0 && c == 0) {
  55. snprintf(output, sizeof(output), "Àðãóìåíòû íå ìîãóò áûòü îòðèöàòåëüíûìè è ðàâíÿòüñÿ íóëþ\n");
  56. writeToFile(file1, output);
  57. }
  58. else if (D > 0) {
  59. x1 = (-b + sqrt(D)) / (2 * a);
  60. x = (-b - sqrt(D)) / (2 * a);
  61. snprintf(output, sizeof(output), "Êîðíè: x1 = %f, x2 = %f\n", x1, x);
  62. writeToFile(file1, output);
  63. }
  64. else if (D == 0) {
  65. x = -b / (2 * a);
  66. snprintf(output, sizeof(output), "Êîðåíü: %f\n", x);
  67. writeToFile(file1, output);
  68. }
  69. else {
  70. snprintf(output, sizeof(output), "Íåò êîðíåé\n");
  71. writeToFile(file1, output);
  72. }
  73. // Ñáðîñ ñ÷åò÷èêà êîýôôèöèåíòîâ äëÿ ñëåäóþùåãî óðàâíåíèÿ
  74. coefCount = 0;
  75. }
  76. token = strtok(NULL, " \n");
  77. }
  78. }
  79. // Åñëè îñòàëèñü êîýôôèöèåíòû, êîòîðûå íå áûëè îáðàáîòàíû
  80. if (coefCount > 0) {
  81. char output[256];
  82. snprintf(output, sizeof(output), "Íåäîñòàòî÷íî êîýôôèöèåíòîâ äëÿ ïîñëåäíåãî óðàâíåíèÿ\n");
  83. writeToFile(file1, output);
  84. }
  85. CloseHandle(file);
  86. CloseHandle(file1);
  87. return EXIT_SUCCESS;
  88. }