C primer plus 第九章 练习7:
2016-05-30 14:06
260 查看
/* ============================================================ 在第6章“C控制语句:循环”的程序清单6.20中,函数power() 的功能是返回一个double类型数的某个正整数次幂。同时,用该函数 实现0的任何次幂为0,并且任何数值的0次幂为1.使用循环的方法编 写该函数并在一个程序中测试它。 ============================================================ */ #include <stdio.h> double power(double n, int p); int main(void) { double x,xpow; int exp; 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", &x, &exp) == 2) { xpow = power(x,exp); printf("%.3g to the power %d is %g\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(double n, int p) { double pow = 1; int i; if(p >= 0){ for(i=1;i<=p;i++) pow *= n; } else { for(i=1;i<= -p;i++) pow /= n; } return pow; }
相关文章推荐
- 操作系统:内存管理(概念)
- 【iOS开发】利用dispatch_once创建单例
- Android 百分比布局库(percent-support-lib) 解析与扩展
- 查询指定条数的记录
- 装虚拟机的步骤
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- (五)WebRTC手记Channel概念
- Struts2.x基础
- 第三章 统计语言模型
- Java线程同步:synchronized锁住的是代码还是对象
- spring 学习
- 监听器(Listener)在开发中的应用
- DECLARE_WAITQUEUE(wait, current)的分析
- spark 连接mysql
- Savitzky-Golay平滑去噪
- Android图片压缩
- Linux cat结合EOF
- 2016年5月27日,杨学明老师为中国移动通信集团提供内训课程与辅导服务!
- Android studio配置opencv
- 设置Sysctl.conf用以提高Linux的性能(最完整的sysctl.conf优化方案)