[数值算法]多项式的秦九韶算法

多项式的秦九韶算法,一般数值计算一类课程的”Hello World”级的内容哟~~~~

做了递归和非递归两个版本:

/*UnRecursive Version*/

Type qjz(Type* pArr,Type x,int n)

       {

             Type ans=0;

             asserts(pArr!=NULL,"in qjz:pass in arr error");

             asserts(n>=0,"in qjz :pass n is <0");

              n--;

              ans=pArr[n];

              while(n>0)

              {

                     n--;

                     ans=pArr[n]+x*ans;

              }

              return ans;

       }

      

/*Recursive Version*/

Type qjzRecursive(Type* pArr,Type x,int n,int currentNum)

       {

           asserts(pArr!=NULL,"in qjzRecursive:pass in arr error");

              if(currentNum+1==n)return pArr[currentNum];

              else return pArr[currentNum]+x*qjzRecursive(pArr,x,n,currentNum+1);

       }

      

/*test program*/      

#include "qjz.h"

#include

#include

void main()

{

      

       double a[]={1,2,3,4,5};

       double x=11;

       double ans=qjz(a,x,5);

       clrscr();

       printf("The answer is:%f",ans);

       ans=qjzRecursive(a,x,5,0);

       printf("Using the Recursive,ans is:%f",qjzRecursive(a,x,5,0));

}

你可能感兴趣的:([数值算法]多项式的秦九韶算法)