Update 'cv3/program.c'

This commit is contained in:
Tamáš 2024-03-08 08:38:01 +00:00
parent 20615955e6
commit 72c07053cd

View File

@ -1,6 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COEFFICIENTS 100 #define MAX_COEFFICIENTS 100
#define BUFFER_SIZE 1024
double evaluatePolynomial(double x, double coefficients[], int n) { double evaluatePolynomial(double x, double coefficients[], int n) {
double result = 0.0; double result = 0.0;
@ -11,23 +14,31 @@ double evaluatePolynomial(double x, double coefficients[], int n) {
} }
int main() { int main() {
char buffer[BUFFER_SIZE];
double x, coef; double x, coef;
double coefficients[MAX_COEFFICIENTS]; double coefficients[MAX_COEFFICIENTS];
int count = 0; int count = 0;
if (scanf("%lf", &x) != 1) {
printf("Nepodarilo sa nacitat zaklad x\n"); printf("Enter the value of x: ");
return 0; 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) { printf("Enter coefficients from highest to lowest degree, finish with an empty line:\n");
coefficients[count++] = coef; while (fgets(buffer, BUFFER_SIZE, stdin) && buffer[0] != '\n') {
} if (sscanf(buffer, "%lf", &coef) == 1) {
coefficients[count++] = coef;
if (count >= MAX_COEFFICIENTS) {
if (!feof(stdin)) { printf("Maximum number of coefficients exceeded.\n");
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1); return 0;
return 0; }
} else {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1);
return 0;
}
} }
double result = evaluatePolynomial(x, coefficients, count); double result = evaluatePolynomial(x, coefficients, count);