C Primer Plus(第6版)第九章答案

1.
#include <stdio.h>

double min (double x, double y);

int main (void)
{
    double x, y;

    printf ("Enter two numbers of double:\n");
    scanf ("%lf %lf", &x, &y);

    printf ("%lf", min (x, y));

    return 0;
}


double min (double a, double b)
{
    double c = a;

    if (a > b)
        c = b;

    return c;
}


2.

#include <stdio.h>

void chline (char ch, int i, int j);

int main (void)
{
    int i, j;
    char ch;

    printf ("Enter a word:\n");
    scanf ("%c", &ch);
    printf ("Enter the number of column and line:\n");
    scanf ("%d %d", &i, &j);

    chline (ch, i, j);

    return 0;
}


void chline (char ch, int i, int j)
{
    int a, b;

    for (a = 0; a < j; a++)
    {
        for (b = 0; b < i; b++)
            printf ("%c", ch);
        printf ("\n");
    }
}

3.

第三题与第二题题目描述一致

#include <stdio.h>

void chline (char ch, int i, int j);

int main (void)
{
    int i, j;
    char ch;

    printf ("Enter a word:\n");
    scanf ("%c", &ch);
    printf ("Enter the number of column and line:\n");
    scanf ("%d %d", &i, &j);

    chline (ch, i, j);

    return 0;
}


void chline (char ch, int i, int j)
{
    int a, b;

    for (a = 0; a < j; a++)
    {
        for (b = 0; b < i; b++)
            printf ("%c", ch);
        printf ("\n");
    }
}


4.

#include <stdio.h>

double aver (double a, double b);

int main (void)
{
    double i, j;

    printf ("Enter two numbers of double:\n");
    scanf ("%lf %lf", &i, &j);
    printf ("%lf", aver (i, j));

    return 0;
}


double aver (double i, double j)
{
    double a = (1 / j + 1 / i) / 2;

    return 1 / a;
}


5.

#include <stdio.h>

void larger_of (double, double);

int main (void)
{
    double a, b;

    printf ("Enter two numbers of double:\n");
    scanf ("%lf %lf", &a, &b);
    larger_of (a, b);

    return 0;
}


larger_of (double a, double b)
{
    if (a > b)
        printf ("a = b = %lf", a);
    else
        printf ("a = b = %lf", b);
}

6.

#include <stdio.h>

void com (double*, double*, double*);

int main (void)
{
    double a, b, c;

    printf ("Enter three numbers of double:\n");
    scanf ("%lf %lf %lf", &a, &b, &c);
    com (&a, &b, &c);
    printf ("a = %lf\nb = %lf\nc = %lf", a, b, c);

    return 0;
}


void com (double* a, double* b, double* c)
{
    double t;

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

7.

#include <stdio.h>

int loca (char ch);

int main (void)
{
    printf ("Enter a character:\n");
    printf ("%d", loca (getchar ()));
}


int loca (char ch)
{
    while (ch != EOF)
    {
        if (((ch >= 'a') && (ch <= 'z')))
        {
            return ch - 'a' + 1;
        }
        else if (((ch >= 'A') && (ch <= 'Z')))
        {
            return ch - 'A' + 1;
        }
        else
           return -1;
    }
}

8.

#include <stdio.h>

float power (int a, int b);

int main (void)
{
    int index;
    int base;

    printf ("请输入底数:\n");
    scanf ("%d", &base);
    printf ("请输入指数:\n");
    scanf ("%d", &index);
    printf ("结果是:%f", (float) power (base, index));

    return 0;
}


float power (int a, int b)
{
    float res;
    int i;

    b = (b < 0) ? b : -b;
    if (a == 0)
        res = 0;
    else if (b == 0)
        res = 1;
    else
    {
        res = 1;
        for (i = 0; i < -b; i++)
        {
            res *= a;
        }
        res = 1 / res;
    }

    return res;
}

9.

#include <stdio.h>

double power (double, int );

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

    printf ("请输入底数和指数:\n");
    scanf ("%lf%d", &x, &exp);
    printf ("%.3g to the power %d is %.5g\n", x, exp, power (x, exp));

    return 0;
}


double power (double n, int p)
{
    int i;
    double pow=1;

    if (p>0)
        for (i=1; i<=p; i++)
        pow *= n;
    else if (p<0)
        pow = 1 / power (n, -p);
    else if (n != 0)
        pow = 1;
    else pow = 1 / n;

 return pow;
}

10.

#include <stdio.h>
#include <math.h>

float to_base_n (int, int);

int main (void)
{
    int base, n;

    printf ("Enter base:\n");
    scanf ("%d", &base);
    printf ("Enter n:\n");
    while ((scanf ("%d", &n)) == 1)
    {
        printf ("The number base to n is: %f\n", to_base_n (base, n));
        printf ("Enter base:\n");
        scanf ("%d", &base);
        printf ("Enter n:\n");
    }

    return 0;
}


float to_base_n (int base, int n)
{
    int a, b;
    int i = 0;
    float s = 0;

    do
    {
        b = base / n;
        a = base % n;
        s = a * pow (10, i) + s;
        base = b;
        i++;
    }
    while (b != 0);

    return s;
}

11.

#include <stdio.h>

int Fib (int num);

int main (void)
{
    int num;
    char ch;

    printf ("请输入你想知道的斐波那契数的序号:\n");
    while (scanf ("%u", &num) == 1)
    {
        if (num <= 0)
        {
            printf ("输入有误,请重新输入:\n");
            continue;
        }
        printf ("第%d号斐波那契数是:%u\n", num, Fib (num));
        printf ("\n请输入你想知道的斐波那契数的序号:\n");
    }

}


int Fib (int num)
{
    int i, j, a, t;

    i = j = 1;
    if ((num == 1) || (num == 2))
        return 1;
    for (a = 2; a < num; a++)
    {
        t = i + j;
        j = i;
        i = t;
    }
    return t;
}

猜你喜欢

转载自blog.csdn.net/sirius_black_tea/article/details/75907421