标题:用递归函数求数值的整数次幂
只看楼主
wenwenl310
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-12-4
 问题点数:0 回复次数:2 
用递归函数求数值的整数次幂
代码如下:
#include<stdio.h>

double power_positive(double n,int p);
double power_negative(double n,int p);

int main(void)
{
 double x,xpow;
 int exp;
 printf("Enter a number and the integer power to which\n");
 printf("the number will be raised.Enter q to quit.\n");
 while(scanf("%lf %d",&x,&exp)==2)
 {
  if(x==0)
   printf("%lf to the power of %d is 0.\n",x,exp);
  else if(exp==0)
   printf("%lf to the power of %d is 1.\n",x,exp);
  else if(exp>0)
  {
   xpow=power_positive(x,exp);
   printf("%lf to the power of %d is %lf.\n",x,exp,xpow);
  }
  else
  {
   xpow=power_negative(x,exp);
   printf("%lf to the power of %d is %lf.\n",x,exp,xpow);
  }
  printf("Enter next pair of numbers or q to quit.\n");
 }
 printf("Hope you enjoyed this power trip --BYE!\n");

 return 0;

}

double power_positive(double n,int p)
{
 double pow=1;
 if(p>0)
  pow=n*power_positive(n,(p-1));

 return pow;
}

double power_negative(double n,int p)
{
 double pow = 1;
 int q;
 q=-p;

 if(q>0)
  pow = power_negative(n,1-q) / n;

 return pow;
}

}
搜索更多相关主题的帖子: include double number Enter power 
2011-12-04 17:50
czz5242199
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:660
专家分:2400
注 册:2011-10-26
得分:0 
不懂你想表达什么
2011-12-04 18:20
阳光110
Rank: 1
来 自:四川
等 级:新手上路
帖 子:25
专家分:1
注 册:2011-10-21
得分:0 
你照书上搬下来的吗???
2011-12-04 19:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-356943-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016726 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved