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

This commit is contained in:
Bohdana Marchenko 2025-03-06 10:53:15 +00:00
parent 8ec7513538
commit 1c40d2ffff

View File

@ -8,11 +8,11 @@
int read_double(double *value, int coef_index) { int read_double(double *value, int coef_index) {
char line[LINE_SIZE]; char line[LINE_SIZE];
if (fgets(line, LINE_SIZE, stdin) == NULL) { if (fgets(line, LINE_SIZE, stdin) == NULL) {
return 0; return 0; // Chyba pri čítaní
} }
line[strcspn(line, "\r\n")] = 0; line[strcspn(line, "\r\n")] = 0;
// Skontrolujeme, či je riadok prázdny
if (strlen(line) == 0) { if (strlen(line) == 0) {
return 0; return 0;
} }
@ -20,6 +20,7 @@ int read_double(double *value, int coef_index) {
char *endptr; char *endptr;
*value = strtod(line, &endptr); *value = strtod(line, &endptr);
// Ak nebolo načítané číslo alebo sú tam neplatné znaky
if (endptr == line || *endptr != '\0') { if (endptr == line || *endptr != '\0') {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index); printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index);
return 0; return 0;
@ -30,7 +31,6 @@ int read_double(double *value, int coef_index) {
int main() { int main() {
double x; double x;
if (!read_double(&x, 1)) { if (!read_double(&x, 1)) {
printf("Chyba: Nepodarilo sa načítať hodnotu x.\n");
return 1; return 1;
} }
@ -41,10 +41,9 @@ int main() {
while (1) { while (1) {
if (!read_double(&coef, coef_count + 2)) { if (!read_double(&coef, coef_count + 2)) {
if (coef_count == 0) { if (coef_count == 0) {
printf("Chyba: Neboli zadané žiadne koeficienty.\n");
return 1; return 1;
} }
return 0; break;
} }
result = result * x + coef; result = result * x + coef;
coef_count++; coef_count++;