c 程序 函数

来源:百度知道 编辑:UC知道 时间:2024/05/21 07:21:40
用下面的公式计算e的x次方。在程序中要求用函数f1计算每项分子的值,用函数f2计算每项分母的值(用递归函数来实现)。通过主函数调用f1和f2完成计算。

int f1(int t, int x)
{
if (t==0) return 1;
else return x * f1(t-1,x);
}

int f2(int t)
{
if (t==0) return 1;
else return t * f2(t-1);
}

void main()
{
int t=0, x;
float result = 0.0;
float temp;
printf("please input x\n");
scanf("%d",&x);
while(t<10)
{
temp = (float)f1(t,x) / (float)f2(t);
result += temp;
t++;
}
printf("result is %f\n",result);
}