Given a binary file of real numbers, containing the coefficients of quadratic equations (A1, B1, C1, A2, B2, C2, ...). Create a new binary file containing at first three numbers N2, N1, N0 - the number of equations in the source file with two, one and zero real roots, then the values ​​of the roots of all equations with two real roots, and then the values ​​of the roots of all equations with one real root . Allowed to work as a text file.

I can not go further filling the first file. How can one read the data from it in such a way that the equation is obtained, i.e. So that 3 coefficients are read and they go to their places?

#include <stdio.h> #include <windows.h> void main() { SetConsoleOutputCP(1251); int A[100][3]; int N, m=3; int i, j,k; FILE *f=fopen("num.txt","w"), *nf=fopen("rad.txt","w"); if(f==NULL) printf("Не удалось открыть файл"); if(nf==NULL) printf("Не удалось создать файл"); printf("Введите количество уравнений:N="); scanf("%d",&N); for(i=0;i<N;i++) { for(j=0;j<m;j++) { printf("Введите коэффициенты уравнений:",i+1,j+1); scanf("%d",&A[i][j]); fprintf(f,"%d",A[i][j]); } } fclose(f); fclose(nf); system("pause"); } 
  • 2
    Another mathematician. "I can not go further filling the first file." - You already have a file with coefficients. And it is binary ( "wb" ) fwrite is, written by fwrite , not fprintf . And the numbers in it are real, not integers. - Igor
  • @Igor Allowed to work as with a text file - ross
  • You yourself do not understand what you wrote ... And no one can do it for you. Answer yourself at least two questions: 1) Why did you declare two files (f, nf) but use only one? If you were going to read from one file and write to another, then why do you have both files open for RECORDING? Well, about binaries you have already been told ... - Sergey
  • @Sergey Allowed to work as a text file, as stated above. The first file is filled with coefficients, and the second calculates the roots, because they are both open for writing. And how should I use the second file if I can't figure out how to send variables there - ross
  • one
    in the second, the roots are calculated - the roots cannot be CALCULATED in the file. The file can only record the result of calculations. - Sergey

1 answer 1

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; }