Обновить du2/program.c
This commit is contained in:
parent
62b5936b83
commit
13a212c122
@ -5,13 +5,16 @@
|
|||||||
|
|
||||||
#define LINE_SIZE 100
|
#define LINE_SIZE 100
|
||||||
|
|
||||||
|
// Funkcia na bezpečné načítanie čísla
|
||||||
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í
|
||||||
}
|
}
|
||||||
|
// Odstránenie nového riadka
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -19,9 +22,10 @@ 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); // Zobrazenie správneho indexu
|
||||||
return -1;
|
return -1; // Signalizujeme chybu
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -34,15 +38,15 @@ int main() {
|
|||||||
|
|
||||||
double coef;
|
double coef;
|
||||||
double result = 0;
|
double result = 0;
|
||||||
int coef_count = 2;
|
int coef_count = 1; // Koeficienty začíname indexovať od 1 (pre x^1)
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
int status = read_double(&coef, coef_count);
|
int status = read_double(&coef, coef_count + 1); // Tu používame coef_count + 1 pre správny index
|
||||||
if (status == -1) {
|
if (status == -1) {
|
||||||
return 0;
|
return 0; // Očakávané ukončenie pri neplatnom vstupe
|
||||||
}
|
}
|
||||||
if (status == 0) {
|
if (status == 0) {
|
||||||
if (coef_count == 2) {
|
if (coef_count == 1) { // Ak nebol zadaný žiadny koeficient
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user