pvjc24/cv3/program.c
2024-02-27 10:14:54 +01:00

48 lines
1.0 KiB
C

#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);
if (endFtr == line){
return false;
}
}
int main() {
char line[MAX_LINE_LENGTH];
double result = 1;
int coefficientIndex = 0;
printf("Zadajte zaklad x: ");
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
printf("Nepodarilo sa nacitat zaklad x\n");
return 1;
}
while (1) {
if (fgets(line, MAX_LINE_LENGTH, stdin) == NULL || !IsValidNumber(line)) {
printf("Nepodarilo sa nacitat polynom na %d mieste. \n", coefficientIndex);
return 1;
}
char *endPtr;
double x = strtod(line, &endPtr);
if (*endPtr != '\n' && *endPtr != '\0') {
printf("Nespravny format cisla\n");
return 1;
}
result = result * x;
coefficientIndex++;
}
printf("Vysledok je: %.2f\n", result);
return 0;
}