#include "stdio.h"
/**/
/* p(x)=2x^6 - 5x^5 + 3x^4 + x^3 - 7x^2 + 7x - 20 */
/* x=0.9/-0.9, 1.1/-1.1, 1.3/-1.3 */
double plyv(double *a, int n, double x)
{
if(NULL == a || n <= 0)
{
return 0;
}
int i;
double u = a[n-1];
for(i=n-2; i>=0; i--)
{
u = u*x + a[i];
}
return u;
}
int main()
{
int i;
double a[] = {2.0, -5.0, 3.0, 1.0, -7.0, 7.0, -20};
double x[] = {0.9, -0.9, 1.1, -1.1, 1.3, -1.3};
for(i=0; i<6; i++)
{
printf("x=%5.2lf,value=%13.7e\n", x[i], plyv(a, 7, x[i]));
}
return 0;
}
执行结果