📄 ex5_14.cpp
字号:
// Ex5_14.cpp (based on Ex5_01.cpp)
// A recursive version of x to the power n
#include <iostream>
using std::cout;
using std::endl;
double power(double x, int n); // Function prototype
int main(void)
{
double x = 2.0; // Different x from that in function power
double result = 0.0;
// Calculate x raised to powers -3 to +3 inclusive
for(int index = -3 ; index<=3 ; index++)
cout << x << " to the power " << index << " is " << power(x, index)<< endl;
return 0;
}
// Recursive function to compute integral powers of a double value
// First argument is value, second argument is power index
double power(double x, int n)
{
if(n < 0)
{
x = 1.0/x;
n = -n;
}
if(n > 0)
return x*power(x, n-1);
else
return 1.0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -