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;
    }
}
相关推荐
张晓~1833994812130 分钟前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
纵有疾風起2 小时前
数据结构中的排序秘籍:从基础到进阶的全面解析
c语言·数据结构·算法·排序算法
夜猫逐梦3 小时前
【Lua】Windows 下编写 C 扩展模块:VS 编译与 Lua 调用全流程
c语言·windows·lua
_OP_CHEN4 小时前
数据结构(C语言篇):(十三)堆的应用
c语言·数据结构·二叉树·学习笔记·堆排序··top-k问题
量子炒饭大师4 小时前
收集飞花令碎片——C语言关键字typedef
c语言·c++·算法
cellurw5 小时前
Linux下C语言实现HTTP+SQLite3电子元器件查询系统
linux·c语言·http
..过云雨5 小时前
03.【Linux系统编程】基础开发工具1(yum软件安装、vim编辑器、编辑器gcc/g++)
linux·c语言·笔记·学习
青草地溪水旁5 小时前
Linux 高性能 I/O 事件通知机制的核心系统调用—— `epoll_ctl`
linux·c语言·c++
JasmineX-18 小时前
数据结构——顺序表(c语言笔记)
c语言·开发语言·数据结构·笔记
啟明起鸣9 小时前
【网络编程】从与 TCP 服务器的对比中探讨出 UDP 协议服务器的并发方案(C 语言)
服务器·c语言·开发语言·网络·tcp/ip·udp