#include<iostream>
using namespace std;
class Point{
public:
double x,y;
Point(double x, double y):x(x),y(y){}
};
class Lagrange{
public:
double getPrimaryFunctionValue(double x, int k);
double getValue(double x);
void setPoints(Point* ps, int n);
private:
Point* ps;
int n;
};
void Lagrange::setPoints(Point* ps, int n){
this->ps = ps;
this->n = n;
}
double Lagrange::getPrimaryFunctionValue(double x, int k){
double son = 1, mother = 1;
for(int i = 0; i < n; i++){
if(k != i){
son *= (x - ps[i].x);
mother *= (ps[k].x - ps[i].x);
}
}
return son/mother;
}
double Lagrange::getValue(double x){
double result = 0;
for(int i = 0; i < n; i++){
result += getPrimaryFunctionValue(x, i) * ps[i].y;
}
return result;
}
int main(){
Lagrange lg;
Point p[] = {Point(0.4,0.41075),Point(0.55,0.57815),Point(0.8,0.88811),Point(0.9,1.02652),Point(1,1.17520)};
lg.setPoints(p, 5);
cout<<lg.getValue(0.5)<<endl;
cout<<lg.getValue(0.7)<<endl;
cout<<lg.getValue(0.85)<<endl;
return 0;
}