pvjc25/du2/program.c

51 lines
1.0 KiB
C
Raw Normal View History

2025-03-06 12:59:38 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LINE_SIZE 100
int read_double(double *num){
char line[LINE_SIZE];
2025-03-06 14:49:51 +00:00
if(!fgets(line, LINE_SIZE, stdin)){
2025-03-06 12:59:38 +00:00
return 0;
}
char *endptr;
*num = strtod(line, &endptr);
if(endptr == line || (*endptr != '\0' && *endptr != '\n')){
2025-03-06 15:07:05 +00:00
return 0;
2025-03-06 12:59:38 +00:00
}
return 1;
}
2025-03-06 14:49:51 +00:00
int main(){
2025-03-06 12:59:38 +00:00
double x, coef, result = 0.0;
int coef_index = 0;
2025-03-06 15:07:05 +00:00
if(!read_double(&x)){
2025-03-06 12:59:38 +00:00
printf("Chyba: Nepodarilo sa nacitat hodnotu x.\n");
2025-03-06 15:07:05 +00:00
return 0;
2025-03-06 12:59:38 +00:00
}
2025-03-06 15:07:05 +00:00
while(read_double(&coef)){
2025-03-06 12:59:38 +00:00
if(coef_index == 0){
result = coef;
}else{
result = result * x + coef;
}
coef_index++;
}
2025-03-06 14:49:51 +00:00
if(read_status == -1){
2025-03-06 15:07:05 +00:00
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", coef_index + 1);
2025-03-06 14:53:18 +00:00
return 0;
2025-03-06 14:49:51 +00:00
}
2025-03-06 12:59:38 +00:00
if(coef_index == 0){
printf("Chyba: Nepodarilo sa nacitat ziadne koeficienty.\n");
2025-03-06 15:07:05 +00:00
return 0;
2025-03-06 12:59:38 +00:00
}
2025-03-06 15:07:05 +00:00
printf("Vysledok je: %.2f\n", round(result * 100) / 100);
2025-03-06 12:59:38 +00:00
return 0;
2025-03-06 15:07:05 +00:00
}