123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #define _CRT_SECURE_NO_WARNINGS
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- int is_valid_double(const char* str) {
- char* endptr;
- strtod(str, &endptr);
- return *endptr == '\0' && endptr != str;
- }
- void write_results(const char* filename, double x1, double x2, double D, int root_count) {
- HANDLE hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- MessageBox(NULL, "Îøèáêà îòêðûòèÿ ôàéëà äëÿ çàïèñè ðåçóëüòàòîâ.", "Îøèáêà", MB_OK | MB_ICONERROR);
- return;
- }
- char buffer[256];
- if (root_count == 2) {
- sprintf(buffer, "Êîðíè óðàâíåíèÿ: %.2f è %.2f\n", x1, x2);
- }
- else if (root_count == 1) {
- sprintf(buffer, "Êîðåíü óðàâíåíèÿ: %.2f\n", x1);
- }
- else {
- sprintf(buffer, "Íåò äåéñòâèòåëüíûõ êîðíåé.\n");
- }
- sprintf(buffer + strlen(buffer), "Äèñêðèìèíàíò: %.2f\n", D);
- DWORD written;
- WriteFile(hFile, buffer, strlen(buffer), &written, NULL);
- CloseHandle(hFile);
- }
- int main() {
- double a, b, c;
- char line[256];
- int count = 0;
- // Ñ÷èòûâàåì êîýôôèöèåíòû èç ôàéëà
- HANDLE hFile = CreateFile("test.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- MessageBox(NULL, "Îøèáêà îòêðûòèÿ ôàéëà äëÿ ÷òåíèÿ êîýôôèöèåíòîâ.", "Îøèáêà", MB_OK | MB_ICONERROR);
- return 1;
- }
- DWORD bytesRead;
- while (ReadFile(hFile, line, sizeof(line) - 1, &bytesRead, NULL) && bytesRead > 0) {
- line[bytesRead] = '\0';
- char* token = strtok(line, " \n");
- while (token != NULL) {
- if (is_valid_double(token)) {
- if (count == 0) {
- a = strtod(token, NULL);
- }
- else if (count == 1) {
- b = strtod(token, NULL);
- }
- else if (count == 2) {
- c = strtod(token, NULL);
- }
- count++;
- }
- else {
- MessageBox(NULL, "Îøèáêà: Ââåäåíû íåêîððåêòíûå äàííûå.", "Îøèáêà", MB_OK | MB_ICONERROR);
- CloseHandle(hFile);
- return 1;
- }
- token = strtok(NULL, " \n");
- }
- }
- CloseHandle(hFile);
- // Ïðîâåðÿåì êîýôôèöèåíòû
- if (count < 3) {
- MessageBox(NULL, "Îøèáêà: Íå äîñòàòî÷íî êîýôôèöèåíòîâ.", "Îøèáêà", MB_OK | MB_ICONERROR);
- return 1;
- }
- if (a == 0) {
- MessageBox(NULL, "Îøèáêà: Êîýôôèöèåíò 'a' íå ìîæåò áûòü ðàâåí 0.", "Îøèáêà", MB_OK | MB_ICONERROR);
- return 1;
- }
- // Âû÷èñëÿåì äèñêðèìèíàíò
- double D = b * b - 4 * a * c;
- double x1, x2;
- int root_count = 0;
- if (D > 0) {
- x1 = (-b + sqrt(D)) / (2 * a);
- x2 = (-b - sqrt(D)) / (2 * a);
- root_count = 2;
- }
- else if (D == 0) {
- x1 = -b / (2 * a);
- root_count = 1;
- }
- // Çàïèñûâàåì ðåçóëüòàòû â ôàéë
- write_results("otvet.txt", x1, x2, D, root_count);
- MessageBox(NULL, "Ðåçóëüòàòû çàïèñàíû â ôàéë 'otvet.txt'.", "Ãîòîâî", MB_OK | MB_ICONINFORMATION);
- return 0;
- }
|