C语言实现一维多项式求值

/*
 * plyv.h
 *
 *  Created on: Oct 14, 2010
 *      Author: jenson
 */

#ifndef PLYV_H_
#define PLYV_H_

/**
 *一维多项式求值
 */
extern double plyv_one_dimension(double * a,int n,double x);

#endif /* PLYV_H_ */

/*
 * plyv.c
 *
 *  Created on: Oct 14, 2010
 *      Author: jenson
 */

#include "plyv.h"
#include <stdio.h>
#include <stdlib.h>
double plyv_one_dimension(double * a,int n,double x){

    if(a!=NULL){
       int i = 0;
       double u = a[n-1];
       for(i = n -2;i>=0;i--){
           u = u*x + a[i];
       }
       return u;
    }
}

/*
 * main.c
 *
 *  Created on: Oct 14, 2010
 *      Author: jenson
 */

#include "include/plyv.h"

int main() {

    int i = 0;

    double a[7] = { -20.0, 7.0, -7.0, 1.0, 3.0, -5.0, 2.0 };
    double x[6] = { 0.9, -0.9, 1.1, -1.1, 1.3, -1.3 };

    for (i = 0; i <= 5; i++) {
        double value = plyv_one_dimension(a, 7, x[i]);
        printf("x(%d)=%5.21f\tp(%d)=%13.7e\n",i,x[i],i,value);
    }

    return 0;
}

你可能感兴趣的:(算法,职场,休闲)