/*
============================================================
使用递归函数重做练习7.
============================================================
*/
#include <stdio.h>
double power(double n, int p);
int main(void)
{
double n;
int p;
printf("Enter a number and the positive integer power to which\n");
printf(" the number will be raised. Enter q to quit.\n");
while(scanf_s("%lf %d", &n, &p) == 2)
{
printf("%g to the power %d is %g\n", n, p, power(n, p));
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
return 0;
}
double power(double n, int p)
{
double pow;
if(n == 0) return 0;
else if(p == 0) return 1;
else if(p > 0) {
pow = n * power(n, p-1);
return pow;
}
else if(p < 0) {
pow = power(n, p+1) / n;
return pow;
}
}
C primer plus 第九章 练习8:
最新推荐文章于 2024-07-25 21:33:09 发布
本文详细解答了C Primer Plus这本书第九章的练习题目,涵盖了指针、内存管理和数组操作等核心概念,帮助读者深入理解C语言的底层机制。
摘要由CSDN通过智能技术生成