给一个自认为比较完善的程序:
#include<stdio.h>
#include<conio.h>
float func(float x,unsigned n)/*计算x的n次幂*/
{
if(n==0)
return 1;
return x*func(x,n-1);/*函数的递归调用*/
}
int jiecheng(unsigned n)/*计算n的阶乘*/
{
if(n==0)
return 1;
return n*jiecheng(n-1);/*函数的递归调用*/
}
float div(float x,unsigned n)/*计算X^n/n!*/
{
if(n==0)
return 1;
return func(x,n)/jiecheng(n);
}
void main()
{
float func(float x,unsigned n);
int jiecheng(unsigned n);
float div(float x,unsigned n);
float S=0.0,X=0.3;
int i;
unsigned n;
printf("Please input n:\n");
scanf("%d",&n);
while(n>=50)/*判断输入的n是否小于50*/
{
printf("n must be small than 50!Input again!\n");
scanf("%d",&n);
}
for(i=0;i<=n;i++)
S+=div(X,i);
printf("The result of S is:%f\n",S);
printf("Press any key to quit!\n");
getchar();
}