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;
    }
}
相关推荐
丛雨要玩游戏2 小时前
字符函数和字符串函数
c语言·开发语言·算法
ad钙奶长高高2 小时前
【C语言】初始C语言
c语言·开发语言·算法
侯小啾2 小时前
【17】C语言-gets() 与 fgets() 函数
c语言·开发语言
是苏浙3 小时前
零基础入门C语言之枚举和联合体
c语言·开发语言
ACP广源盛139246256735 小时前
(ACP广源盛)GSV2231---DisplayPort 1.4 MST 到 HDMI 2.0/DP/Type-C 转换器(带嵌入式 MCU)
c语言·开发语言·单片机·嵌入式硬件·音视频·mst
QT 小鲜肉5 小时前
【QT/C++】Qt网络编程进阶:UDP通信和HTTP请求的基本原理和实际应用(超详细)
c语言·网络·c++·笔记·qt·http·udp
Yurko136 小时前
【C语言】选择结构和循环结构的进阶
c语言·开发语言·学习
范纹杉想快点毕业6 小时前
12个月嵌入式进阶计划ZYNQ 系列芯片嵌入式与硬件系统知识学习全计划(基于国内视频资源)
c语言·arm开发·单片机·嵌入式硬件·学习·fpga开发·音视频
木木木丫7 小时前
嵌入式项目:韦东山驱动开发第六篇 项目总结——显示系统(framebuffer编程)
c语言·c++·驱动开发·dsp开发
柳鲲鹏7 小时前
RGB转换为NV12,查表式算法
linux·c语言·算法