67 lines
1.5 KiB
C
67 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#define LINE_SIZE 100 //maximalna veľkosť čiary
|
|
|
|
int read_double(double *value, int coef_index) {
|
|
char line[LINE_SIZE]; //deklaruje pole na uloženie reťazca
|
|
|
|
if (fgets(line, LINE_SIZE, stdin) == NULL) {
|
|
return 0;
|
|
}
|
|
|
|
|
|
line[strcspn(line, "\r\n")] = 0;
|
|
|
|
|
|
if (strlen(line) == 0) {
|
|
return 0;
|
|
}
|
|
|
|
char *endptr; // smernik na hľadanie konca reťazca pri prevode na číslo
|
|
*value = strtod(line, &endptr); // Prevod reťazca na dvojité číslo
|
|
|
|
|
|
if (endptr == line || *endptr != '\0') {
|
|
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index);
|
|
return -1;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
int main() {
|
|
double x;
|
|
|
|
if (!read_double(&x, 1)) {
|
|
return 1;
|
|
}
|
|
|
|
double coef; //uloženie koeficientu polynómu
|
|
double result = 0; // uloženie výsledku
|
|
int coef_count = 0; //Počítadlo počtu zadaných koeficientov
|
|
|
|
while (1) { //Nekonečný cyklus na zadávanie koeficientov
|
|
coef_count++;
|
|
int status = read_double(&coef, coef_count); //Číta koeficient
|
|
|
|
if (status == -1) {
|
|
return 0;
|
|
}
|
|
|
|
if (status == 0) {
|
|
if (coef_count == 1) {
|
|
return 1;
|
|
}
|
|
break;
|
|
}
|
|
|
|
result = result * x + coef;
|
|
}
|
|
|
|
|
|
printf("Vysledok je: %.2f\n", result);
|
|
return 0;
|
|
} |