pvjc26/du1/program.c
2026-03-05 12:50:36 +00:00

81 lines
1.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define LINE_SIZE 100
int vyhodnot_vstup (float *num){
//return value podla toho ci je input validny alebo nie
char vstup[LINE_SIZE];
memset (vstup, 0, LINE_SIZE);
char *citaj = fgets (vstup, LINE_SIZE, stdin);
if (citaj == NULL) return -1; //-1 ak je NULL
else if (vstup[0] == '\n') return -2; //-2 ak sa vyenteroval input
char *ukoncovaci;
float hodnota = strtof (vstup, &ukoncovaci);
if (ukoncovaci == vstup) return -3; //-3 ak sa nenacitalo nic
*num = hodnota; //updatnem float s realnou hodnotou z stdin
return 1;
}
int main(){
float num = 0;
//prepis vstupu do premennej
int vyhodnotenie = vyhodnot_vstup(&num);
//vyhodnotenie vysledkov pomocnej funkcie
if (vyhodnotenie == -1) {
printf("Vstupny input je NULL\n");
exit(1);
}
if (vyhodnotenie == -2) {
printf("Vstupny input je prazdny\n");
exit(1);
}
if (vyhodnotenie == -3) {
printf("Vstupny input sa nepodarilo precitat\n");
exit(1);
}
//nejak horner
float vysledok = 0;
float koeficient[LINE_SIZE];
memset (koeficient, 0, sizeof(koeficient));
int idx = 0;
while (1) {
int vyhodnotenie = vyhodnot_vstup(&koeficient[idx]);
if (vyhodnotenie == -2) {
break;
} else
if (vyhodnotenie == -1) {
printf("Vstupny input je NULL\n");
exit(1);
} else
if (vyhodnotenie == -3) {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", idx + 1);
exit(1);
} else {
idx++;
}
}
if (idx == 0) {
printf("Nebol nacitany ziaden vstup\n");
exit(1);
}
for (int i = 0; i < idx; i++) {
vysledok = vysledok * num + koeficient[i];
}
//vysledok
printf ("Vysledok je: %.2f\n", vysledok);
return 0;
}