pvjc25/du2/program.c

54 lines
1.5 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];
if (!fgets(buffer, sizeof(buffer), stdin)) { //если fgets не смог считать строку
return 0;
}
2025-02-22 13:16:17 +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++) {
result = result * x + coefficients[i];
}
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];
double count = 0;
2025-02-22 13:25:14 +00:00
2025-02-22 13:57:36 +00:00
printf("Zadajte x: ");
if (!read_number(&x)) { //если не может считать
printf("Chyba. Nepodarilo sa nacitat hodnotu");
return 1;
}
printf("Zadajte koeficienty (po jednom na riadok, ukoncite prazdnym riadkom):\n");
while (count < maxcoe)
double coef; //переменна для хранения считанного коэффициента
char buffer[50];//строковый буфер для fgetc
if (!fgetc(buffer, sizeof(buffer), stdin) || buffer[0] == '\n') {
break;
}
2025-02-22 14:10:50 +00:00
if (sscanf(buffer, sizeof(buffer), stdin)) {
printf("Chyba. Nepodarilo sa nacitat koeficient %d.\n", count + 1);
return 1
}
cof[count] = coef;
count++;
2025-02-22 13:57:36 +00:00
2025-02-22 12:56:22 +00:00
return 0;
}