pvjc24/cv3/program.c

84 lines
1.4 KiB
C
Raw Normal View History

2024-03-07 10:00:55 +00:00
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#define POLE 100
int main() {
double X = 0.00;
char vstup[POLE];
memset(vstup, 0,POLE); //osetrenie
char* a = fgets(vstup,POLE,stdin);
if (vstup[0] == '\n'){ //ked pride na enter
printf("koniec");
exit(0);
}
for (int i = 0; i < strlen(vstup)-1; i++){
//X = atof(riadok);
if(vstup[i] == '.'){
continue;
}
if (isdigit(vstup[i]) == 0){
2024-03-07 11:11:57 +00:00
printf ("Nepodarilo sa nacitat zaklad x\n");
2024-03-07 10:00:55 +00:00
exit(0);
}
}
X = atof(a);
double polynom = 0.00;
char Ppole [POLE];
memset(Ppole, 0,POLE);
double rovnica [POLE];
int index = 0;
int stupen = 0;
double vysledok = 0.00;
int idx = 0;
2024-03-07 10:57:31 +00:00
int pocitadlo = 0;
2024-03-07 10:00:55 +00:00
while (1){
char* b = fgets(Ppole,POLE,stdin);
2024-03-07 10:57:31 +00:00
pocitadlo++;
2024-03-07 10:00:55 +00:00
if (Ppole[0] == '\n'){ //ked pride na enter
break;
}
for (int i = 0; i < strlen(Ppole)-1; i++){
//X = atof(riadok);
if(Ppole[i] == '.'){
continue;
}
if (isdigit(Ppole[i]) == 0){
2024-03-07 10:57:31 +00:00
printf ("Nepodarilo sa nacitat polynom na %d mieste.\n", pocitadlo);
2024-03-07 10:00:55 +00:00
exit(0);
}
}
polynom = atof (b);
rovnica[index] = polynom;
index++;
stupen++;
}
for (int i = stupen -1; i >= 0; i --){
vysledok = vysledok + rovnica[idx] * pow(X, i);
idx++;
}
vysledok = round (vysledok * 100)/100;
2024-03-07 10:26:49 +00:00
printf ("Vysledok je: %.2f\n", vysledok);
2024-03-07 10:00:55 +00:00
return 0;
}