C Primer Plus 第6版 Chapter 3 课后编程练习

ex3.1

// ex_3.1
#include <stdio.h>
#include <limits.h>
#include <float.h>

int main(void)
{
    int intMax = INT_MAX;
    float floatMax = FLT_MAX;
    printf("The maximum value of int = %d\n", intMax);
    printf("The maximum value of int + 1 = %d\n", intMax + 1);
    printf("The maximum value of float = %.10e\n", FLT_MAX);

    return 0;
}

// 可以自行查看limits.h 和 float.h库的说明文档并尝试

ex3.2

// ex_3.2
#include <stdio.h>

int main(void)
{
    int ch;
    printf("Enter an ASCII code: ");
    scanf("%d", &ch);
    printf("The value of ASCII code is %d.\n", ch);
    printf("The character of ASCII code is %c.\n", ch);

    return 0;
}

ex3.3

// ex_3.3
#include <stdio.h>

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

    return 0;
}

ex3.4

// ex_3.4
#include <stdio.h>

int main(void)
{
    float num = 0;
    printf("Enter a floating-point value: ");
    scanf("%f", &num);
    printf("fixed-point notation: %10f\n", num);
    printf("exponential notation: %e\n", num);
    printf("p notation: %a\n", num);
    
    return 0;
}

本人编译环境是winXP上的CodeBlocks 17.12,编译器添加C11标准。运行结果如下:

-------------------------------------------------------------------------------------------------------------------------------

ex3.6

// ex_3.6
#include <stdio.h>
#define MOLECULE_OF_WATER (3.0e-23)
#define QUART_OF_WATER 950

int main(void)
{
    double num_of_quart = 0;
    printf("How many quarts of water you have: ");
    scanf("%lf", &num_of_quart);
    double num_of_molecule = num_of_quart * QUART_OF_WATER / MOLECULE_OF_WATER;
    printf("%e", num_of_molecule);

    return 0;
}

-------------------------------------------------------------------------------------------------------------------------------

ex3.7

// ex_3.7
#include <stdio.h>
#define INCH_TO_CM 2.54

int main(void)
{
    float inch = 0;
    float cm = 0;
    printf("What's your height in inch:");
    scanf("%f", &inch);
    cm = INCH_TO_CM * inch;
    printf("Your height is %.1f cm.", cm);

    return 0;
}

-------------------------------------------------------------------------------------------------------------------------------

ex3.8

// ex_3.8
#include <stdio.h>
#define PINT_TO_CUP 2
#define CUP_TO_OUNCE 8
#define OUNCE_TO_TABLESPOON 2
#define TABLESPOON_TO_TEASPOON 3

int main(void)
{
    float pints = 0;
    float cups = 0;
    float ounces = 0;
    float tablespoons = 0;
    float teaspoons = 0;

    printf("Enter a volume in cups: ");
    scanf("%f", &cups);
    pints = cups / PINT_TO_CUP;
    ounces = cups * CUP_TO_OUNCE;
    tablespoons = ounces * OUNCE_TO_TABLESPOON;
    teaspoons = tablespoons * TABLESPOON_TO_TEASPOON;
    printf("PINT: %.2f\n", pints);
    printf("CUP: %.2f\n", cups);
    printf("OUNCE: %.2f\n", ounces);
    printf("TABLE SPOON: %.2f\n", tablespoons);
    printf("TEA SPOON: %.2f\n", teaspoons);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/seeleday/article/details/82797749