diff --git a/du2/program.c b/du2/program.c index 5eafd3b..ade152c 100644 --- a/du2/program.c +++ b/du2/program.c @@ -4,11 +4,11 @@ int read_number (double *num) { char buffer[50]; - if (!fgets(buffer, sizeof(buffer), stdin)) { //если fgets не смог считать строку + if (!fgetc(buffer, sizeof(buffer), stdin)) { //если fgets не смог считать строку return 0; } - if (sscanf(buffer, "%lf", num) ! = 1) { //переделываем в double. Если всё успешно + if (sscanf(buffer, "%lf", num) != 1) { //переделываем в double. Если всё успешно return 0; } return 1; @@ -17,7 +17,7 @@ int read_number (double *num) { double hornerovaschema (double x, double cofecients[], int count) { //вычесляет значение многочлена в точке x double result = 0.0; for (int i = 0; i < count; i++) { - result = result * x + coefficients[i]; + result = result * x + cofecients[i]; } return result; } @@ -25,7 +25,7 @@ double hornerovaschema (double x, double cofecients[], int count) { //вычес int main() { double x; double cof[maxcoe]; - double count = 0; + int count = 0; printf("Zadajte x: "); if (!read_number(&x)) { //если не может считать @@ -35,7 +35,7 @@ int main() { printf("Zadajte koeficienty (po jednom na riadok, ukoncite prazdnym riadkom):\n"); - while (count < maxcoe) + while (count < maxcoe) { double coef; //переменна для хранения считанного коэффициента char buffer[50];//строковый буфер для fgetc @@ -43,12 +43,17 @@ int main() { break; } - if (sscanf(buffer, sizeof(buffer), stdin)) { + if (sscanf(buffer, "%lf", &coef) != 1) { printf("Chyba. Nepodarilo sa nacitat koeficient %d.\n", count + 1); - return 1 + return 1; } cof[count] = coef; count++; + } + + double result = hornerovaschema(x, cof, count); + + printf("%.2f\n", result); return 0; } \ No newline at end of file