#include <stdio.h>
long power(int x, int y);
long factorial(int n);
double e(int x);
void main( )
{
int x;
double ex;
printf("Enter x:");
scanf("%d", &x);
ex = e(x);
printf("\ne^%d=%f", x, ex);
}
long power(int x, int y)
/* 计算xy */
{
long p = 1;
int i;
for (i=1; i<=y; i++)
p = p*x;
return p;
}
long factorial(int n)
/* 计算n! */
{
int i;
long f = 1;
for (i=2; i<=n; i++)
f = f*i;
return f;
}
double e(int x)
/* 计算ex */
{
double result = 1.0, tmp;
int i = 1;
if( x>0 )
do {
tmp = power(x,i)*1.0/factorial(i++);
result += tmp;
} while( i <= 30 ); /* 前30项 */
return result;
}