Jimmy Chen

A Programmer

C Primer Plus 第六版 第九章课后编程练习答案

Advertisements
Advertisements
Advertisements

编程环境Visual Studio 2017

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


// practice 1
double min_rewrite(double x, double y)
{
    return (x < y ? x : y);
}
void p9_1(void)
{
    double number1 = 0.0;
    double number2 = 0.0;

    printf("Enter the first number:");
    scanf_s("%lf", &number1);
    getchar();


    printf("Enter the second number:");
    scanf_s("%lf", &number2);
    getchar();

    printf("The min number of %lf and %lf is %lf", number1, number2, min_rewrite(number1, number2));

    return;
}



// practice 2
void chline(char *ch, int i, int j)
{
    int m = 0;
    int n = 0;
    int tmp = 0;
    while ((*(ch + tmp)) != '\0')
    {

        if (m == i - 1)
        {
            break;
        }
        if ('\n' == (*(ch + tmp)))
        {
            m++;
        }
        tmp++;
    }

    for (n = 0; n < j; n++)
    {
        if (n == j - 1)
        {
            putchar(ch[tmp]);
        }
        tmp++;
    }
}
void p9_2(void)
{
    int i = 0;
    int j = 0;
    char ch = 0;
    char *words = "hello world!\nThis is the bulteaful world!\nLearn to program in c language!\nc language is a nice language!";

    printf("Please enter the row:");
    scanf_s("%d", &i);
    getchar();

    printf("Please enter the column:");
    scanf_s("%d", &j);
    getchar();

    printf("The character of %d row %d column is ", i, j);
    chline(words, i, j);

    printf("\n");

    return;
}


// practice 3
void print_n_char(char ch, int repeat_time, int repeat_row)
{
    for (int i = 0; i < repeat_row; i++)
    {
        for (int j = 0; j < repeat_time; j++)
        {
            putchar(ch);
        }
        putchar('\n');
    }

    return;
}
void p9_3()
{
    int i = 0;
    int j = 0;
    char ch = 0;

    printf("Enter the character you want to print:");
    ch = getchar();
    getchar();

    printf("Enter the number of times to print:");
    scanf_s("%d", &i);
    getchar();

    printf("Enter the number of lines to print:");
    scanf_s("%d", &j);
    getchar();

    print_n_char(ch, i, j);

    return;

}


// practice 4
double harmonic_avg(double number1, double number2)
{
    double reciprocal1 = 1 / number1;
    double reciprocal2 = 1 / number2;
    double sum = reciprocal1 + reciprocal2;
    double sum_avg = sum / 2;
    return (1 / sum_avg);
}
void p9_4(void)
{
    double number1 = 0.0;
    double number2 = 0.0;
    double result = 0.0;

    printf("Ener the first double number:");
    scanf_s("%lf", &number1);
    getchar();

    printf("Enter the second double number:");
    scanf_s("%lf", &number2);
    getchar();

    result = harmonic_avg(number1, number2);

    printf("The harmonic average of %lf and %lf is %lf\n", number1, number2, result);

    return;
}


// practice 5
void larger_of(double *x, double *y)
{
    if (*x > *y)
    {
        *y = *x;
    }
    else
    {
        *x = *y;
    }
}
void p9_5(void)
{
    double number1 = 0.0;
    double number2 = 0.0;

    printf("Enter the first double number:");
    scanf_s("%lf", &number1);
    getchar();

    printf("Enter the second double number:");
    scanf_s("%lf", &number2);
    getchar();

    printf("Before larger_of, the two numbers are %lf and %lf\n", number1, number2);
    larger_of(&number1, &number2);
    printf("After larger_of, the two number are %lf and %lf\n", number1, number2);

    return;
}


// practice 6
void rank_three_double(double *number1, double *number2, double *number3)
{
    double tmp = 0.0;
    if (*number1 > *number2)
    {
        tmp = *number1;
        *number1 = *number2;
        *number2 = tmp;
    }
    if (*number1 > *number3)
    {
        tmp = *number1;
        *number1 = *number3;
        *number3 = tmp;
    }
    if (*number2 > *number3)
    {
        tmp = *number2;
        *number2 = *number3;
        *number3 = tmp;
    }
    return;
}
void p9_6(void)
{
    double number1 = 0.0;
    double number2 = 0.0;
    double number3 = 0.0;

    printf("Enter the first double number:");
    scanf_s("%lf", &number1);
    getchar();

    printf("Enter the second double number:");
    scanf_s("%lf", &number2);
    getchar();

    printf("Enter the third double number:");
    scanf_s("%lf", &number3);
    getchar();

    printf("Before rank, the three double number are: %lf, %lf and %lf\n", number1, number2, number3);
    rank_three_double(&number1, &number2, &number3);
    printf("After rank, the three double number are: %lf, %lf and %lf\n", number1, number2, number3);

    return;
}


// practice 7
// 不好模拟文件末尾,所以用#代替文件末尾
int judge_alpha(char ch)
{
    if (isalpha(ch))
    {
        return ch - 'a' + 1;
    }
    else
    {
        return -1;
    }
}
void p9_7(void)
{
    char ch = 0;
    int result = 0;

    printf("Enter the character:(# to quit):");
    while ((ch = getchar()) != '#')
    {
        if ('\n' == ch)
        {
            continue;
        }
        int jud = judge_alpha(ch);
        if (-1 == jud)
        {
            printf("%c is not a alpha.\n", ch);
        }
        else
        {
            printf("%c:%d\n", ch, jud);
        }
        printf("Enter the next character:(# to quit):");
    }

    return;
}


// practice 8
double pow_rewrite(double base, double index)
{
    if (index > 0)
    {
        return pow(base, index);
    }
    if (0 == index)
    {
        if (0 == base)
        {
            printf("The base number is 0 is not defined!\n");
        }

        return 1;
    }
    if (index < 0)
    {
        double tmp = pow(base, -index);
        return (1 / tmp);
    }
}
void p9_8(void)
{
    double base = 0.0;
    double index = 0.0;
    double result = 0.0;

    printf("Enter the base number:");
    scanf_s("%lf", &base);
    getchar();

    printf("Enter the index number:");
    scanf_s("%lf", &index);
    getchar();

    result = pow_rewrite(base, index);
    printf("base number: %lf, index number: %lf, the result: %lf\n", base, index, result);

    return;
}


// practice 9
double pow_recursive(double base, double index)
{
    if (index > 0)
    {
        index--;
        return (base * pow_recursive(base, index));
    }
    if (0 == index)
    {
        if (base == 0)
        {
            printf("The base number is 0 is not defined!\n");
        }
        return 1;
    }
    if (index < 0)
    {
        index++;
        double tmp = 1 / base;
        return (tmp * pow_recursive(base, index));
    }
}
void p9_9(void)
{
    double base = 0.0;
    double index = 0.0;
    double result = 0.0;

    printf("Enter the base number:");
    scanf_s("%lf", &base);
    getchar();

    printf("Enter the index number:");
    scanf_s("%lf", &index);
    getchar();

    result = pow_recursive(base, index);
    printf("base number: %lf, index number: %lf, the result: %lf\n", base, index, result);

    return; 
}


// practice 10
void to_base_n(unsigned long n, int base)
{
    int r = 0;

    r = n % base;
    if (n >= base)
    {
        to_base_n(n / base, base);
    }
    putchar(('0' + r));

    return;
}
void p9_10(void)
{
    unsigned long number = 0;
    int base = 0;

    printf("Enter the value that needs to be converted:");
    scanf_s("%ld", &number);
    getchar();

    printf("Enter the value of the base number:");
    scanf_s("%d", &base);
    getchar();

    printf("The result is: ");
    to_base_n(number, base);
    putchar('\n');

    return;
}


// practice 11
void Fibonacci(unsigned int n)
{
    int fib1 = 1;
    int fib2 = 1;

    printf("%d, %d", fib1, fib2);

    for (size_t i = 0; (i = (fib1 + fib2)) < n;)
    {
        printf(", %d", i);
        fib1 = fib2;
        fib2 = i;
    }

    putchar('\n');
    return;
}
void p9_11(void)
{
    unsigned long num = 0;

    printf("Enter the larger number of fabonicci:");
    scanf_s("%ld", &num);
    getchar();

    Fibonacci(num);

    return;
}



int main(int argc, char **argv)
{
    p9_11();

    while (getchar())
    {

    }

    return;
}

此文为博主原创文章,转载请注明出处

Advertisements
Advertisements
  1. Ninw说道:

    第八题
    你看下是不是有问题?
    我看不懂,我编译器结果输出不正确。

    递归这玩意该怎么理解啊?

    1. jimmychen说道:

      第八题我这边看这没有问题啊;递归理解可以简单理解为在一个函数里面自己调用自己,然后在合适的时候通过return来借宿递归

  2. Mr_cola说道:

    :exclaim:博主大大= =,能不能和我解释一下11题的代码最后int main(int argc, char **argv)这个函数的意义。感觉摸不到头脑,删掉这段结果也无误。如果能抽空解惑的话,万分感谢

    1. jimmychen说道:

      呃兄弟,这是c程序的main函数啊

      1. Mr_cola说道:

        = =之前没弄懂 char**是什么hh。现在已经明白了hh

  3. CQ说道:

    博主 请问一下为什么第一题要用getchar()
    新手求教 万分感谢!!!

  4. FDCQ说道:

    博主 请问第一题为什么要用getchar()??
    新手求教 万分感谢

    1. jimmychen说道:

      用来读取缓冲里面的回车键

  5. 友人A说道:

    第八题复制你的代码编译有警告,warning: control reaches end of non-void function。 位置是子函数的最后一行,是编译器的问题吗?我用的codeblock

    1. jimmychen说道:

      这是因为编译器检查到pow_rewrite子函数里面并不是每个执行路径里面都有返回值,所以才报的warning,你可以将if(index > 0)这个判断语句去掉,然后将return pow(base, index)作为默认的return语句

  6. sunrise说道:

    第二题 第三题 我怎么感觉是同一个意思,楼主能解释下区别么 这两题

  7. 新手学员说道:

    楼主,你这个函数有问题,运行起来不能完全显示出来,如果输入n等于6,显示前6个Fibonacci。
    结果只能显示:1 1 2 3 5,因为条件fib1 + fib2 等于8 ,判别式fib1 + fib2 < n 此时n等于6已经不成立。
    // practice 11
    void Fibonacci(unsigned int n)
    {
    int fib1 = 1;
    int fib2 = 1;

    printf("%d, %d", fib1, fib2);

    for (size_t i = 0; (i = (fib1 + fib2)) < n;)
    {
    printf(", %d", i);
    fib1 = fib2;
    fib2 = i;
    }

    putchar('\n');
    return;
    }
    应改为:
    void Fibonacci(unsigned int n)
    {
    int fib1 = 1;
    int fib2 = 1;
    int sum =0;
    printf("%d %d", fib1, fib2);
    for(int i = 2; i < n; i++)
    {
    sum = fib1 + fib2;
    printf(" %d", sum);
    fib1 = fib2;
    fib2 = sum;
    }
    return ;
    }
    这样的话就能完全达到题目要求。

  8. junxingzhou说道:

    博主 你第九题用递归处理幂函数的递归部分,在Xcode中提示这个bug"Control may reach end of non-void function" 请问如何解决呢

  9. c0nuT说道:

    博主,第七题中,如果是大写的话,题目要求返回的是跟大写一样的值,是不是应该多加一个大小写的判断? :confused:

    1. adsdf说道:

      直接return tolower(ch)-'a'+1;就可以了。

  10. GR10说道:

    答主递归写错了

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

%d 博主赞过: