123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <windows.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- #include <stdio.h>
- #include <math.h>
- void writeToFile(HANDLE file, const char* data) {
- DWORD bytesWritten;
- WriteFile(file, data, strlen(data), &bytesWritten, NULL);
- }
- void readFromFile(HANDLE file, char* buffer, DWORD size) {
- DWORD bytesRead;
- ReadFile(file, buffer, size - 1, &bytesRead, NULL);
- buffer[bytesRead] = '\0'; // Çàâåğøåíèå ñòğîêè
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- HANDLE file1 = CreateFile(L"coefOUT.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- HANDLE file = CreateFile(L"coef.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (file == INVALID_HANDLE_VALUE) {
- fprintf(stderr, "Îøèáêà îòêğûòèÿ ôàéëà coef.txt: %lu\n", GetLastError());
- return EXIT_FAILURE;
- }
- if (file1 == INVALID_HANDLE_VALUE) {
- fprintf(stderr, "Îøèáêà îòêğûòèÿ ôàéëà coefOUT.txt: %lu\n", GetLastError());
- CloseHandle(file);
- return EXIT_FAILURE;
- }
- char line[256];
- double coef[3];
- int coefCount = 0; // Ñ÷åò÷èê êîıôôèöèåíòîâ
- while (1) {
- readFromFile(file, line, sizeof(line)); // ×òåíèå ñòğîêè èç ôàéëà
- // Ïğîâåğêà íà êîíåö ôàéëà
- if (strlen(line) == 0) {
- break; // Åñëè ñòğîêà ïóñòàÿ, âûõîäèì èç öèêëà
- }
- char* token = strtok(line, " \n");
- // Îáğàáîòêà êîıôôèöèåíòîâ
- while (token != NULL) {
- coef[coefCount] = atof(token);
- coefCount++;
- // Åñëè ïğî÷èòàíî ğîâíî 3 êîıôôèöèåíòà, âû÷èñëÿåì êîğíè
- if (coefCount == 3) {
- double x1, x;
- double a = coef[0];
- double b = coef[1];
- double c = coef[2];
- char output[256];
- snprintf(output, sizeof(output), "Êîıôôèöèåíòû: %f %f %f\n", a, b, c);
- writeToFile(file1, output);
- double D = b * b - 4 * a * c;
- if (a == 0 && b == 0 && c == 0) {
- snprintf(output, sizeof(output), "Àğãóìåíòû íå ìîãóò áûòü îòğèöàòåëüíûìè è ğàâíÿòüñÿ íóëş\n");
- writeToFile(file1, output);
- }
- else if (D > 0) {
- x1 = (-b + sqrt(D)) / (2 * a);
- x = (-b - sqrt(D)) / (2 * a);
- snprintf(output, sizeof(output), "Êîğíè: x1 = %f, x2 = %f\n", x1, x);
- writeToFile(file1, output);
- }
- else if (D == 0) {
- x = -b / (2 * a);
- snprintf(output, sizeof(output), "Êîğåíü: %f\n", x);
- writeToFile(file1, output);
- }
- else {
- snprintf(output, sizeof(output), "Íåò êîğíåé\n");
- writeToFile(file1, output);
- }
- // Ñáğîñ ñ÷åò÷èêà êîıôôèöèåíòîâ äëÿ ñëåäóşùåãî óğàâíåíèÿ
- coefCount = 0;
- }
- token = strtok(NULL, " \n");
- }
- }
- // Åñëè îñòàëèñü êîıôôèöèåíòû, êîòîğûå íå áûëè îáğàáîòàíû
- if (coefCount > 0) {
- char output[256];
- snprintf(output, sizeof(output), "Íåäîñòàòî÷íî êîıôôèöèåíòîâ äëÿ ïîñëåäíåãî óğàâíåíèÿ\n");
- writeToFile(file1, output);
- }
- CloseHandle(file);
- CloseHandle(file1);
- return EXIT_SUCCESS;
- }
|