Обновить du2/program.c
This commit is contained in:
parent
200ec08718
commit
caea955c39
@ -1,38 +1,46 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#define LINE_SIZE 100
|
#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() {
|
int main() {
|
||||||
char riadok[LINE_SIZE];
|
double x;
|
||||||
memset(riadok, 0, LINE_SIZE);
|
if (!read_double(&x)) {
|
||||||
double sum = 0.0;
|
printf("Chyba: Nepodarilo sa načítať hodnotu x.\n");
|
||||||
|
return 1;
|
||||||
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) {
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Vysledok je: %.2f\n", sum);
|
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;
|
||||||
|
}
|
||||||
|
result = result * x + coef;
|
||||||
|
coef_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Vysledok je: %.2f\n", result);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user