From 72c07053cdc404ab73b1898437e67c2d1023281f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1=C5=A1?= Date: Fri, 8 Mar 2024 08:38:01 +0000 Subject: [PATCH] Update 'cv3/program.c' --- cv3/program.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/cv3/program.c b/cv3/program.c index 3ef9ab9..a2f5b40 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,6 +1,9 @@ #include +#include +#include #define MAX_COEFFICIENTS 100 +#define BUFFER_SIZE 1024 double evaluatePolynomial(double x, double coefficients[], int n) { double result = 0.0; @@ -11,23 +14,31 @@ double evaluatePolynomial(double x, double coefficients[], int n) { } int main() { + char buffer[BUFFER_SIZE]; double x, coef; double coefficients[MAX_COEFFICIENTS]; int count = 0; - if (scanf("%lf", &x) != 1) { - printf("Nepodarilo sa nacitat zaklad x\n"); - return 0; + + printf("Enter the value of x: "); + fgets(buffer, BUFFER_SIZE, stdin); + if (sscanf(buffer, "%lf", &x) != 1) { + printf("Nepodarilo sa nacitat zaklad x\n"); + return 0; } - while (scanf("%lf", &coef) == 1) { - coefficients[count++] = coef; - } - - - if (!feof(stdin)) { - printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1); - return 0; + printf("Enter coefficients from highest to lowest degree, finish with an empty line:\n"); + 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);