123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #define BUFFER_SIZE 1024
- int main() {
- system("chcp 1251 > nul");
- double a, b, c, discriminant, x1, x2;
- char buffer[BUFFER_SIZE];
- char result[BUFFER_SIZE];
- DWORD lpNumberOfBytesRead;
- DWORD bytesWritten;
- // Îòêðûòèå ôàéëà ñ êîýôôèöèåíòàìè äëÿ ÷òåíèÿ
- HANDLE myFile = CreateFileA("coefficients.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (myFile == INVALID_HANDLE_VALUE) {
- printf("Îøèáêà îòêðûòèÿ ôàéëà coefficients.txt. Îøèáêà: %lu\n", GetLastError());
- return 1;
- }
- printf("Ôàéë äëÿ ÷òåíèÿ óñïåøíî îòêðûò\n");
- // ×òåíèå äàííûõ èç ôàéëà
- while (ReadFile(myFile, buffer, sizeof(buffer), &lpNumberOfBytesRead, NULL)) {
- if (lpNumberOfBytesRead == 0) break;
- }
- CloseHandle(myFile);
-
- // Ñ÷èòûâàíèå êîýôôèöèåíòîâ â áóôåð
- if (sscanf_s(buffer, "%lf %lf %lf", &a, &b, &c) != 3) {
- printf("Íåêîððåêòíûé ôîðìàò âõîäíûõ äàííûõ\n");
- return 0;
- }
- printf("Êîýôôèöèåíòû: a=%.2f, b=%.2f, c=%.2f\n", a, b, c);
- // Ïðîâåðêà êîððåêòíîñòè êîýôôèöèåíòîâ
- if (a == 0 && b == 0) {
- printf("Íåâîçìîæíî ðåøèòü óðàâíåíèå, îáà êîýôôèöèåíòà a è b ðàâíû 0\n");
- snprintf(result, BUFFER_SIZE, "Íåâîçìîæíî ðåøèòü óðàâíåíèå, îáà êîýôôèöèåíòà a è b ðàâíû 0\n");
-
- HANDLE myFileResult = CreateFileA("results.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (myFileResult == INVALID_HANDLE_VALUE) {
- printf("Îøèáêà ñîçäàíèÿ ôàéëà results.txt. Îøèáêà: %lu\n", GetLastError());
- return 1;
- }
- printf("Ôàéë äëÿ çàïèñè óñïåøíî ñîçäàí\n");
-
- WriteFile(myFileResult, result, strlen(result), &bytesWritten, NULL);
- CloseHandle(myFileResult);
- printf("Ðåçóëüòàò çàïèñàí â ôàéë results.txt\n");
- return 0;
- }
- // Âû÷èñëåíèå äèñêðèìèíàíòà
- discriminant = b * b - 4 * a * c;
-
- if (discriminant > 0) {
- x1 = (-b + sqrt(discriminant)) / (2 * a);
- x2 = (-b - sqrt(discriminant)) / (2 * a);
- snprintf(result, BUFFER_SIZE, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 2\nÊîðåíü 1: %.2f\nÊîðåíü 2: %.2f\n",
- discriminant, x1, x2);
- }
- else if (discriminant == 0) {
- x1 = -b / (2 * a);
- snprintf(result, BUFFER_SIZE, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 1\nÊîðåíü: %.2f\n",
- discriminant, x1);
- }
- else {
- snprintf(result, BUFFER_SIZE, "Äèñêðèìèíàíò: %.2f\nÄåéñòâèòåëüíûõ êîðíåé íåò.\n",
- discriminant);
- }
- // Îòêðûòèå ôàéëà äëÿ çàïèñè ðåçóëüòàòà
- HANDLE myFileResult = CreateFilÝeA("results.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (myFileResult == INVALID_HANDLE_VALUE) {
- printf("Îøèáêà ñîçäàíèÿ ôàéëà results.txt. Îøèáêà: %lu\n", GetLastError());
- return 1;
- }
- printf("Ôàéë äëÿ çàïèñè óñïåøíî ñîçäàí\n");
- // Çàïèñü ðåçóëüòàòà â ôàéë
- WriteFile(myFileResult, result, strlen(result), &bytesWritten, NULL);
- CloseHandle(myFileResult);
- printf("Ðåçóëüòàò çàïèñàí â ôàéë results.txt\n");
- return 0;
- }
|