Source.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #define BUFFER_SIZE 1024
  7. int main() {
  8. system("chcp 1251 > nul");
  9. double a, b, c, discriminant, x1, x2;
  10. char buffer[BUFFER_SIZE];
  11. char result[BUFFER_SIZE];
  12. DWORD lpNumberOfBytesRead;
  13. DWORD bytesWritten;
  14. // Îòêðûòèå ôàéëà ñ êîýôôèöèåíòàìè äëÿ ÷òåíèÿ
  15. HANDLE myFile = CreateFileA("coefficients.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  16. if (myFile == INVALID_HANDLE_VALUE) {
  17. printf("Îøèáêà îòêðûòèÿ ôàéëà coefficients.txt. Îøèáêà: %lu\n", GetLastError());
  18. return 1;
  19. }
  20. printf("Ôàéë äëÿ ÷òåíèÿ óñïåøíî îòêðûò\n");
  21. // ×òåíèå äàííûõ èç ôàéëà
  22. while (ReadFile(myFile, buffer, sizeof(buffer), &lpNumberOfBytesRead, NULL)) {
  23. if (lpNumberOfBytesRead == 0) break;
  24. }
  25. CloseHandle(myFile);
  26. // Ñ÷èòûâàíèå êîýôôèöèåíòîâ â áóôåð
  27. if (sscanf_s(buffer, "%lf %lf %lf", &a, &b, &c) != 3) {
  28. printf("Íåêîððåêòíûé ôîðìàò âõîäíûõ äàííûõ\n");
  29. return 0;
  30. }
  31. printf("Êîýôôèöèåíòû: a=%.2f, b=%.2f, c=%.2f\n", a, b, c);
  32. // Ïðîâåðêà êîððåêòíîñòè êîýôôèöèåíòîâ
  33. if (a == 0 && b == 0) {
  34. printf("Íåâîçìîæíî ðåøèòü óðàâíåíèå, îáà êîýôôèöèåíòà a è b ðàâíû 0\n");
  35. snprintf(result, BUFFER_SIZE, "Íåâîçìîæíî ðåøèòü óðàâíåíèå, îáà êîýôôèöèåíòà a è b ðàâíû 0\n");
  36. HANDLE myFileResult = CreateFileA("results.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  37. if (myFileResult == INVALID_HANDLE_VALUE) {
  38. printf("Îøèáêà ñîçäàíèÿ ôàéëà results.txt. Îøèáêà: %lu\n", GetLastError());
  39. return 1;
  40. }
  41. printf("Ôàéë äëÿ çàïèñè óñïåøíî ñîçäàí\n");
  42. WriteFile(myFileResult, result, strlen(result), &bytesWritten, NULL);
  43. CloseHandle(myFileResult);
  44. printf("Ðåçóëüòàò çàïèñàí â ôàéë results.txt\n");
  45. return 0;
  46. }
  47. // Âû÷èñëåíèå äèñêðèìèíàíòà
  48. discriminant = b * b - 4 * a * c;
  49. if (discriminant > 0) {
  50. x1 = (-b + sqrt(discriminant)) / (2 * a);
  51. x2 = (-b - sqrt(discriminant)) / (2 * a);
  52. snprintf(result, BUFFER_SIZE, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 2\nÊîðåíü 1: %.2f\nÊîðåíü 2: %.2f\n",
  53. discriminant, x1, x2);
  54. }
  55. else if (discriminant == 0) {
  56. x1 = -b / (2 * a);
  57. snprintf(result, BUFFER_SIZE, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 1\nÊîðåíü: %.2f\n",
  58. discriminant, x1);
  59. }
  60. else {
  61. snprintf(result, BUFFER_SIZE, "Äèñêðèìèíàíò: %.2f\nÄåéñòâèòåëüíûõ êîðíåé íåò.\n",
  62. discriminant);
  63. }
  64. // Îòêðûòèå ôàéëà äëÿ çàïèñè ðåçóëüòàòà
  65. HANDLE myFileResult = CreateFilÝeA("results.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  66. if (myFileResult == INVALID_HANDLE_VALUE) {
  67. printf("Îøèáêà ñîçäàíèÿ ôàéëà results.txt. Îøèáêà: %lu\n", GetLastError());
  68. return 1;
  69. }
  70. printf("Ôàéë äëÿ çàïèñè óñïåøíî ñîçäàí\n");
  71. // Çàïèñü ðåçóëüòàòà â ôàéë
  72. WriteFile(myFileResult, result, strlen(result), &bytesWritten, NULL);
  73. CloseHandle(myFileResult);
  74. printf("Ðåçóëüòàò çàïèñàí â ôàéë results.txt\n");
  75. return 0;
  76. }