pvjc24/cv3/program.c

49 lines
1.1 KiB
C
Raw Normal View History

2024-02-27 09:04:02 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX_LINE_LENGTH 256
bool IsValidNumber(const char *line) {
char *endFtr;
strtod(line, &endFtr);
2024-02-28 14:46:06 +00:00
return endFtr != line;
2024-02-27 09:04:02 +00:00
}
int main() {
char line[MAX_LINE_LENGTH];
double result = 1;
int coefficientIndex = 0;
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
printf("Nepodarilo sa nacitat zaklad x\n");
return 1;
}
2024-02-28 14:49:44 +00:00
double x_base = strtod(line, NULL);
2024-02-27 09:04:02 +00:00
while (1) {
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
2024-02-28 14:46:06 +00:00
if (line[0] == '\n') {
break;
}
2024-02-27 09:04:02 +00:00
printf("Nepodarilo sa nacitat polynom na %d mieste. \n", coefficientIndex);
return 1;
}
char *endPtr;
2024-02-28 14:49:44 +00:00
double coefficient = strtod(line, &endPtr);
2024-02-27 09:04:02 +00:00
if (*endPtr != '\n' && *endPtr != '\0') {
printf("Nespravny format cisla\n");
return 1;
}
2024-02-28 14:49:44 +00:00
result = result * x_base + coefficient;
2024-02-28 14:46:06 +00:00
coefficientIndex++;
2024-02-27 09:04:02 +00:00
}
2024-02-27 09:13:05 +00:00
printf("Vysledok je: %.2f\n", result);
2024-02-27 09:04:02 +00:00
return 0;
}