Обновить du2/program.c

This commit is contained in:
Bohdana Marchenko 2025-03-06 10:40:23 +00:00
parent 200ec08718
commit caea955c39

View File

@ -1,38 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#define LINE_SIZE 100
int read_double(double *value) {
char line[LINE_SIZE];
if (fgets(line, LINE_SIZE, stdin) == NULL) {
return 0; // Chyba pri čítaní
}
char *endptr;
*value = strtod(line, &endptr);
if (endptr == line || (*endptr != '\0' && *endptr != '\n')) {
return 0; // Neplatný vstup
}
return 1;
}
int main() {
char riadok[LINE_SIZE];
memset(riadok, 0, LINE_SIZE);
double sum = 0.0;
printf("Zadajte čísla: \n");
while (fgets(riadok, LINE_SIZE, stdin) != NULL) {
size_t len = strlen(riadok);
if (len > 0 && riadok[len - 1] == '\n') {
riadok[len - 1] = '\0';
}
if (strlen(riadok) == 0) {
double x;
if (!read_double(&x)) {
printf("Chyba: Nepodarilo sa načítať hodnotu x.\n");
return 1;
}
double coef;
double result = 0;
int coef_count = 0;
while (1) {
if (!read_double(&coef)) {
if (coef_count == 0) {
printf("Chyba: Neboli zadané žiadne koeficienty.\n");
return 1;
}
break;
}
char* endptr = NULL;
double cislo = strtod(riadok, &endptr);
if (*endptr != '\0') {
printf("Neplatný vstup! Prosím, zadávajte iba čísla.\n");
return 1;
}
sum += cislo;
result = result * x + coef;
coef_count++;
}
printf("Vysledok je: %.2f\n", sum);
printf("Vysledok je: %.2f\n", result);
return 0;
}
}