pvjc25/du2/program.c

55 lines
1.4 KiB
C
Raw Normal View History

2025-02-22 12:56:22 +00:00
#include <stdio.h>
#include <stdlib.h>
#define maxcoe 100
int read_number (double *num) {
2025-02-22 12:57:04 +00:00
char buffer[50];
2025-02-22 14:30:33 +00:00
if (!fgets(buffer, sizeof(buffer), stdin)) { //если fgets не смог считать строку
2025-02-22 12:57:04 +00:00
return 0;
}
2025-02-22 14:22:00 +00:00
if (sscanf(buffer, "%lf", num) != 1) { //переделываем в double. Если всё успешно
2025-02-22 13:16:02 +00:00
return 0;
}
2025-02-22 13:16:17 +00:00
return 1;
}
2025-02-22 13:25:14 +00:00
double hornerovaschema (double x, double cofecients[], int count) { //вычесляет значение многочлена в точке x
2025-02-22 13:16:17 +00:00
double result = 0.0;
for (int i = 0; i < count; i++) {
2025-02-22 14:22:00 +00:00
result = result * x + cofecients[i];
2025-02-22 13:16:17 +00:00
}
2025-02-22 13:25:14 +00:00
return result;
2025-02-22 12:56:22 +00:00
}
int main() {
2025-02-22 13:57:36 +00:00
double x;
double cof[maxcoe];
2025-02-22 14:22:00 +00:00
int count = 0;
2025-02-22 13:25:14 +00:00
2025-02-22 13:57:36 +00:00
if (!read_number(&x)) { //если не может считать
return 1;
}
2025-02-22 14:22:00 +00:00
while (count < maxcoe) {
2025-02-22 13:57:36 +00:00
double coef; //переменна для хранения считанного коэффициента
char buffer[50];//строковый буфер для fgetc
2025-02-22 14:30:33 +00:00
if (!fgets(buffer, sizeof(buffer), stdin) || buffer[0] == '\n') {
2025-02-22 13:57:36 +00:00
break;
}
2025-02-22 14:22:00 +00:00
if (sscanf(buffer, "%lf", &coef) != 1) {
return 1;
2025-02-22 14:10:50 +00:00
}
cof[count] = coef;
count++;
2025-02-22 14:22:00 +00:00
}
double result = hornerovaschema(x, cof, count);
2025-02-22 14:35:18 +00:00
printf("Vysledok je: %.2f\n", result);
2025-02-22 13:57:36 +00:00
2025-02-22 12:56:22 +00:00
return 0;
}