Сделал 45 Задание
This commit is contained in:
parent
dae8abd5cc
commit
f677c28eb4
7 changed files with 99 additions and 0 deletions
5
45Ex/input.txt
Normal file
5
45Ex/input.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
23
45Ex/main.c
Normal file
23
45Ex/main.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include "tools.h"
|
||||
#include "solve_polynomial.h"
|
||||
|
||||
/*
|
||||
Problem 45
|
||||
Coordinate X: 2
|
||||
Solution: 129
|
||||
Derivative: 222
|
||||
*/
|
||||
|
||||
int main(void) {
|
||||
double derivative, polynomial, x;
|
||||
FILE * file = getFile();
|
||||
if (file == NULL) return 1;
|
||||
|
||||
printf("Enter x coordinat: ");
|
||||
scanf("%lf", &x);
|
||||
|
||||
if (solvePolynomial(file, x, &derivative, &polynomial)) return 1;
|
||||
printf("Solve of the polynomial is %lf\nSolve of its derivative is %lf\n", polynomial, derivative);
|
||||
return 0;
|
||||
}
|
11
45Ex/makefile
Normal file
11
45Ex/makefile
Normal file
|
@ -0,0 +1,11 @@
|
|||
all: main.o solve_polynomial.o tools.o
|
||||
gcc main.o solve_polynomial.o tools.o && del *.o
|
||||
|
||||
main.o: main.c
|
||||
gcc -c main.c
|
||||
|
||||
solve_polynomial.o: solve_polynomial.c
|
||||
gcc -c solve_polynomial.c
|
||||
|
||||
tools.o: tools.c
|
||||
gcc -c tools.c
|
20
45Ex/solve_polynomial.c
Normal file
20
45Ex/solve_polynomial.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "solve_polynomial.h"
|
||||
|
||||
int solvePolynomial(FILE * file, double x, double * derivative, double * polynomial) {
|
||||
double current, i;
|
||||
|
||||
if (fscanf(file, "%lf", ¤t) != 1) {
|
||||
printf("File is empty!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
*derivative = *polynomial = i = 0.;
|
||||
|
||||
do {
|
||||
*polynomial += current * pow(x, i);
|
||||
*derivative += i * current * pow(x, i - 1);
|
||||
i++;
|
||||
} while (fscanf(file, "%lf", ¤t) == 1);
|
||||
|
||||
return 0;
|
||||
}
|
9
45Ex/solve_polynomial.h
Normal file
9
45Ex/solve_polynomial.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef SOLVE_POLYNOMIAL
|
||||
#define SOLVE_POLYNOMIAL
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int solvePolynomial(FILE * file, double x, double * derivative, double * polynomial);
|
||||
|
||||
#endif // SOLVE_POLYNOMIAL
|
23
45Ex/tools.c
Normal file
23
45Ex/tools.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "tools.h"
|
||||
|
||||
FILE * getFile(void)
|
||||
{
|
||||
FILE * file;
|
||||
char filename[50];
|
||||
|
||||
printf("Enter filename: ");
|
||||
if (scanf("%s", filename) == 1)
|
||||
{
|
||||
file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
printf("Error file!\n");
|
||||
return NULL;
|
||||
} else {
|
||||
return file;
|
||||
}
|
||||
} else
|
||||
{
|
||||
printf("Empty name!\n");
|
||||
return NULL;
|
||||
}
|
||||
}
|
8
45Ex/tools.h
Normal file
8
45Ex/tools.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef TOOLS
|
||||
#define TOOLS
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
FILE * getFile(void);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue