#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
double QIP(int n,double t,double *x,double *y)
{
int i,k;
double u,v,w,x0,x1,x2,f;
for(i=0;i<=n-4;i++)
{
if(t<=x[i+1])
{
k=i;
if((i!=1)&&(t-x[i]<x[i+1]-t))
k-=1;
}
else
k=n-3;
}
x0=x[k];
x1=x[k+1];
x2=x[k+2];
u=(t-x1)*(t-x2)/((x0-x1)*(x0-x2));
v=(t-x0)*(t-x2)/((x1-x0)*(x1-x2));
w=(t-x0)*(t-x1)/((x2-x0)*(x2-x1));
f=u*y[k]+v*y[k+1]+w*y[k+2];
return (f);
}
double QIP(int ,double,double *,double *);
main()
{
int i,n;
double *x,*y,t,f;
n=6;
t=75.5;
x=(double *)calloc(n,sizeof(double));
if(x==NULL)
exit(1);
y=(double *)calloc(n,sizeof(double));
if(y==NULL)
exit(1);
x[0]=75;
x[1]=76;
x[2]=77;
x[3]=78;
x[4]=79;
x[5]=80;
y[0]=2.768;
y[1]=2.833;
y[2]=2.903;
y[3]=2.979;
y[4]=3.062;
y[5]=3.153;
printf("使用拉哥朗日算法:\n");
printf("当x=75.5时,结果为 %f \n",QIP(n,t,x,y));
t=78.3;
printf("当x=78.3时,结果为 %f \n",QIP(n,t,x,y));
free(x);
free(y);
}