123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <conio.h>
- int main(int argc, char* argv[]) {
- FILE* input_file, * output_file;
- double a, b, c, discriminant, x1, x2;
- int num_roots;
- system("chcp 1251 > nul");
- // Îòêðûâàåì ôàéë äëÿ ÷òåíèÿ êîýôôèöèåíòîâ
- input_file = fopen("coefficients.txt", "r");
- if (!input_file) {
- fprintf(stderr, "Îøèáêà îòêðûòèÿ ôàéëà coefficients.txt\n");
- return 1;
- }
- // Ñ÷èòûâàåì êîýôôèöèåíòû
- if (fscanf(input_file, "%lf %lf %lf", &a, &b, &c) != 3) {
- fprintf(stderr, "Íåêîððåêòíûé ôîðìàò âõîäíûõ äàííûõ\n");
- fclose(input_file);
- return 1;
- }
- fclose(input_file);
- // Ïðîâåðÿåì êîððåêòíîñòü êîýôôèöèåíòîâ
- if (a == 0 && b == 0) {
- fprintf(stderr, "Íåâîçìîæíî ðåøèòü óðàâíåíèå, îáà êîýôôèöèåíòà a è b ðàâíû 0\n");
- return 1;
- }
- // Îòêðûâàåì ôàéë äëÿ çàïèñè ðåçóëüòàòîâ
- output_file = fopen("results.txt", "w");
- if (!output_file) {
- fprintf(stderr, "Îøèáêà îòêðûòèÿ ôàéëà results.txt\n");
- return 1;
- }
- // Âû÷èñëÿåì äèñêðèìèíàíò
- discriminant = b * b - 4 * a * c;
- // Îïðåäåëÿåì êîëè÷åñòâî êîðíåé
- if (discriminant > 0) {
- num_roots = 2;
- x1 = (-b + sqrt(discriminant)) / (2 * a);
- x2 = (-b - sqrt(discriminant)) / (2 * a);
- fprintf(output_file, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 2\nÊîðåíü 1: %.2f\nÊîðåíü 2: %.2f\n",
- discriminant, x1, x2);
- printf("Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 2\nÊîðåíü 1: %.2f\nÊîðåíü 2: %.2f\n",
- discriminant, x1, x2);
- }
- else if (discriminant == 0) {
- num_roots = 1;
- x1 = -b / (2 * a);
- fprintf(output_file, "Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 1\nÊîðåíü: %.2f\n",
- discriminant, x1);
- printf("Äèñêðèìèíàíò: %.2f\nÊîëè÷åñòâî êîðíåé: 1\nÊîðåíü: %.2f\n",
- discriminant, x1);
- }
- else {
- num_roots = 0;
- fprintf(output_file, "Äèñêðèìèíàíò: %.2f\nÄåéñòâèòåëüíûõ êîðíåé íåò.\n",
- discriminant);
- printf("Äèñêðèìèíàíò: % .2f\nÄåéñòâèòåëüíûõ êîðíåé íåò.\n",
- discriminant);
- }
- fclose(output_file);
- printf("Ðåçóëüòàòû âû÷èñëåíèé ñîõðàíåíû â ôàéë results.txt\n");
- return 0;
- }
|