pvjc24/cv3/program.c
2024-03-08 08:40:36 +00:00

49 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COEFFICIENTS 100
#define BUFFER_SIZE 1024
double evaluatePolynomial(double x, double coefficients[], int n) {
double result = 0.0;
for (int i = 0; i < n; ++i) {
result = result * x + coefficients[i];
}
return result;
}
int main() {
char buffer[BUFFER_SIZE];
double x, coef;
double coefficients[MAX_COEFFICIENTS];
int count = 0;
fgets(buffer, BUFFER_SIZE, stdin);
if (sscanf(buffer, "%lf", &x) != 1) {
printf("Nepodarilo sa nacitat zaklad x\n");
return 0;
}
while (fgets(buffer, BUFFER_SIZE, stdin) && buffer[0] != '\n') {
if (sscanf(buffer, "%lf", &coef) == 1) {
coefficients[count++] = coef;
if (count >= MAX_COEFFICIENTS) {
printf("Maximum number of coefficients exceeded.\n");
return 0;
}
} else {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1);
return 0;
}
}
double result = evaluatePolynomial(x, coefficients, count);
printf("Vysledok je: %.2f\n", result);
return 0;
}