From f677c28eb42ff45f7d1b845c1aed863b343138bc Mon Sep 17 00:00:00 2001 From: AZEN-SGG <74971141+AZEN-SGG@users.noreply.github.com> Date: Sun, 22 Sep 2024 22:05:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=2045=20?= =?UTF-8?q?=D0=97=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 45Ex/input.txt | 5 +++++ 45Ex/main.c | 23 +++++++++++++++++++++++ 45Ex/makefile | 11 +++++++++++ 45Ex/solve_polynomial.c | 20 ++++++++++++++++++++ 45Ex/solve_polynomial.h | 9 +++++++++ 45Ex/tools.c | 23 +++++++++++++++++++++++ 45Ex/tools.h | 8 ++++++++ 7 files changed, 99 insertions(+) create mode 100644 45Ex/input.txt create mode 100644 45Ex/main.c create mode 100644 45Ex/makefile create mode 100644 45Ex/solve_polynomial.c create mode 100644 45Ex/solve_polynomial.h create mode 100644 45Ex/tools.c create mode 100644 45Ex/tools.h 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