与函数原型:
double power(double x, int n);
应该计算并返回x ^ n
的值。表达式power(5.0,4)
将评估计算表达式:5.0 * 5.0 * 5.0 * 5.0
,计算的结果值是:625.0
,将power()
函数实现为递归函数。
实现代码
#include <stdio.h>
double power(double x, int n);
int main(void)
{
for (double x = 2.0; x <= 5.0; x += 0.5)
{
for (int n = -4; n < 5; ++n)
printf("The value of %.2lf raised to the power %d is %.4lf\n", x, n, power(x, n));
printf("\n");
}
system("pause");
return 0;
}
//计算x的n次幂函数
double power(double x, int n) {
if (n < 0)
{
n = -n;
x = 1.0 / x;
}
if (n == 0)
return 1.0;
return x * power(x, n - 1);
}
执行上面示例代码,得到以下结果:
The value of 2.00 raised to the power -4 is 0.0625
The value of 2.00 raised to the power -3 is 0.1250
The value of 2.00 raised to the power -2 is 0.2500
The value of 2.00 raised to the power -1 is 0.5000
The value of 2.00 raised to the power 0 is 1.0000
The value of 2.00 raised to the power 1 is 2.0000
The value of 2.00 raised to the power 2 is 4.0000
The value of 2.00 raised to the power 3 is 8.0000
The value of 2.00 raised to the power 4 is 16.0000
The value of 2.50 raised to the power -4 is 0.0256
The value of 2.50 raised to the power -3 is 0.0640
The value of 2.50 raised to the power -2 is 0.1600
The value of 2.50 raised to the power -1 is 0.4000
The value of 2.50 raised to the power 0 is 1.0000
The value of 2.50 raised to the power 1 is 2.5000
The value of 2.50 raised to the power 2 is 6.2500
The value of 2.50 raised to the power 3 is 15.6250
The value of 2.50 raised to the power 4 is 39.0625
The value of 3.00 raised to the power -4 is 0.0123
The value of 3.00 raised to the power -3 is 0.0370
The value of 3.00 raised to the power -2 is 0.1111
The value of 3.00 raised to the power -1 is 0.3333
The value of 3.00 raised to the power 0 is 1.0000
The value of 3.00 raised to the power 1 is 3.0000
The value of 3.00 raised to the power 2 is 9.0000
The value of 3.00 raised to the power 3 is 27.0000
The value of 3.00 raised to the power 4 is 81.0000
The value of 3.50 raised to the power -4 is 0.0067
The value of 3.50 raised to the power -3 is 0.0233
The value of 3.50 raised to the power -2 is 0.0816
The value of 3.50 raised to the power -1 is 0.2857
The value of 3.50 raised to the power 0 is 1.0000
The value of 3.50 raised to the power 1 is 3.5000
The value of 3.50 raised to the power 2 is 12.2500
The value of 3.50 raised to the power 3 is 42.8750
The value of 3.50 raised to the power 4 is 150.0625
The value of 4.00 raised to the power -4 is 0.0039
The value of 4.00 raised to the power -3 is 0.0156
The value of 4.00 raised to the power -2 is 0.0625
The value of 4.00 raised to the power -1 is 0.2500
The value of 4.00 raised to the power 0 is 1.0000
The value of 4.00 raised to the power 1 is 4.0000
The value of 4.00 raised to the power 2 is 16.0000
The value of 4.00 raised to the power 3 is 64.0000
The value of 4.00 raised to the power 4 is 256.0000
The value of 4.50 raised to the power -4 is 0.0024
The value of 4.50 raised to the power -3 is 0.0110
The value of 4.50 raised to the power -2 is 0.0494
The value of 4.50 raised to the power -1 is 0.2222
The value of 4.50 raised to the power 0 is 1.0000
The value of 4.50 raised to the power 1 is 4.5000
The value of 4.50 raised to the power 2 is 20.2500
The value of 4.50 raised to the power 3 is 91.1250
The value of 4.50 raised to the power 4 is 410.0625
The value of 5.00 raised to the power -4 is 0.0016
The value of 5.00 raised to the power -3 is 0.0080
The value of 5.00 raised to the power -2 is 0.0400
The value of 5.00 raised to the power -1 is 0.2000
The value of 5.00 raised to the power 0 is 1.0000
The value of 5.00 raised to the power 1 is 5.0000
The value of 5.00 raised to the power 2 is 25.0000
The value of 5.00 raised to the power 3 is 125.0000
The value of 5.00 raised to the power 4 is 625.0000