| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 665 人关注过本帖
标题:用递归函数求数值的整数次幂
只看楼主 加入收藏
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
快速回复:用递归函数求数值的整数次幂
数据加载中...
 
   



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

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