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 小时前
28. 找出字符串中第一个匹配项的下标
c语言·开发语言·数据结构·算法
小猪扒饭1 小时前
C基础 12_day
c语言·笔记·学习·算法
屁股割了还要学3 小时前
【数据结构入门】时间、空间复杂度的计算
c语言·开发语言·数据结构·c++·算法
DIY机器人工房5 小时前
【科普】在STM32中有哪些定时器?
c语言·嵌入式·定时器·diy机器人工房
sheepwjl7 小时前
《嵌入式C语言笔记(十七):进制转换、结构体与位运算精要》
linux·c语言·开发语言·笔记·算法
xnglan9 小时前
数据结构与算法:队列的表示和操作的实现
c语言·数据结构·算法·链表
FirstFrost --sy9 小时前
数据结构之排序
c语言·数据结构·算法·排序算法
程序员编程指南11 小时前
Qt 移动应用发布与分发指南
c语言·开发语言·c++·qt
程序员编程指南11 小时前
Qt 与物联网(IoT)开发
c语言·开发语言·c++·qt·物联网
野原鑫之祝12 小时前
嵌入式开发学习———Linux环境下IO进程线程学习(一)
linux·c语言·学习·vim·嵌入式