C primer plus (第六版)第九章 编程练习第8,9题

题目:

8.第6章的程序清单6.20中,power()函数返回⼀个double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使⽤⼀个循环,并在程序中测试该函数。

9.使⽤递归函数重写编程练习8。

思路:

  1. 用if...else...条件语句分别处理正整数次幂,负整数次幂和0次幂,8题用for循环语句取值计算;
cpp 复制代码
//程序清单6.20 power.c程序
// power.c -- 计算数的整数幂
#include <stdio.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)
     {
          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)    // 原本6.20配套函数定义,只能计算正整数次幂
// {
//      double pow = 1;
//      int i;
//      for (i = 1; i <= p; i++)
//           pow *= n;
//      return pow;                // 返回pow的值
// }

double power(double n, int p)
{
    double pow = 1;
    int i;
    if (p > 0 && n != 0)            //计算正整数次幂
    {
     for (i = 1; i <= p; i++)
        pow *= n;
     return pow;  
    }
    else if ( p < 0 && n != 0)      //计算负整数次幂
    {
     for (i = 1; i <= (0-p); i++)
        pow *= (1.0/n);
     return pow;  
    }
    else if (p == 0 && n != 0)      //0次幂情况,
    {
        return 1;
    }
    else if (p == 0 && n == 0)      //0的0次幂情况;
    {
        printf("The power of 0 to the power of 0 is undefined, so the value is treated as 1\n");
        return 1;
    }
}
  1. 9题用递归取代for循环;
cpp 复制代码
//程序清单6.20 power.c程序
// power.c -- 计算数的整数幂
//8.第6章的程序清单6.20中,power()函数返回⼀个double类型数的正整数次幂。
//改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何
// 数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。
//要使⽤⼀个循环,并在程序中测试该函数。
//9.使⽤递归函数重写编程练习8。
#include <stdio.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)
     {
          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;
    if (p > 0)
    {
        pow *= n*power(n, p-1);
        return pow;  
    }
    else if ( p < 0 )
    {
        pow *= (1.0/n)*power(n,p+1);
        return pow;  
    }
    else if (p == 0 && n != 0)
    {
        return 1;
    }
    else if (p == 0 && n == 0)
    {
        printf("The power of 0 to the power of 0 is undefined, so the value is treated as 1\n");
        return 1;
    }
}
相关推荐
艾莉丝努力练剑1 小时前
【C语言16天强化训练】从基础入门到进阶:Day 6
c语言·数据结构·学习·算法
草莓熊Lotso2 小时前
【C语言强化训练16天】--从基础到进阶的蜕变之旅:Day10
c语言·开发语言·经验分享·算法·强化
草莓熊Lotso4 小时前
【C++】--函数参数传递:传值与传引用的深度解析
c语言·开发语言·c++·其他·算法
qq_411262425 小时前
为什么会“偶发 539/500 与建连失败”
服务器·c语言·网络·智能路由器
Jenkinscao5 小时前
我从零开始学习C语言(13)- 循环语句 PART2
c语言·开发语言·学习
白书宇5 小时前
12.从零开始写LINUX内核--控制台初始化
linux·c语言·驱动开发·嵌入式硬件·microsoft·iot
我是一只菜菜5 小时前
中国大学MOOC--C语言第十一周结构类型
c语言·开发语言
你好,我叫C小白16 小时前
C语言 常量,数据类型
c语言·开发语言·数据类型·常量
多吃蔬菜!!!18 小时前
vscode 搭建C/C++开发环境搭建(linux)
linux·c语言·c++
爱和冰阔落1 天前
从关机小游戏学 C 语言:分支循环 + 关键字(break/continue)实战
c语言·开发语言