diff --git a/45Ex/input.txt b/45Ex/input.txt new file mode 100644 index 0000000..85954ea --- /dev/null +++ b/45Ex/input.txt @@ -0,0 +1,5 @@ +1 +2 +3 +4 +5 \ No newline at end of file diff --git a/45Ex/main.c b/45Ex/main.c new file mode 100644 index 0000000..0c8ab7a --- /dev/null +++ b/45Ex/main.c @@ -0,0 +1,23 @@ +#include +#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; +} diff --git a/45Ex/makefile b/45Ex/makefile new file mode 100644 index 0000000..7b54c1a --- /dev/null +++ b/45Ex/makefile @@ -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 \ No newline at end of file diff --git a/45Ex/solve_polynomial.c b/45Ex/solve_polynomial.c new file mode 100644 index 0000000..80ce4df --- /dev/null +++ b/45Ex/solve_polynomial.c @@ -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; +} diff --git a/45Ex/solve_polynomial.h b/45Ex/solve_polynomial.h new file mode 100644 index 0000000..5d58e07 --- /dev/null +++ b/45Ex/solve_polynomial.h @@ -0,0 +1,9 @@ +#ifndef SOLVE_POLYNOMIAL +#define SOLVE_POLYNOMIAL + +#include +#include + +int solvePolynomial(FILE * file, double x, double * derivative, double * polynomial); + +#endif // SOLVE_POLYNOMIAL diff --git a/45Ex/tools.c b/45Ex/tools.c new file mode 100644 index 0000000..3900212 --- /dev/null +++ b/45Ex/tools.c @@ -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; + } +} diff --git a/45Ex/tools.h b/45Ex/tools.h new file mode 100644 index 0000000..860a2a7 --- /dev/null +++ b/45Ex/tools.h @@ -0,0 +1,8 @@ +#ifndef TOOLS +#define TOOLS + +#include + +FILE * getFile(void); + +#endif