Доделал 46е задание

This commit is contained in:
AZEN-SGG 2024-09-24 21:32:13 +03:00
parent 5d875464a3
commit 973e407a45
6 changed files with 41 additions and 21 deletions

Binary file not shown.

1
46Ex/input.txt Normal file
View file

@ -0,0 +1 @@
5 4 3 2 1

View file

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include "tools.h" #include "tools.h"
#include "solution_polynomial.h"
/* /*
Example: 5 4 3 2 1 Example: 5 4 3 2 1
@ -9,27 +10,6 @@ Answer: 80 + 32 + 12 + 4 + 1 = 129
160 + 48 + 12 + 2 = 222 160 + 48 + 12 + 2 = 222
*/ */
int solutionPolynomial(FILE * file, double x, double * derivative, double * polynomial);
int solutionPolynomial(FILE * file, double x, double * derivative, double * polynomial) {
double current;
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!");
return 1;
}
*derivative = *polynomial = 0;
do {
*derivative = *derivative * x + *polynomial;
*polynomial = *polynomial * x + current;
// c4x^3 + c3x^2 + c2x + c = c = c2x + c = c3x^2 + c2x + c = ...
} while (fscanf(file, "%lf", &current) == 1);
return 0;
}
int main(void) { int main(void) {
double x, derivative, polynomial; double x, derivative, polynomial;
FILE * file = getFile(); FILE * file = getFile();

11
46Ex/makefile Normal file
View file

@ -0,0 +1,11 @@
all: main.o solution_polynomial.o tools.o
gcc main.o solution_polynomial.o tools.o && del *.o
main.o: main.c
gcc -c main.c
solution_polynomial.o: solution_polynomial.c
gcc -c solution_polynomial.c
tools.o: tools.c
gcc -c tools.c

View file

@ -0,0 +1,20 @@
#include "solution_polynomial.h"
int solutionPolynomial(FILE * file, double x, double * derivative, double * polynomial) {
double current;
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!");
return 1;
}
*derivative = *polynomial = 0;
do {
*derivative = *derivative * x + *polynomial;
*polynomial = *polynomial * x + current;
// c4x^3 + c3x^2 + c2x + c = c = c2x + c = c3x^2 + c2x + c = ...
} while (fscanf(file, "%lf", &current) == 1);
return 0;
}

View file

@ -0,0 +1,8 @@
#ifndef SOLUTION_POLYNOMIAL
#define SOLUTION_POLYNOMIAL
#include <stdio.h>
int solutionPolynomial(FILE * file, double x, double * derivative, double * polynomial);
#endif // SOLUTION_POLYNOMIAL