I have added a few functions about your problem, in particular, the readCoefsFromFile function reads the coefficients from a binary file:
#include <stdio.h> #include <math.h> //Максимальное количество уравнений в программе (насколько я понял Вам нельзя работать с динамической памятью) #define MAX_ARRAY_SIZE 5 // Функция для записи коэффициентов в файл void writeCoefsToFile(const char* fileName, double coefs[MAX_ARRAY_SIZE][3]) { FILE* f = fopen(fileName, "wb"); if (f) { fwrite(coefs, sizeof(double), MAX_ARRAY_SIZE * 3, f); fclose(f); } } // Функция для считывания коэффициентов из файла void readCoefsFromFile(const char* fileName, double coefs[MAX_ARRAY_SIZE][3]) { FILE* f = fopen(fileName, "rb"); if (f) { fread(coefs, sizeof(double), MAX_ARRAY_SIZE * 3, f); fclose(f); } } // Функция для решения квадратных уравнений int findRoots(double a, double b, double c, double* x1, double* x2) { double d = b * b - 4 * a * c; if ((int)d == 0) { *x1 = -b/(2 * a); return 1; } else if (d > 0) { *x1 = (-b + sqrt(d))/(2 * a); *x2 = (-b - sqrt(d))/(2 * a); return 2; } return 0; } // Функция для распечатки коэффициентов void printCoefs(double coefs[MAX_ARRAY_SIZE][3]) { int i,j; for (i = 0; i < MAX_ARRAY_SIZE; i++) { for (j = 0; j < 3; j++) { printf("%lf ", coefs[i][j]); } printf("\n"); } } // Вывести решения в файл void createReport(const char* fileName, double coefs[MAX_ARRAY_SIZE][3]) { int i; double twoRootsEq[MAX_ARRAY_SIZE][2]; double oneRootEq[MAX_ARRAY_SIZE][1]; int countOfEqWithNoRoots = 0, countOfEqWithOneRoot = 0, countOfEqWithTwoRoots = 0; for (i = 0; i < MAX_ARRAY_SIZE; i++) { double x1, x2; int rootsCount = findRoots(coefs[i][0], coefs[i][1], coefs[i][2], &x1, &x2); if (rootsCount == 0) { countOfEqWithNoRoots++; } else if (rootsCount == 1) { oneRootEq[countOfEqWithOneRoot][0] = x1; countOfEqWithOneRoot++; } else { twoRootsEq[countOfEqWithTwoRoots][0] = x1; twoRootsEq[countOfEqWithTwoRoots][1] = x2; countOfEqWithTwoRoots++; } } FILE* f = fopen(fileName, "wb"); if (f) { fwrite(&countOfEqWithTwoRoots, sizeof(int), 1, f); fwrite(&countOfEqWithOneRoot, sizeof(int), 1, f); fwrite(&countOfEqWithNoRoots, sizeof(int), 1, f); if (countOfEqWithTwoRoots > 0) { fwrite(twoRootsEq, sizeof(double), countOfEqWithTwoRoots * 2, f); } if (countOfEqWithOneRoot > 0) { fwrite(oneRootEq, sizeof(double), countOfEqWithOneRoot * 1, f); } fclose(f); } } // Распечатать решения из файла void printReportFromFile(const char* fileName) { FILE* f = fopen(fileName, "rb"); if (f) { double twoRootsEq[MAX_ARRAY_SIZE][2]; double oneRootEq[MAX_ARRAY_SIZE][1]; int countOfEqWithNoRoots = 0, countOfEqWithOneRoot = 0, countOfEqWithTwoRoots = 0; fread(&countOfEqWithTwoRoots, sizeof(int), 1, f); fread(&countOfEqWithOneRoot, sizeof(int), 1, f); fread(&countOfEqWithNoRoots, sizeof(int), 1, f); if (countOfEqWithTwoRoots > 0) { fread(twoRootsEq, sizeof(double), countOfEqWithTwoRoots * 2, f); } if (countOfEqWithOneRoot > 0) { fread(oneRootEq, sizeof(double), countOfEqWithOneRoot * 1, f); } fclose(f); printf("2 roots count: %d, 1 root count: %d, no roots count: %d\n", countOfEqWithTwoRoots, countOfEqWithOneRoot, countOfEqWithNoRoots); int i; for (i = 0; i < countOfEqWithTwoRoots; i++) { printf("%lf %lf\n", twoRootsEq[i][0], twoRootsEq[i][1]); } for (i = 0; i < countOfEqWithOneRoot; i++) { printf("%lf\n", oneRootEq[i][0]); } } } int main(int argc, const char * argv[]) { double coefs[MAX_ARRAY_SIZE][3] = {{1.23, 34.5, 34.2}, {1.23, 343.345, 344.2}, {123.23, 34.55, 34.2}, {12.23, 34.5, 34.2}, {7.243, 341.5, 84.2}}; // Коэффициенты уравнений для примера writeCoefsToFile("input.txt", coefs); readCoefsFromFile("input.txt", coefs); printCoefs(coefs); createReport("output.txt", coefs); printReportFromFile("output.txt"); return 0; }
"wb")fwriteis, written byfwrite, notfprintf. And the numbers in it are real, not integers. - Igor