C Primer Plus 第六版(中文版)第九章(完美版)编程练习答案

//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽所能修改;
//附上课后编程练习题目;
//若是对您有用的话请点赞或分享提供给它人;
C Primer Plus 第六版(中文版)第九章(完美版)编程练习答案_第1张图片
//9.11 - 1.c

#include 

double min(double x, double y);

int main(void)
{
    double a, b;

    printf("请您输入2个数(按q退出本程序):");
    while (scanf("%lf %lf", &a, &b) == 2)
    {
        printf("最小的数是:%g\n", min(a, b));
        printf("您可以再次输入2个数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

double min(double x, double y)
{
    return (x < y ? x : y);
}

//-------------

//9.11 - 2.c

#include 
#include 

void chline(int ch, int cols, int rows);

int main(void)
{
    int str, i, j;

    printf("请输入一个字符:\n");
    do
    {
        str = getchar();
    } while (isspace(str)); //←输入空白符打印时效果不明显;
    while (getchar() != '\n')
        continue;

    printf("请输入2个正整数(按q退出本程序):");
    while ((scanf("%d %d", &j, &i)) == 2)
    {
        if ((j <= 0) || (i <= 0))
        {
            printf("数据无效!请重新输入:");
            continue;
        }
        printf("打印%c的%d行%d列是:\n", str, j, i);
        chline(str, i, j);
        printf("您可以再次输入2个正整数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void chline(int ch, int cols, int rows)
{
    int a, b;

    for (a = 1; a <= rows; a++)
    {
        for (b = 1; b <= cols; b++)
        {
            putchar(ch);
        }
        putchar('\n');
    }
    return;
}

//-------------

//9.11 - 3.c

#include 
#include 

void show(int ch, int cols, int rows);

int main(void)
{
    int str, i, j;

    printf("请输入一个字符:\n");
    do
    {
        str = getchar();
    } while (isspace(str)); //←输入空白符打印时效果不明显;
    while (getchar() != '\n')
        continue;

    printf("请输入2个正整数(按q退出本程序):");
    while ((scanf("%d %d", &i, &j)) == 2)
    {
        if ((i <= 0) || (j <= 0))
        {
            printf("数据无效!请重新输入:");
            continue;
        }
        printf("打印%c的%d行%d列是:\n", str, j, i);
        show(str, j, i);
        printf("您可以再次输入2个正整数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void show(int ch, int cols, int rows)
{
    int a, b;

    for (a = 1; a <= rows; a++)
    {
        for (b = 1; b <= cols; b++)
        {
            putchar(ch);
        }
        putchar('\n');
    }
    return;
}

//-------------

//9.11 - 4.c

#include 

double harmean(double x, double y);

int main(void)
{
    double a, b;

    printf("请您输入2个数(按q退出本程序):");
    while ((scanf("%lf %lf", &a, &b)) == 2)
    {
        printf("%g和%g的调和平均数是:%g\n", a, b, harmean(a, b));
        printf("您可以再次输入2个数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

double harmean(double x, double y)
{
    return 2 / (1 / x + 1 / y);
}

//-------------

//9.11 - 5.c

#include 

void larger_of(double *x, double *y);

int main(void)
{
    double a, b;

    printf("请输入2个数(按q退出本程序):");
    while (scanf("%lf %lf", &a, &b) == 2)
    {
        larger_of(&a, &b);
        printf("替换成较大的值是%g和%g\n", a, b);
        printf("您可以再次输入2个数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void larger_of(double *x, double *y)
{
    *x = *y = (*x > *y ? *x : *y);
    return;
}

//-------------

//9.11 - 6.c

#include 

void test(double *a, double *b, double *c);

int main(void)
{
    double x, y, z;

    printf("请输入3个数(按q退出本程序):");
    while (scanf("%lf %lf %lf", &x, &y, &z) == 3)
    {
        test(&x, &y, &z);
        printf("最小值是%g\n", x);
        printf("中间值是%g\n", y);
        printf("最大值是%g\n", z);
        printf("您可以再次输入3个数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void test(double *a, double *b, double *c)
{
    double tp;

    if (*a > *b)
    {
        tp = *a;
        *a = *b;
        *b = tp;
    }
    if (*a > *c)
    {
        tp = *a;
        *a = *c;
        *c = tp;
    }
    if (*b > *c)
    {
        tp = *b;
        *b = *c;
        *c = tp;
    }
    return;
}

//-------------

//9.11 - 7.c

#include 
#include 

void attain_pos(void);
int position(int ch);

int main(void)
{
    attain_pos();

    return 0;
}

void attain_pos(void)
{
    int ch;
    int figure;

    printf("请您输入一些字符(遇到EOF结束):\n");
    while ((ch = getchar()) != EOF)
    {
        figure = position(ch);
        if ((figure != -1) && isupper(ch))
        {
            printf("%c是第%d个大写字母.\n", ch, figure);
        }
        else if ((figure != -1) && islower(ch))
        {
            printf("%c是第%d个小写字母.\n", ch, figure);
        }
        else
        {
            printf("%c不是字母!\n", ch); //包括回车换行符和空白符;
        }
    }
    printf("本程序完成!\n");
}

int position(int ch)
{
    if (isupper(ch))
    {
        return (ch - 'A' + 1);
    }
    else if (islower(ch))
    {
        return (ch - 'a' + 1);
    }
    return -1;
}

//-------------

//9.11 - 8.c

#include 

double power(double n, int p);

int main(void)
{
    double x, xpow;
    int exp;

    printf("Enter a number and the 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)
{
    int i;
    double pow = 1.0;

    if ((0 == p) && (0 == n))
    {
        printf("0 to the 0 undefined, using 1 as the value.\n");
        return pow;
    }
    if (0 == n)
    {
        pow = 0.0;
        return pow;
    }
    if (0 == p)
    {
        return pow;
    }
    if (p > 0)
    {
        for (i = 1; i <= p; i++)
        {
            pow *= n;
        }
        return pow;
    }
    else
    {
        for (i = 1; i <= -p; i++)
        {
            pow *= 1 / n;
        }
        return pow;
    }
}

//-------------

//9.11 - 9.c

#include 

double power(double n, int p);

int main(void)
{
    double x, xpow;
    int exp;

    printf("Enter a number and the 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.0;

    if ((0 == p) && (0 == n))
    {
        printf("0 to the 0 undefined, using 1 as the value.\n");
        return pow;
    }
    if (0 == n)
    {
        pow = 0.0;
        return pow;
    }
    if (0 == p)
    {
        return pow;
    }
    if (p > 0)
    {
        return n * power(n, p - 1);
    }
    else
    {
        return power(n, p + 1) / n;
    }
}

//-------------

//9.11 - 10.c

#include 

void to_base_n(int x, int base);

int main(void)
{
    int b;
    long int n;

    printf("请输入一个正整数(按q退出本程序):");
    while (scanf("%ld", &n) == 1)
    {
        if (n <= 0)
        {
            printf("数据无效!请您重新输入(或按q退出):");
            continue;
        }
        printf("请输入一个需要转换的进制(2-10):");
        while ((scanf("%d", &b) != 1) || (b < 2 || b > 10))
        {
            while (getchar() != '\n')
                continue;
            printf("输入无效,进制转换的范围仅限于2-10:");
        }
        printf("%d的%d进制是:", n, b);
        to_base_n(n, b);
        printf("\n您可以再次输入一个正整数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void to_base_n(int x, int base)
{
    int r;

    r = x % base;
    if (x >= base)
    {
        to_base_n(x / base, base);
    }
    printf("%d", r);
    return;
}

//-------------

//9.11 - 11.c

#include 

void Fibonacci(int len);

int main(void)
{
    int n;
    printf("请您输入一个正整数(按q退出本程序):");
    while (scanf("%d", &n) == 1)
    {
        if (n <= 0)
        {
            printf("数据无效!请重新输入:");
            continue;
        }
        printf("斐波那契数列前%d项是:\n", n);
        Fibonacci(n);
        printf("你可以再次输入一个正整数(或按q退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

void Fibonacci(int len)
{
    int i;
    unsigned long int t, x, y;
    x = y = 1;

    for (i = 0; i < len; i++)
    {
        printf("%-lu\n", x);
        t = x + y;
        x = y;
        y = t;
    }
    return;
}

//-------------

//----------------------------2020年4月4日 -------------------------------

//致敬英雄,缅怀同胞,愿山河无恙,人间皆安!

你可能感兴趣的:(C,Primer,Plus)