求函数得值
给定数组double A[N+1]表示函数f(x) = a0+a1*x^1+a2*x^2+…+anx^n的系数,写出一个函数doubleeval(double x,double* A, int N)用来求函数的值.
/*给定数组double A[N+1]表示函数f(x) = a0+a1*x^1+a2*x^2+…+anx^n的系数,写出一个函数doubleeval(double x,double* A, int N)用来求函数的值.*/ #include <stdio.h> double f(double a,int b) { if (b == 0) return 1; else return a*f(a, b - 1); } double doubleeval(double x, double* A, int N) { if (N == 0) return *A; else return (*(A + N))*f(x, N) + doubleeval(x, A, N - 1); } int main(void) { double A[10], n;//因为不支持变长数组,暂定为10 int N; N = sizeof(A) / sizeof(A[0]) - 1; printf("Enter x: "); scanf("%lf", &n); for (int i = 0; i < sizeof(A) / sizeof(A[0]); i++)//for循环用于给数组赋值 { A[i] = i + 1.3; printf("A[%d] = %f\n", i, A[i]); } printf("f(%f) = %f\n", n, doubleeval(n, A, N)); return 0; }
[此贴子已经被作者于2017-2-23 15:52编辑过]