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;
    }
}
相关推荐
智者知已应修善业4 小时前
【51单片机普通延时奇偶灯切换】2023-4-4
c语言·经验分享·笔记·嵌入式硬件·51单片机
晚风(●•σ )5 小时前
C++语言程序设计——11 C语言风格输入/输出函数
c语言·开发语言·c++
X***48965 小时前
C源代码生成器
c语言·开发语言
合作小小程序员小小店7 小时前
桌面安全开发,桌面二进制%恶意行为拦截查杀%系统安全开发3.0,基于c/c++语言,mfc,win32,ring3,dll,hook,inject,无数据库
c语言·开发语言·c++·安全·系统安全
oioihoii10 小时前
C++语言演进之路:从“C with Classes”到现代编程基石
java·c语言·c++
历程里程碑13 小时前
各种排序法大全
c语言·数据结构·笔记·算法·排序算法
树在风中摇曳13 小时前
带哨兵位的双向循环链表详解(含 C 代码)+ LeetCode138 深度解析 + 顺序表 vs 链表缓存机制对比(图解 CPU 层级)
c语言·链表·缓存
雨落在了我的手上13 小时前
C语言入门(二十一):字符函数和字符串函数(1)
c语言
embrace9914 小时前
【C语言学习】结构体详解
android·c语言·开发语言·数据结构·学习·算法·青少年编程
EXtreme3515 小时前
深入浅出数据结构:手把手实现动态顺序表,从此不再怕数组扩容!
c语言·顺序表·malloc·realloc