pvjc24/cv3/program.c
2024-03-03 18:10:44 +01:00

76 lines
1.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE_OF_ARRAY 100
#define LINE_SIZE 2
double powering(double, int);
int main()
{
double result_of_main=0;
double array_of_numbers[SIZE_OF_ARRAY];
int counter=0;
int result;
double pow_result;
while(1){
double x=0;
result=reading_input(&x);
if(result==0||result==2){
break;
}
array_of_numbers[counter]=x;
counter++;
}
if(result==0){
if(counter==0){
printf("Nepodarilo sa nacitat zaklad x\n");
}else{
printf("Nepodarilo sa nacitat polynom na %d mieste.\n",counter);
}
return 0;
}
for(int j=1; j<counter; j++){
//double rawPow = pow(array_of_numbers[0],(counter-j)-1);
//double roundedPos = round(rawPow*100)/100;
double true_power=powering(array_of_numbers[0],(counter-j)-1);
// printf("RAW: %f, ROUNDED: %f, true %f\n", rawPow, roundedPos,true_power);
result_of_main += array_of_numbers[j]*true_power;
}
printf("Vysledok je: %.2f\n",round(result_of_main * 100) / 100);
return 0;
}
//ceil(result_of_main * 100) / 100)
int reading_input(double *number){
char buffer[SIZE_OF_ARRAY];
fgets(buffer,SIZE_OF_ARRAY,stdin);
for(int j=0; buffer[j]!='\0'; j++){
if (buffer[j]>='0'&&buffer[j]<='9') {
continue;
}
if (buffer[j]=='.') {
continue;
}
if(buffer[j]=='\n'){
buffer[j]='\0';
break;
}
return 0;
}
if(buffer[0]=='\0'){
return 2;
}
*number=atof(buffer);
return 1;
}
double powering(double number, int counter){
double result=1;
for(int i=0; i<counter; i++){
result =result * number;
}
return result;
}