Anna 2 هفته پیش
والد
کامیت
39b01d49b1
2فایلهای تغییر یافته به همراه78 افزوده شده و 37 حذف شده
  1. 76 37
      Project8/FileName.c
  2. 2 0
      Project8/otvet.txt

+ 76 - 37
Project8/FileName.c

@@ -15,49 +15,88 @@
 #include <stdio.h>
 #include <conio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
 
-void main() {
-    //Ñ ïîìîùüş ïåğåìåííîé file áóäåì îñóùåñòâëÿòü äîñòóï ê ôàéëó
+int is_valid_double(const char* str) {
+    char* endptr;
+    // Ïğîâåğÿåì, ìîæíî ëè ïğåîáğàçîâàòü ñòğîêó â ÷èñëî òèïà double
+    strtod(str, &endptr);
+
+    // Åñëè endptr óêàçûâàåò íà íà÷àëî ñòğîêè èëè íå íà êîíåö ñòğîêè, çíà÷èò, ıòî íå ÷èñëî
+    return *endptr == '\0' && endptr != str;
+}
+
+int main() {
+    system("chcp 1251>nul");
+    // Ñ ïîìîùüş ïåğåìåííîé file áóäåì îñóùåñòâëÿòü äîñòóï ê ôàéëó
     FILE* file;
-    //Îòêğûâàåì òåêñòîâûé ôàéë ñ ïğàâàìè íà çàïèñü
+    // Îòêğûâàåì òåêñòîâûé ôàéë ñ ïğàâàìè íà ÷òåíèå
     file = fopen("test.txt", "r");
-    //Ïèøåì â ôàéë
+
     double a, b, c;
 
-	if (file == NULL) {
-		fprintf(stderr, "Îøèáêà îòêğûòèÿ ôàéëà äëÿ ÷òåíèÿ êîıôôèöèåíòîâ.\n");
-		return 1;
-	}
-	fscanf(file, "%lf %lf %lf", &a, &b, &c);
+    if (file == NULL) {
+        fprintf(stderr, "Îøèáêà îòêğûòèÿ ôàéëà äëÿ ÷òåíèÿ êîıôôèöèåíòîâ.\n");
+        return 1;
+    }
+
+    char line[256];
+    int count = 0;
+
+    // Ñ÷èòûâàåì çíà÷åíèÿ èç ôàéëà ïî ñòğîêàì
+    while (fgets(line, sizeof(line), file) != NULL) {
+        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 {
+                fprintf(stderr, "Îøèáêà: '%s' íå ÿâëÿåòñÿ äîïóñòèìûì ÷èñëîì.\n", token);
+                fclose(file);
+                return 1;
+            }
+            token = strtok(NULL, " \n");
+        }
+    }
 
-    
-    //Çàêğûâàåì ôàéë
+    // Çàêğûâàåì ôàéë
     fclose(file);
-    printf("%lf", a);
-    _getch();
-	double D = b * b - 4 * a * c;
-	/*char otvet[200] = "êîğíè óğàâíåíèÿ: ";*/
-	FILE* file1 = fopen("otvet.txt", "w");
-	if (file1 == NULL) {
-		fprintf(stderr, "Îøèáêà îòêğûòèÿ ôàéëà äëÿ çàïèñè ğåçóëüòàòîâ.\n");
-		return;
-	}
-	if (D > 0) {
-		double x1 = (-b + sqrt(D)) / (2 * a);
-		double x2 = (-b - sqrt(D)) / (2 * a);
-		char buffer[200];
-
-		fprintf(file1, "%.2f è %.2f", x1, x2);
-		fprintf(file1, "\n Äèñêğèìèíàíò %.2f", D);
-	}
-	else if (D == 0) {
-		double x = -b / (2 * a);
-		fprintf(file1, "Êîğåíü óğàâíåíèÿ: %.2f", x);
-		fprintf(file1, "\n Äèñêğèìèíàíò %.2f", D);
-	}
-	else if (D < 0)fprintf(file1, "Íåò äåéñòâèòåëüíûõ êîğíåé");
-	fclose(file1);
-	_getch(); 
-	return 0;
+    printf("Êîıôôèöèåíòû: a = %lf, b = %lf, c = %lf\n", a, b, c);
+
+    double D = b * b - 4 * a * c;
+
+    // Îòêğûâàåì ôàéë äëÿ çàïèñè ğåçóëüòàòîâ
+    FILE* file1 = fopen("otvet.txt", "w");
+    if (file1 == NULL) {
+        fprintf(stderr, "Îøèáêà îòêğûòèÿ ôàéëà äëÿ çàïèñè ğåçóëüòàòîâ.\n");
+        return 1;
+    }
 
+    if (D > 0) {
+        double x1 = (-b + sqrt(D)) / (2 * a);
+        double x2 = (-b - sqrt(D)) / (2 * a);
+        fprintf(file1, "Êîğíè óğàâíåíèÿ: %.2f è %.2f\n", x1, x2);
+    }
+    else if (D == 0) {
+        double x = -b / (2 * a);
+        fprintf(file1, "Êîğåíü óğàâíåíèÿ: %.2f\n", x);
+    }
+    else {
+        fprintf(file1, "Íåò äåéñòâèòåëüíûõ êîğíåé\n");
+    }
+
+    fprintf(file1, "Äèñêğèìèíàíò: %.2f\n", D);
+    fclose(file1);
+    _getch();
+    return 0;
 }

+ 2 - 0
Project8/otvet.txt

@@ -0,0 +1,2 @@
+Корни уравнения: -0.43 и -0.43
+Дискриминант: 22.01