主题:求助C++ 程序已经可以运行,请问如何定义符号变量,谢谢
用递归方法求n阶勒让德多项式的值,递归公式为(见图)
#include <iostream>
using namespace std;
int fac(int); //函数声明
# define x 1
int main()
{
int n,i;
float f;
cout<<"Please enter n:";
cin>>n;
if(n==0)
cout<<"p(x)="<<1<<endl;
else if(n==1)
cout<<"p(x)="<<'x'<<endl;
else
{for(i=2;i<=n;i++)
f=fac(i);
}
cout<<"p(x)="<<f<<endl;
return 0;
}
int fac(int n)
{
static float m=x;
static float k=1;
float t=0;
t=m;
m=(2*n-1)*x-m-(n-1)*k;
m=m/n;
k=t;
return m;
}
#include <iostream>
using namespace std;
int fac(int); //函数声明
# define x 1
int main()
{
int n,i;
float f;
cout<<"Please enter n:";
cin>>n;
if(n==0)
cout<<"p(x)="<<1<<endl;
else if(n==1)
cout<<"p(x)="<<'x'<<endl;
else
{for(i=2;i<=n;i++)
f=fac(i);
}
cout<<"p(x)="<<f<<endl;
return 0;
}
int fac(int n)
{
static float m=x;
static float k=1;
float t=0;
t=m;
m=(2*n-1)*x-m-(n-1)*k;
m=m/n;
k=t;
return m;
}