pvjc24/cv3/program.c
2024-03-07 22:44:25 +01:00

40 lines
886 B
C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_LINE_LENGTH 256
bool IsValidNumber(const char *line) {
char *endFtr;
strtod(line, &endFtr);
return endFtr != line;
}
int main() {
char line[MAX_LINE_LENGTH];
double result = 0;
double x;
int lineNumber = 1;
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
printf("Nepodarilo sa nacitat zaklad x\n");
return 1;
}
x = strtod(line, NULL);
while (fgets(line, MAX_LINE_LENGTH, stdin) != NULL && IsValidNumber(line)) {
double coefficient = strtod(line, NULL);
result = result * x + coefficient;
lineNumber++;
}
if (line[0] != '\n') {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", lineNumber);
return 0;
}
printf("Vysledok je: %.2f\n", result);
return 0;
}