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

//本博主所写的代码仅为阅读者提供参考;

//若有不足之处请提出,博主会尽所能修改;

//附上课后编程练习题目;

//若是对您有用的话请点赞或分享提供给它人;


//9.11 - 1.c

#include <stdio.h>

double min(double x, double y);

int main(void)
{
    
    
    double a, b;

    printf("Please enter two numbers (q to quit): ");
    while (scanf("%lf %lf", &a, &b) == 2)
    {
    
    
        printf("Minimum: %g\n", min(a, b));
        printf("You can enter again (q to quit): ");
    }
    printf("Done.\n");

    return 0;
}

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

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

//9.11 - 2.c

#include <stdio.h>
#define CHAR '#'

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

int main(void)
{
    
    
    int i, j;

    printf("Please enter two numbers (q to quit): ");
    while (scanf("%d %d", &j, &i) == 2)
    {
    
    
        printf("Printing %c in %d rows and %d columns is:\n", CHAR, j, i);
        chline(CHAR, i, j);
        printf("You can enter two numbers again (q to quit): ");
    }
    printf("Done.\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 <stdio.h>
#define CHAR '#'

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

int main(void)
{
    
    
    int i, j;

    printf("Please enter two numbers (q to quit): ");
    while (scanf("%d %d", &j, &i) == 2)
    {
    
    
        printf("Printing %c in %d rows and %d columns is:\n", CHAR, i, j);
        show(CHAR, j, i);
        printf("You can enter two numbers again (q to quit): ");
    }
    printf("Done.\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 <stdio.h>

double harmean(double x, double y);

int main(void)
{
    
    
    double a, b;

    printf("Please enter two numbers (q to quit): ");
    while (scanf("%lf %lf", &a, &b) == 2)
    {
    
    
        printf("The harmonic mean of %g and %g is %g\n", a, b, harmean(a, b));
        printf("You can enter two numbers again (q to quit): ");
    }
    printf("Done.\n");

    return 0;
}

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

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

//9.11 - 5.c

#include <stdio.h>

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

int main(void)
{
    
    
    double a, b;

    printf("Please enter two numbers (q to quit): ");
    while (scanf("%lf %lf", &a, &b) == 2)
    {
    
    
        larger_of(&a, &b);
        printf("Results: %g and %g\n", a, b);
        printf("You can enter two numbers again (q to quit): ");
    }
    printf("Done.\n");

    return 0;
}

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

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

//9.11 - 6.c

#include <stdio.h>

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

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

    printf("Please enter three numbers (q to quit): ");
    while (scanf("%lf %lf %lf", &x, &y, &z) == 3)
    {
    
    
        test(&x, &y, &z);
        printf("Min: %g\n", x);
        printf("Middle: %g\n", y);
        printf("Max: %g\n", z);
        printf("You can enter three numbers again (q to quit): ");
    }
    printf("Done.\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 <stdio.h>
#include <ctype.h>

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

int main(void)
{
    
    
    attain_pos();

    return 0;
}

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

    printf("Please enter a character (EOF to quit):\n");
    while ((ch = getchar()) != EOF)
    {
    
    
        figure = position(ch);
        if (figure)
        {
    
    
            printf("%c is %d in alphabet.\n", ch, figure);
        }
        else
        {
    
    
            printf("%c isn't a letter!\n", ch); //包括回车换行符和空白符;
        }
        while (getchar() != '\n')
            continue;
        printf("You can enter again (EOF to quit): ");
    }
    printf("Done.\n");
}

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

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

//9.11 - 8.c

#include <stdio.h>

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 <stdio.h>

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 <stdio.h>

void to_base_n(int x, int base);

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

    printf("Please enter a number (q to quit): ");
    while (scanf("%ld", &n) == 1)
    {
    
    
        if (n <= 0)
        {
    
    
            printf("Illegal data! Please enter again: ");
            continue;
        }
        printf("Please enter a base system number (2 - 10): ");
        while (scanf("%d", &b) != 1 || (b < 2 || b > 10))
        {
    
    
            while (getchar() != '\n')
                continue;
            printf("Please enteragain (2 - 10): ");
        }
        printf("%d in %d base system is: ", n, b);
        to_base_n(n, b);
        printf("\nYou can enter a number again (q to quit): ");
    }
    printf("Done.\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 <stdio.h>

void Fibonacci(int len);

int main(void)
{
    
    
    int n;
    printf("Please enter a integer (<= 0 or q to quit): ");
    while (scanf("%d", &n) == 1)
    {
    
    
        printf("Top %d items of Fibonacci sequence:\n", n);
        Fibonacci(n);
        printf("You can enter again (<= 0 or q to quit): ");
    }
    printf("Done.\n");

    return 0;
}

void Fibonacci(int len)
{
    
    
    int i;
    unsigned long 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日 -------------------------------

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

猜你喜欢

转载自blog.csdn.net/m0_46181359/article/details/105309108