define MAXTIMES 10 //插值函数最高次数
int Times; //想构造得插值次数
typedef struct {
double x;
double y;
}Point;
Point interKnot[MAXTIMES + 1];
void initData();
double interFunc(double x); //插值函数
int main()
{
double x;
initData();
printf("Input x:\n");
scanf_s("%lf", &x, sizeof(double));
printf("%f \n", interFunc(x));
while (getchar() != '\n');
getchar();
return 0;
}
void initData()
{
int i;
printf("Input the Times:");
scanf_s("%d", &Times, sizeof(int));
printf("Input the knots:(pairwise (x y))\n");
for (i = 0; i <= Times; i++)
{
scanf_s("%lf", &interKnot[i].x, sizeof(double));
scanf_s("%lf", &interKnot[i].y, sizeof(double));
}
}
double interFunc(double x)
{
int i,j;
double y = 0;
double baseFunc;
for (i = 0; i <= Times; i++)
{
baseFunc = 1;
for (j = 0; j <= Times; j++)
{
if (j == i)
continue;
else
{
baseFunc *= (x - interKnot[j].x) / (interKnot[i].x - interKnot[j].x);
}
}
y += interKnot[i].y*baseFunc;
}
return y;
}