Обновить du2/program.c

This commit is contained in:
Denys Sanchuk 2025-02-25 14:09:29 +00:00
parent 5b6d09c104
commit 7be208152c

View File

@ -1,38 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 50
void evaluate_polynomial(double x, double *coefficients, int n) {
double result = coefficients[0];
int main() {
double x, coefficient, result = 0.0;
int count = 0;
double coefficients[MAX_SIZE];
if (scanf("%lf", &x) != 1) {
printf("Chyba: Hodnota pre x nebola nacitana.\n");
return 1;
}
while (count < MAX_SIZE) {
if (scanf("%lf", &coefficient) != 1) {
if (feof(stdin)) break; // Конец файла - завершаем чтение
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1);
return 1;
}
coefficients[count++] = coefficient;
}
if (count == 0) {
printf("Chyba: Koeficient nebol nacitany.\n");
return 1;
}
result = coefficients[0];
for (int i = 1; i < count; i++) {
// Применение метода Хорнера
for (int i = 1; i < n; i++) {
result = result * x + coefficients[i];
}
printf("Vysledok je: %.2lf\n", result);
printf("Vysledok je: %.2f\n", result);
}
int main() {
double x;
int n = 0;
// Ввод значения x
if (scanf("%lf", &x) != 1) {
printf("Chyba: Neplatne x\n");
return 1;
}
double coefficients[100]; // Массив для коэффициентов полинома
// Чтение коэффициентов
while (scanf("%lf", &coefficients[n]) == 1) {
n++;
}
// Если нет коэффициентов
if (n == 0) {
printf("Chyba: Nebyly zadany koeficienty\n");
return 1;
}
// Вызов функции для вычисления полинома
evaluate_polynomial(x, coefficients, n);
return 0;
}