C Primer Plus Sixth Edition (Chinese Edition) Chapter 3 (Perfect Revised Edition) Programming Practice Answers

//The code written by this blogger is only for readers' reference;

//If you have any shortcomings, please raise them, and the blogger will do their best to modify;

//Attach the programming practice questions after class;

//If it is useful to you, please like or share it with others;



//3.11 - 1.c

#include <stdio.h>
#include <float.h>
#include <limits.h>

int main(void)
{
    
    
    int big_int = 2147483647;
    //↑signed int最大值是2的31次方减1;
    float big_float = 3.4e38;
    //↑float最大值一般是3.4E38;
    float small_float = 10.0 / 3;
    //↑float有效位数是6位;
    printf("The big int data is %d.\n", big_int + 1);
    //↑整型数据最大值+1会造成越界,结果是-2147493648;
    printf("The big float data is %f.\n", big_float * 10);
    //↑float最大值*10会造成越界,结果是inf;
    printf("The big bit float data is %f.\n", small_float);
    //↑float最大有效精度;
    printf("The max float data is %f.\n", FLT_MAX);
    //↑float类型最大值;
    printf("The max int data is %ld.\n", INT_MAX);
    //↑int类型最大值;

    return 0;
}

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

//3.11 - 2.c

#include <stdio.h>

int main(void)
{
    
    
    int ch;

    printf("Please you enter a ASCII value:");
    scanf("%d", &ch);
    printf("ASCII %d is %c.\n", ch, ch);

    return 0;
}

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

//3.11 - 3.c

#include <stdio.h>

int main(void)
{
    
    
    printf("\a");
    printf("Startled by the sudden sound, ");
    printf("Sally shouted,\n");
    printf("\"By the Great Pumpkin, what was that!\"\n");

    return 0;
}

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

//3.11 - 4.c

#include <stdio.h>

int main(void)
{
    
    
    float number;

    printf("Enter a float-point value:");
    scanf("%f", &number);
    printf("fixed-point notation: %f\n", number);
    //↑小数点形式;
    printf("exponential notation: %e\n", number);
    //↑指数形式;
    printf("p notation: %a\n", number);
    //↑十六进制记数法;

    return 0;
}

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

//3.11 - 5.c

#include <stdio.h>
#define SEC_PER_YEAR 3.156e7

int main(void)
{
    
    
    double age, seconds;

    printf("Please enter your age:");
    scanf("%lf", &age);
    seconds = age * SEC_PER_YEAR;
    printf("%g years old includes %g seconds.\n", age, seconds);

    return 0;
}

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

//3.11 - 6.c

#include <stdio.h>
#define MASS_PER_MOLE 3.0e-23
#define MASS_PER_QUART 950

int main(void)
{
    
    
    double quart, molecules;

    printf("Please enter a quart for water:");
    scanf("%lf", &quart);
    //1夸脱水大约是950克;
    //1个水分子质量约为3.0 * 10^(-23)克;
    molecules = quart * MASS_PER_QUART / MASS_PER_MOLE;
    printf("%g quart water includes %e water molecules.\n", quart, molecules);

    return 0;
}

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

//3.11 - 7.c

#include <stdio.h>
#define CM_PER_INCH 2.54

int main(void)
{
    
    
    double inch, cm;

    printf("Please enter your height(inch): ");
    scanf("%lf", &inch);
    cm = inch * CM_PER_INCH;
    //1英寸相当于2.54厘米;
    printf("Your height is %g cm.\n", cm);

    return 0;
}

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

//3.11 - 8.c

#include <stdio.h>
#define PINT_PER_CUP 2
#define CUP_PER_OUNCE 8
#define OUNCE_PER_BIGSPOON 2
#define BIGSPOON_PER_TEASPOON 3

int main(void)
{
    
    
    double pint, ounce, cup;
    double bigspoon, teaspoon;

    printf("Please you enter a number of cups: ");
    scanf("%lf", &cup);
    pint = cup / PINT_PER_CUP;
    ounce = cup * CUP_PER_OUNCE;
    bigspoon = ounce * OUNCE_PER_BIGSPOON;
    teaspoon = bigspoon * BIGSPOON_PER_TEASPOON;

    printf("Here are some ways to show %g cups\n", cup);
    printf("Pint: %g\n", pint);
    printf("Ounce: %g\n", ounce);
    printf("Bigspoon: %g\n", bigspoon);
    printf("Teaspoon: %g\n", teaspoon);

    return 0;
}

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

//----------------------------April 2, 2020-------------- -----------------

Guess you like

Origin blog.csdn.net/m0_46181359/article/details/105280579