Calculation formula: Pn(x) = 1, n=0 Pn(x) = 2x, n=1 Pn(x) =2*Pn-1(x)-2(n-1)Pn-2(x), n>1
Non-recursive use of the stack to implement the recursive computation of the function.
//定义结构体
Struct Stack{
int id; //函数参数
double val; //当前数值
}S;
//使用栈保存每一次计算的结果
Double Pfun(int n, int x){ //n:当前参数,x为当前结果
InitStack(S);
double e;
for(int i = 1;i<=n;i++){
Push(S,i); //每一次将当前位置传入栈
}
while(!IsEmpty(S)){ //栈不为空时取出元素进行递归计算后压栈继续递归计算
GetTop(S,e);
e.val = 2*Pfun(n-1,x)-2(e.id-1)*Pfun(n-2,x);
pop(S,e);
}
if(n==0)
return 1;
else if(n==1)
return 2*x;
}