代码细节需注意
来源:互联网 发布:php乘法口诀 编辑:程序博客网 时间:2024/05/17 09:32
将下面的代码变为递归函数循环
if(b>1)
q=abc(a,b);
b--;
原来的代码:
// power.c -- 计算数值的整数次幂
#include <stdio.h>
#include <stdbool.h>
double power(double n, int p); // ANSI 原型
int main(void)
{
double x, xpow;
int exp;
#include <stdio.h>
#include <stdbool.h>
double power(double n, int p); // ANSI 原型
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2)
{
if(x==0) xpow=0;
if(exp==0) xpow=1;
else xpow = power(x,exp); // 函数调用
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2)
{
if(x==0) xpow=0;
if(exp==0) xpow=1;
else xpow = power(x,exp); // 函数调用
printf("%.3g to the power %d is %.5g\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;
bool q;
q=false;
{
double pow = 1;
int i;
bool q;
q=false;
if(p<0) p=-p,q=true;
for (i = 1; i <= p; i++)
pow *= n;
if(q)
pow=1/pow;
for (i = 1; i <= p; i++)
pow *= n;
if(q)
pow=1/pow;
return pow; // 返回pow值
}
}
改为递归循环
// power.c -- 计算数值的整数次幂
#include <stdio.h>
#include <stdbool.h>
double abc(double a,int b);
double power(double n, int p); // ANSI 原型
int main(void)
{
double x, xpow;
int exp;
#include <stdio.h>
#include <stdbool.h>
double abc(double a,int b);
double power(double n, int p); // ANSI 原型
int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2)
{
if(x==0) xpow=0;
if(exp==0) xpow=1;
else xpow = power(x,exp); // 函数调用
printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d", &x, &exp) == 2)
{
if(x==0) xpow=0;
if(exp==0) xpow=1;
else xpow = power(x,exp); // 函数调用
printf("%.3g to the power %d is %.5g\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;
int i;
bool q;
q=false;
{
double pow;
int i;
bool q;
q=false;
if(p<0) p=-p,q=true;
pow=abc(n,p);
if(q) pow=1/pow;
return pow; // 返回pow值
}
}
double abc(double a,int b)
{
double q;
q=1;
{
double q;
q=1;
if(b>1)
q=abc(a,b);
b--;
q*=a;
return q;
}
return q;
}
时发现可运行但输入时会未响应
于是带入值 1 2
开始研究函数
原来是没注意b--使递归成了死循环
于是把b--;
放到q=abc(a,b);
后面还是不行,
后面还是不行,
就干脆放到if前面
运行通过
- 代码细节需注意
- 代码注意细节
- 算法需注意细节
- 写代码要注意的细节
- 注意细节,提高代码运行效率
- 写代码要注意的细节
- Java代码优化必须注意的细节
- 生活细节需注意一下!
- Java编码需注意细节
- Netty所需注意细节
- php 需注意的细节
- 注意细节
- 注意细节
- 注意细节
- 注意细节
- 注意细节
- 注意细节
- AS3常用代码整理及细节优化注意
- 使用ActivityGroup来切换Activity和Layout
- 被除数为0的悲剧
- JSTL 入门: 探讨 core
- 目前不太懂的输出问题已解决
- 关于jdbc内嵌事务(结合spring的传播属性)
- 代码细节需注意
- 浅谈 "高内聚、低耦合" 这对兄弟
- JS分页代码
- 再谈免费馅饼
- 再看阿里云
- 字体对话框
- php防止重复定义常量 .
- can总线(三)--如何通过波形解析can总线数据
- LAMP环境配置