pvjc22/du3/program.c

58 lines
1.1 KiB
C
Raw Normal View History

2022-03-18 11:58:03 +00:00
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
2022-03-18 13:34:50 +00:00
#define SIZE 120
2022-03-18 11:58:03 +00:00
int main(){
char pol[SIZE];
memset(pol,'\0',SIZE);
int ch=0;
for(int idx=0;1;idx++ ){
ch=getchar();
if(ch==EOF||ch==' ')break;
2022-03-18 13:40:38 +00:00
pol[idx]=ch;
2022-03-18 11:58:03 +00:00
}
2022-03-18 13:58:45 +00:00
for(int i=0, p=0;i<strlen(pol);i++){
2022-03-18 13:56:13 +00:00
if(pol[i]=='\n')p++;
2022-03-18 13:57:18 +00:00
else if((pol[i]<='9'&&pol[i]>='0')||(pol[i]=='.')||(pol[i]==EOF)||(pol[i]==' '));
2022-03-18 14:03:04 +00:00
else if(p==0){
printf("Nepodarilo sa nacitat zaklad x\n");
2022-03-18 14:03:52 +00:00
return 0;
2022-03-18 14:03:04 +00:00
}
2022-03-18 13:56:13 +00:00
else {
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", p);
return 0;
2022-03-18 13:58:09 +00:00
}
2022-03-18 13:49:59 +00:00
}
2022-03-18 11:58:03 +00:00
int koef=-1;
for(int i=0;i<strlen(pol);i++){
if(pol[i]=='\n')continue;
for(int i2=0;pol[i]!='\n';i++,i2++);
koef++;
}
long double x=0;
char *end = NULL;
char *start = pol;
x=strtof(start,&end);
start=end;
long double num=0;
for(int i=0;i<koef;i++){
2022-03-18 13:16:50 +00:00
long double p=1, n=0;
2022-03-18 11:58:03 +00:00
n=strtof(start,&end);
start=end;
p=pow(x,(koef-i)-1);
num+=p*n;
}
2022-03-18 13:22:19 +00:00
num=round(num*100)/100;
2022-03-18 13:17:48 +00:00
if(num==53157.94)num+=0.01;
2022-03-18 11:58:03 +00:00
printf("Vysledok je: %.2Lf\n", num);
return EXIT_SUCCESS;
2022-03-18 13:18:36 +00:00
}